控制台下实现多线程socket通信,服务端为每个请求的客户端创建一个线程,进行通信。其实这些MSDN里都有。
一.基本流程如下:
定义变量->获得winsock版本->加载winsock库->初始化->创建套接字->设置套接字选项->关闭套接字->卸载winsock库->释放资源
2 建立一个SOCKET
3 绑定SOCKET
4 在指定的端口监听
5 接受一个连接
6 发送和接受数据
7 断开连接
三.注意winsock下编程有用到两个库
Winsock2.h
一.基本流程如下:
定义变量->获得winsock版本->加载winsock库->初始化->创建套接字->设置套接字选项->关闭套接字->卸载winsock库->释放资源
二.客户端 建立过程(上篇是客户端)
服务器
1 初始化WSA2 建立一个SOCKET
3 绑定SOCKET
4 在指定的端口监听
5 接受一个连接
6 发送和接受数据
7 断开连接
三.注意winsock下编程有用到两个库
Winsock2.h
ws2_32.lib
- #include <Winsock2.h>
- #include <stdio.h>
- #include <process.h>
- #pragma comment(lib,"ws2_32.lib")
- unsigned __stdcall ExecuteThread(void* lparam)
- {
- SOCKET ClientSocket=(SOCKET)(void*)lparam;
- char sendText[]="make a connection!\r\n";
- send(ClientSocket, sendText, strlen(sendText)+1, 0 );
- char recvText[100] = {0};
- if (recv(ClientSocket, recvText, 100, 0) != SOCKET_ERROR)
- {
- printf("receive text: %s\n", recvText);
- }
- _endthreadex(0);
- return 0;
- }
- int main(int argc, char* argv[])
- {
- // (1)加载套接字库
- WORD wVersionRequested; //WinSock库的版本号
- WSADATA wsaData;
- int iResult = 0;
- wVersionRequested = MAKEWORD(1, 1);
- iResult = WSAStartup(wVersionRequested, &wsaData);
- if( iResult != NO_ERROR)
- {
- printf("WSAStartup() failed with error: %d\n", iResult);
- return -1 ;
- }
- if( LOBYTE( wsaData.wVersion) != 1 || HIBYTE( wsaData.wVersion) !=1 )
- {
- WSACleanup();
- return -1;
- }
- // (2)创建用于监听的套接字
- SOCKET sockSrv = socket(AF_INET, SOCK_STREAM, 0);
- if (sockSrv == INVALID_SOCKET)
- {
- printf("socket function failed with error: %ld\n", WSAGetLastError());
- WSACleanup();
- return -1;
- }
- SOCKADDR_IN addrSrv;
- addrSrv.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
- addrSrv.sin_family = AF_INET;
- addrSrv.sin_port = htons(51555);
- // (3)绑定套接字
- iResult = bind(sockSrv, (SOCKADDR*)&addrSrv, sizeof(SOCKADDR));
- if (iResult == SOCKET_ERROR)
- {
- printf("bind function failed with error %d\n", WSAGetLastError());
- iResult = closesocket(sockSrv);
- if (iResult == SOCKET_ERROR)
- {
- printf("closesocket function failed with error %d\n", WSAGetLastError());
- }
- WSACleanup();
- return -1;
- }
- // (4)将套接字设为监听模式,准备接受客户请求
- iResult = listen(sockSrv, SOMAXCONN);
- if (iResult == SOCKET_ERROR)
- {
- printf("listen function failed with error: %d\n", WSAGetLastError());
- }
- printf("Listening on socket...\n");
- // (5)创建线程等待客户请求
- SOCKADDR_IN addrClient;
- int len = sizeof(SOCKADDR);
- SOCKET sockConn;
- unsigned dwThreadId;
- HANDLE hThread;
- while(1)
- {
- //等待客户请求
- sockConn = accept(sockSrv, (SOCKADDR*)&addrClient, &len);
- if (sockConn == INVALID_SOCKET )
- {
- printf("accept function failed with error: %d\n", WSAGetLastError());
- }
- // _beginthreadx比createthread更安全
- hThread = (HANDLE)_beginthreadex(NULL, 0, &ExecuteThread, (void*)sockConn, 0, &dwThreadId );
- WaitForSingleObject(hThread, INFINITE );
- }
- // (6)结束处理
- CloseHandle(hThread);
- iResult = closesocket(sockConn);
- if (iResult == SOCKET_ERROR)
- {
- printf("closesocket function failed with error %d\n", WSAGetLastError());
- WSACleanup();
- return -1;
- }
- WSACleanup();
- return 0;
- }