void ClientTCP() { TcpClient client; Packet data; data.SetByteOrder(CX_PACKET_BIG_ENDIAN); if(!client.InitializeSocket(HOSTNAME, PORT)) { cout << "Unable to connect to host " << HOSTNAME << endl; return; } cout << "Initialized TCP Client Socket\n"; char key = 0; while(key != 27) { if((key = GetChar()) > 0) { data.Clear(); // Clear packet and encode data. data.Write(key); client.Send(data); // Send data (could send character array also if desired /* Alternative way to send: client.Send(&key, 1); // Send buffer/array. */ data.Clear(); if(client.Recv(data, 50, 0)) { cout << *data.Ptr() << endl; } //cout << key; } } }
int main() { // 避免出现僵尸进程 signal(SIGCHLD, SIG_IGN); // 创建TcpListener对象 // 监听来自9090端口的连接 TcpListener *tl = new TcpListener(9090); pid_t ch_id; while(1) { // 新连接到来 TcpClient *tc = tl->Accept(); cout<<"accept new connection form "<<tc->GetRemoteAddress()<<":"<<tc->GetRemotePort()<<endl; if ((ch_id = fork()) == 0) { delete tl; tl = NULL; char *buf; int bytes; buf = new char[BUF_SIZE + 1]; try { // 接收数据 // Recv正常返回接收到的字节数 // 当对方关闭连接时返回0,其他情况抛出异常 while( (bytes = tc->Recv(buf, BUF_SIZE, TIMEOUT) )) { buf[bytes] = '\0'; // 打印数据 cout<<bytes<<": "<<buf; } cout << endl; } catch(exception &e) { cout<<"\ncatch exception: "<<e.what()<<endl; } // 删除TcpClient对象 delete tc; delete [] buf; cout<<"lost connection with client."<<endl; exit(0); } delete tc; tc = NULL; } delete tl; return 0; }