#include <iostream> #include "bsdsocket/ODSocket.h" #include "pthread/pthread.h" #include "unistd.h" #include "list" using namespace std; int main(int argc, const char * argv[]) { // 编写一个socket长链接服务器端 // 1 创建 Socket ODSocket* sockServer = new ODSocket(); sockServer->Init(); // 0表示使用tcp方式进行通讯 bool res = sockServer->Create(AF_INET, SOCK_STREAM,0); // 2 绑定端口 res = sockServer->Bind(6666); cout<<"绑定端口 "<<(res==1?"成功":"失败")<<endl 3="" res="sockServer-">Listen(); cout<< "侦听端口 "<<(res==1?"成功":"失败")<<endl 4="" char="" message="" 30="" char="" address="" 40="" odsocket="" p_client="new" odsocket="" sockserver-="">Accept(*p_Client,address); cout<<address<<"来了"<<endl 5="" p_client-="">Send("welcome you", 11); // 11表示前面字符串长度 // 6 接受从客户端发送过来的数据 while(1){ // // 如果有客户端连接,创建客户端的socket // ODSocket* p_sockClient = new ODSocket(); // // accept 应该是接受另一个对象,在这里这个对象代表客户端,还有个receive虽然意思差不多,但是是接受信息 // sockServer.Accept(*p_sockClient,address); cout<<address<<"来了"<<endl sprintf="" message="" welcome="" s="" address="" p_sockclient-="">Send(message, sizeof(message)); // // 启动线程接受数据 p_Client->Recv(message, sizeof(message)); cout<<"接收到:"<<message<<endl if="" strcmp="" message="" exit="" 0="" break="" 7="" p_client-="">Close(); sockServer->Close(); return 0; } </endl></endl></endl></endl></endl></iostream>