windows socket程式設計入門示例1
阿新 • • 發佈:2019-02-15
/// tcpSocketClient.cpp #include <stdio.h> #include <winsock2.h> #pragma comment(lib,"ws2_32.lib") #define IP_ADDRESS "127.0.0.1" #define PORT 5678 int main() { int retCode; WORD sockVersion = MAKEWORD(2, 2); WSADATA data; if (WSAStartup(sockVersion, &data) != 0) { fprintf(stderr, "Load WSADTATA failed!\n"); return 0; } SOCKET sclient = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (sclient == INVALID_SOCKET) { fprintf(stderr, "invalid socket !\n"); return 0; } sockaddr_in serAddr; serAddr.sin_family = AF_INET; serAddr.sin_port = htons(PORT); serAddr.sin_addr.S_un.S_addr = inet_addr(IP_ADDRESS); if (connect(sclient, (sockaddr *)&serAddr, sizeof(serAddr)) == SOCKET_ERROR) { printf("connect error !\n"); closesocket(sclient); return 0; } char * sendData = "你好,TCP服務端,我是客戶端!\n"; printf("Send data to server...\n"); retCode = send(sclient, sendData, strlen(sendData), 0); if (retCode == SOCKET_ERROR && WSAGetLastError() == WSAECONNRESET) { fprintf(stderr, "Server has disconneted, send data to server failed!\n"); } else if (retCode == SOCKET_ERROR) { printf("Send data to server failed!\n"); } if (retCode != SOCKET_ERROR) printf("Send data to server finished.\n"); char recData[255]; int ret = recv(sclient, recData, 255, 0); if (ret > 0) { recData[ret] = 0x00; fprintf(stdout, "recivce data from server success!\n"); fprintf(stdout, "Recive data:\n"); printf(recData); } else if (ret == 0 || (ret == SOCKET_ERROR && WSAGetLastError() == WSAECONNRESET)) fprintf(stderr, "Server has disconneted, recive data failed!\n"); else if (ret == SOCKET_ERROR && WSAGetLastError() == WSAEWOULDBLOCK) fprintf(stdout, "非阻塞型別返回\n"); closesocket(sclient); WSACleanup(); return 0; }
/// tcpSocketServer.cpp #include <stdio.h> #include <winsock2.h> #pragma comment(lib,"ws2_32.lib") #define IP_ADDRESS "127.0.0.1" #define PORT 5678 int mian() { //初始化WSA WORD sockVersion = MAKEWORD(2, 2); WSADATA wsaData; if (WSAStartup(sockVersion, &wsaData) != 0) { fprintf(stderr, "Load WSADTATA failed!\n"); return 0; } //建立套接字 SOCKET slisten = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (slisten == INVALID_SOCKET) { printf("socket error !"); return 0; } //繫結IP和埠 sockaddr_in sin; sin.sin_family = AF_INET; sin.sin_port = htons(PORT); //sin.sin_addr.S_un.S_addr = INADDR_ANY; sin.sin_addr.S_un.S_addr = inet_addr(IP_ADDRESS); if (bind(slisten, (LPSOCKADDR)&sin, sizeof(sin)) == SOCKET_ERROR) { closesocket(slisten); printf("bind error !"); return 0; } //開始監聽 if (listen(slisten, 5) == SOCKET_ERROR) { printf("listen error !"); closesocket(slisten); return 0; } //迴圈接收資料 SOCKET sClient; sockaddr_in remoteAddr; int nAddrlen = sizeof(remoteAddr); char revData[255]; while (true) { printf("等待連線...\n"); sClient = accept(slisten, (SOCKADDR *)&remoteAddr, &nAddrlen); if (sClient == INVALID_SOCKET) { printf("accept error !"); continue; } printf("接受到一個連線:%s \r\n", inet_ntoa(remoteAddr.sin_addr)); //closesocket(slisten); //接收資料 int ret = recv(sClient, revData, 255, 0); if (ret > 0) { revData[ret] = 0x00; fprintf(stdout, "recivce data from client success!\n"); fprintf(stdout, "Recive data:\n"); printf(revData); } else if (ret == 0 || (ret == SOCKET_ERROR && WSAGetLastError() == WSAECONNRESET)) fprintf(stderr, "Client has disconneted, recive data failed!\n"); else if (ret == SOCKET_ERROR && WSAGetLastError() == WSAEWOULDBLOCK) fprintf(stdout, "非阻塞型別返回\n"); //傳送資料 char * sendData = "你好,TCP客戶端!\n"; ret = send(sClient, sendData, strlen(sendData), 0); if (ret == SOCKET_ERROR && WSAGetLastError() == WSAECONNRESET) { fprintf(stderr, "Client has disconneted, send data to client failed!\n"); } else if (ret == SOCKET_ERROR) { printf("Send data to server failed!\n"); } if (ret != SOCKET_ERROR) printf("Send data to server finished.\n"); closesocket(sClient); } closesocket(slisten); WSACleanup(); return 0; }