//服务端(以C++作范例) #include"mysocket.h" //导入头文件:由于mysock头文件本身就有其它文件,这里无需导入 using namespace std; SOCKET s_accept; int main() { cout << "---------------------------服务端---------------------------\n"; cout << "正在启动...\n"; startup(); cout << "启动成功!\n"; SOCKET s = createSocket(PF_INET, SOCK_STREAM); //以IPV6协议创建套接字 //填充信息 SOCKADDR_IN server_addr; server_addr.sin_family = AF_INET;//协议 server_addr.sin_port = htons(8226);//端口,随便填 server_addr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1"); //ip地址,这里填服务端设备上的ip,127.0.0.1是本地ip,不能实现跨设备交互 if (bind(s, (SOCKADDR*)&server_addr, sizeof(SOCKADDR)) == SOCKET_ERROR) //绑定套接字 { cout << "套接字绑定失败!\n"; WSACleanup(); return FALSE; } cout << "套接字绑定成功!" << endl; if (listen(s, 1) == SOCKET_ERROR) { //监听:将服务端转为被动状态,接收请求 cout << "监听失败!\n"; WSACleanup(); return FALSE; } cout << "设置监听状态成功!\n"; sockaddr_in accept_addr; //用来记录请求连接的套接字信息 int len = sizeof(SOCKADDR); cout << "等待客户端发起请求..." << endl; SOCKET t = accept(s, (SOCKADDR*)&accept_addr, &len); //接受连接请求 if (s_accept == SOCKET_ERROR) { cout << "请求错误!\n"; WSACleanup(); return FALSE; } cout << "建立连接成功!\n"; char recv_buf[1145], send_buf[4514]; while(1){ //信息交互部分,可以作改动 myrecv(t, recv_buf); cout << "接收到客户端信息:" << recv_buf << endl; cout << "请输入要发送的信息:"; cin >> send_buf; mysend(t, send_buf); } return 0; }
标签:头文件,int,send,导入,buf,服务端 From: https://www.cnblogs.com/fushuxuan/p/18401362