1、伺服器端
通訊模型:初始化winsock -> 建立socket -> 綁定(bind) -> 監聽(listen) -> 接收(accept)
/** * main.c * socket server * @date : 2011/11/27 * @author : MJN */#include <stdio.h>#include <winsock2.h>#pragma comment(lib, "ws2_32")int main() { int iResult; WORD wVersionRequested; WSADATA wsaData; SOCKET listenSocket; SOCKET acceptSocket; struct sockaddr_in service; /* Use the MAKEWORD(lowbyte, highbyte) macro declared in Windef.h */ wVersionRequested = MAKEWORD(2, 2); /* Initializing Winsock */ iResult = WSAStartup(wVersionRequested, &wsaData); if (iResult != 0) { printf("WSAStartup failed with error: %d\n", iResult); return -1; } else { printf("WSAStartup succeeded!\n"); } if (wsaData.wVersion != wVersionRequested) { printf("the socket version loaded is: %d.%d, but is not the requested %d.%d", HIBYTE(wsaData.wVersion), LOBYTE(wsaData.wVersion), HIBYTE(wVersionRequested), LOBYTE(wVersionRequested)); WSACleanup(); return -1; } /* create a tcp socket */ listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (listenSocket == INVALID_SOCKET) { printf("create socket falied with error: %d\n", WSAGetLastError()); WSACleanup(); return -1; } else { printf("socket created!\n"); } /* bind the socket */ service.sin_family = AF_INET; service.sin_port = htons(9999); service.sin_addr.s_addr = inet_addr("127.0.0.1"); iResult = bind(listenSocket, (SOCKADDR *) &service, sizeof(service)); if (iResult == SOCKET_ERROR) { printf("bind failed with error:%d\n", WSAGetLastError()); closesocket(listenSocket); WSACleanup(); return -1; } else { printf("bind succeeded!\n"); } /* listen for incoming connection requests */ iResult = listen(listenSocket, SOMAXCONN); if (iResult == SOCKET_ERROR) { printf("listen failed with error:%d\n", WSAGetLastError()); closesocket(listenSocket); WSACleanup(); return -1; } else { printf("listen succeeded!\n"); } printf("listening on socket...\n"); /* accept new incoming connections */ while (1) { acceptSocket = accept(listenSocket, NULL, NULL); if (acceptSocket == INVALID_SOCKET) { printf("accept failed with error: %d", WSAGetLastError()); closesocket(listenSocket); WSACleanup(); return -1; } else { printf("accepted a new socket with id: %d\n", acceptSocket); } } closesocket(listenSocket); WSACleanup(); return 0;
2、用戶端
通訊模型:初始化winsock -> 建立socket -> 串連(connect)
/** * main.c * socket client * @date : 2011/11/27 * @author : MJN */#include <stdio.h>#include <WinSock2.h>#pragma comment(lib, "ws2_32")int main() { int iResult; WORD wVersionRequested; WSADATA wsaData; SOCKET clientSocket; struct sockaddr_in clientService; wVersionRequested = MAKEWORD(2, 2); /* initialize windows socket */ iResult = WSAStartup(wVersionRequested, &wsaData); if (iResult != 0) { printf("WSAStartup failed with error: %d\n", iResult); return -1; } else { printf("WSAStartup succeeded!\n"); } /* create client tcp socket */ clientSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (clientSocket == INVALID_SOCKET) { printf("create socket failed with error: %d\n", WSAGetLastError()); WSACleanup(); return -1; } else { printf("create socket succeeded!\n"); } /* connect to server */ clientService.sin_family = AF_INET; clientService.sin_port = htons(9999); clientService.sin_addr.s_addr = inet_addr("127.0.0.1"); iResult = connect(clientSocket, (SOCKADDR *) &clientService, sizeof(clientService)); if (iResult == SOCKET_ERROR) { printf("connect failed with error: %d\n", WSAGetLastError()); closesocket(clientSocket); WSACleanup(); return -1; } else { printf("connect succeed!\n"); } closesocket(clientSocket); WSACleanup(); return 0;}
註:
IDE:Microsoft Visual Studio 2010