服务端
#include <stdio.h> #include <Winsock2.h> #pragma comment (lib, "ws2_32") #include <thread> void sendFuction(SOCKET &sockConn) { while (true) { char buffer[100]; char mymsg[100]; gets(mymsg); sprintf_s(buffer,100,"server say:\n%s",mymsg); send(sockConn, buffer, strlen(buffer)+sizeof(char), NULL); } } void recvFunction(SOCKET& sockClient) { while (true) { char recvBuf[50]; recv(sockClient,recvBuf,50,0); printf("%s\n",recvBuf); } } void main() { //1:加载套接字库,创建套接字(WSAStartup()/socket()); WORD wVersionRequested; WSADATA wsaData; int err; wVersionRequested = MAKEWORD( 1, 1 ); err = WSAStartup( wVersionRequested, &wsaData ); if ( err != 0 ) { return; } if ( LOBYTE( wsaData.wVersion ) != 1 || HIBYTE( wsaData.wVersion ) != 1 ) { WSACleanup( ); return; } SOCKET sockSrv=socket(AF_INET,SOCK_STREAM,0); //2:绑定套接字到一个IP地址和一个端口上(bind()); SOCKADDR_IN addrSrv; addrSrv.sin_addr.S_un.S_addr=htonl(INADDR_ANY); addrSrv.sin_family=AF_INET; addrSrv.sin_port=htons(6000); bind(sockSrv,(SOCKADDR*)&addrSrv,sizeof(SOCKADDR)); //3:将套接字设置为监听模式等待连接请求(listen()); listen(sockSrv,5); //4:请求到来后,接受连接请求,返回一个新的对应于此次连接的套接字(accept()); SOCKADDR_IN addrClient; int len=sizeof(SOCKADDR); while(1) { SOCKET sockConn=accept(sockSrv,(SOCKADDR*)&addrClient,&len); char sendBuf[50]; sprintf(sendBuf,"Welcome %s to here!",inet_ntoa(addrClient.sin_addr)); //5:用返回的套接字和客户端进行通信(send()/recv()) send(sockConn,sendBuf,strlen(sendBuf)+1,0); char recvBuf[50]; recv(sockConn,recvBuf,50,0); printf("%s\n",recvBuf); //多线程 std::thread sendThread(sendFuction,sockConn); sendThread.detach(); std::thread recvThread(recvFunction,sockConn); recvThread.detach(); //6:返回,等待另一连接请求; //7:关闭套接字,关闭加载的套接字库(closesocket()/WSACleanup())。 //closesocket(sockConn); } }
#include <stdio.h> #include <Winsock2.h> #pragma comment (lib, "ws2_32") #include <thread> void sendFuction(SOCKET &sockConn) { while (true) { char buffer[100]; char mymsg[100]; gets(mymsg); sprintf_s(buffer,100,"clien say:\n%s",mymsg); send(sockConn, buffer, strlen(buffer)+sizeof(char), NULL); } } void recvFunction(SOCKET& sockClient) { while (true) { char recvBuf[50]; recv(sockClient,recvBuf,50,0); printf("%s\n",recvBuf); } } void main() { //1:加载套接字库,创建套接字(WSAStartup()/socket()); WORD wVersionRequested; WSADATA wsaData; int err; wVersionRequested = MAKEWORD( 1, 1 ); err = WSAStartup( wVersionRequested, &wsaData ); if ( err != 0 ) { return; } if ( LOBYTE( wsaData.wVersion ) != 1 || HIBYTE( wsaData.wVersion ) != 1 ) { WSACleanup( ); return; } SOCKET sockClient=socket(AF_INET,SOCK_STREAM,0); //2:向服务器发出连接请求(connect()); SOCKADDR_IN addrSrv; addrSrv.sin_addr.S_un.S_addr=inet_addr("127.0.0.1"); addrSrv.sin_family=AF_INET; addrSrv.sin_port=htons(6000); connect(sockClient,(SOCKADDR*)&addrSrv,sizeof(SOCKADDR)); //3:和服务器端进行通信(send()/recv()); send(sockClient,"hello",strlen("hello")+1,0); char recvBuf[50]; recv(sockClient,recvBuf,50,0); printf("%s\n",recvBuf); std::thread sendThread(sendFuction,sockClient); sendThread.detach(); std::thread recvThread(recvFunction,sockClient); recvThread.detach(); //4:关闭套接字,关闭加载的套接字库(closesocket()/WSACleanup())。 //closesocket(sockClient); //WSACleanup(); while (true) { } }