当前位置: 首页>>代码示例>>C++>>正文


C++ TCPSocket::write方法代码示例

本文整理汇总了C++中TCPSocket::write方法的典型用法代码示例。如果您正苦于以下问题:C++ TCPSocket::write方法的具体用法?C++ TCPSocket::write怎么用?C++ TCPSocket::write使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在TCPSocket的用法示例。


在下文中一共展示了TCPSocket::write方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: request

 void Client::request(
     Method method, Uri uri,
     Header header,
     void *data, int len){
     
     TCPSocket *socket = new TCPSocket(
         uri.getDomain().c_str(), uri.getPort(),
         [=](int err, TCPSocket *socket){
             if( err != eNoError ){
                 EP_SAFE_DEFER( responseCallback, err, Header(),"" );
             }
             else{
                 string *buffer = new string();
                 
                 socket->onReceive(
                     [=](void *data, int len){
                         buffer->append( (char*)data );
                     });
                 socket->onUnbind(
                     [=](){
                         Header header;
                         string body;
                         int offset = header.load( *buffer );
                         
                         /*
                          todo : parse chunked content-length
                          */
                         header.getField(
                             "Transfer-Encoding");
                         
                         if( offset == - 1 ){
                             EP_SAFE_DEFER( responseCallback, eParseError, Header(),"" );
                         }
                         else{
                             body = buffer->substr( offset );
                             
                             EP_SAFE_DEFER( responseCallback, eNoError, header, body );
                         }
                         
                         delete buffer;
                     });
                 
                 string requestLine =
                     generateRequestLine( method, uri.getRequestUri(), "HTTP/1.1" );
                 string headerString =
                     header.dump();
                 
                 socket->write(
                     (void*)requestLine.c_str(), requestLine.size());
                 socket->write(
                     (void*)headerString.c_str(), headerString.size());
                 
                 if( len > 0 ){
                     socket->write( data, len );
                 }
             }
         });
 }
开发者ID:pjc0247,项目名称:EventPie,代码行数:58,代码来源:Client.cpp

示例2: test

bool TcpSocketTest::test(){
	start();
	sleep(1);
	TCPSocket* client = new TCPSocket("127.0.0.1",9586);
	string msg = "12345678901234567890";
	client->write(msg.c_str(), msg.length());
	cout<<"client sent: "<<msg<<endl;
	char buff[1024];
	ssize_t rc = client->read(buff,sizeof(buff));
	buff[rc] = '\0';
	cout<<"client recv: "<<buff<<endl;

	client->close();
	delete client;


	if(rc == (int)msg.size()){
		if(strcmp(msg.c_str(),buff) == 0){
			return true;
		}
	}
	cerr<<"TCP test failed"<<endl;
	waitForThread();
	return false;
}
开发者ID:GalProject,项目名称:Network-Programming-Project,代码行数:25,代码来源:TcpSocketTest.cpp

示例3: runClient

void runClient(const char * host, int port, const char * message)
{
  printf("Setting up a client socket to %s:%d\n", host, port);

  TCPSocket socket;
  int err = socket.open(host, port);

  if(err) {
    const int  msgSize = 128;
    char  msgBuf[msgSize] = "";
    printf("%s cannot open client socket to %s:%d -> %s\n", 
	   appname, host, port, strerror_s(msgBuf, msgSize, err));
    return;
  }

  socket.setNoDelay(true);

  char buf[1024];

  for(int i = 0; i < iterations; i++) {

    puts("Sending message");

    sprintf(buf, "%s %d", message, i + 1);
    
    int32_t len = strlen(buf) + 1;
    socket.write( & len, 4);
    socket.write(buf, len);

    if(withreplies) {
      len = 0;
      socket.read( & len, 4);
      bzero(buf, sizeof(buf));
      socket.read(buf, len);
      printf("Received reply \"%s\"\n", buf);
    }
  }

  puts("Closing");

  socket.close();
}
开发者ID:enuuros,项目名称:multitude,代码行数:42,代码来源:Main.cpp

示例4: run

void TcpSocketTest::run(){
	TCPSocket* server = new TCPSocket(9586);
	TCPSocket* peer = server->listenAndAccept();
	cout<<"server got peer connection"<<endl;

	char buff[1024];
	int rc = peer->read(buff,sizeof(buff));

	buff[rc] = '\0';
	cout<<"server recv: "<<buff<<endl;

	peer->write(buff, strlen(buff));
	cout<<"server sent: "<<buff<<endl;

	peer->close();
	server->close();
	delete peer;
	delete server;
}
开发者ID:GalProject,项目名称:Network-Programming-Project,代码行数:19,代码来源:TcpSocketTest.cpp

示例5: runServer

void runServer(const char * host, int port, bool withBlocking)
{
  printf("Setting up a server socket to %s:%d\n", host, port);

  char buf[1024];

  TCPServerSocket server;
  int i, err = server.open(host, port, 5);
// err = server.open(0, port, 5)
  if(err) {
    const int  msgSize = 128;
    char  msgBuf[msgSize] = "";
    printf("%s cannot open server socket to %s:%d -> %s\n", 
	   appname, host, port, strerror_s(msgBuf, msgSize, err));
    return;
  }
  
  for(i = 0; i < 10; i++) {
    puts("Waiting for a connection");

    while(withBlocking && !server.isPendingConnection(1000000)) {
      printf(".");
      fflush(0);
    }

    TCPSocket * socket = server.accept();

    if(!socket) {
      Radiant::error("Could not create accept a socket connection.");
      return;
    }

    socket->setNoDelay(true);

    printf("Got a new socket %p\n", socket);
    fflush(0);

    for(int j = 0; j < iterations; j++) {
      int32_t len = 0;
      buf[0] = '\0';
      int n = socket->read( & len, 4);
      
      if(n != 4) {
	Radiant::error("Could not read 4 header bytes from the socket, got %d", n);
	delete socket;
	return;
      }
      
      n = socket->read(buf, len);
      
      if(n != len) {
	Radiant::error("Could not read %d data bytes from the socket, got %d",
		       len, n);
      }
      
      printf("Received \"%s\"\n", buf);

      if(withreplies) {
	socket->write( & len, 4);
	socket->write( & buf, len);
      }
    }

    delete socket;
  }
  
  fflush(0);

  info("%s %d clients handled, returning", appname, i);
}
开发者ID:enuuros,项目名称:multitude,代码行数:70,代码来源:Main.cpp

示例6: send

//-----------------------------------------------------------------------------
void ConsoleServer::send(TCPSocket client, const char* message)
{
	uint32_t msg_len = string::strlen(message);
	client.write((const char*) &msg_len, 4);
	client.write(message, msg_len);
}
开发者ID:dgu123,项目名称:crown,代码行数:7,代码来源:console_server.cpp


注:本文中的TCPSocket::write方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。