服务端: // WSASever.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include <WinSock2.h> #include <Windows.h> #pragma comment (lib,"wSock32.lib") int _tmain(int argc, _TCHAR* argv[]) { WORD wVersionRequested; WSADATA wsaData; int err; wVersionRequested = MAKEWORD(2, 2); err = WSAStartup(wVersionRequested, &wsaData); if (err != 0) { printf("WSAStartup failed with error: %d\n", err); return 1; } if (LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2) { printf("Could not find a usable version of Winsock.dll\n"); WSACleanup(); return 1; } else printf("The Winsock 2.2 dll was found okay\n"); //程序开始 //创建socket->bing-》listen->accept->recv->send->closesocket SOCKET severSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (severSocket == INVALID_SOCKET) { printf("new socket error!"); } SOCKADDR_IN sockAddr; sockAddr.sin_port = htons(10086); sockAddr.sin_family = AF_INET; //IP地址表示方法 /*方法1: m_addr.sin_addr.S_un.S_un_b.s_b1 = 192; m_addr.sin_addr.S_un.S_un_b.s_b2 = 168; m_addr.sin_addr.S_un.S_un_b.s_b3 = 0; m_addr.sin_addr.S_un.S_un_b.s_b4 = 1; 方法2: m_addr.sin_addr.S_un.S_un_w.s_w1 = (168 << 8) | 192; m_addr.sin_addr.S_un.S_un_w.s_w2 = (1 << 8) | 0; 方法3: m_addr.sin_addr.S_un.S_addr = (1 << 24) | (0 << 16) | (168 << 8) | 192 方法4; service.sin_addr.s_addr = inet_addr("127.0.0.1"); */ //sockAddr.sin_addr.s_addr = inet_addr(INADDR_ANY); sockAddr.sin_addr.S_un.S_un_b.s_b1 = 127; sockAddr.sin_addr.S_un.S_un_b.s_b2 = 0; sockAddr.sin_addr.S_un.S_un_b.s_b3 = 0; sockAddr.sin_addr.S_un.S_un_b.s_b4 = 1; if (bind(severSocket, (sockaddr*)&sockAddr, sizeof(SOCKADDR_IN)) == SOCKET_ERROR) { printf("bind error! %d\n", WSAGetLastError()); } //监听1个 if (listen(severSocket, 5) == SOCKET_ERROR) { printf("listen error!%d\n", WSAGetLastError()); } //创建一个一客户端连接的socket SOCKET sockLink = accept(severSocket, NULL, NULL); if (sockLink != INVALID_SOCKET) { printf("communication sucess!\n"); } int nErr=0; while (true) { char pSeverBuff[MAXBYTE] = { 0 }; nErr = recv(sockLink, pSeverBuff, MAXBYTE, 0); printf("%s(%d)%s\n", inet_ntoa(sockAddr.sin_addr), sockAddr.sin_port, pSeverBuff); send(sockLink, pSeverBuff, MAXBYTE, 0); //当客户端关闭时,服务端也随之关闭 if (nErr == INVALID_SOCKET) break; } closesocket(severSocket); closesocket(sockLink); WSACleanup(); return 0; }
客户端 // WASClient.cpp : 定义控制台应用程序的入口点。 // //#include <WinSock2.h>一定要在#include <Windows.h>前面 #include "stdafx.h" #include <WinSock2.h> #include <Windows.h> #pragma comment (lib,"wSock32.lib") int _tmain(int argc, _TCHAR* argv[]) { WORD wVersionRequested; WSADATA wsaData; int err; //版本检测 wVersionRequested = MAKEWORD(2, 2); err = WSAStartup(wVersionRequested, &wsaData); if (err != 0) { printf("WSAStartup failed with error: %d\n", err); return 1; } if (LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2) { printf("Could not find a usable version of Winsock.dll\n"); WSACleanup(); return 1; } else printf("The Winsock 2.2 dll was found okay\n"); //程序开始 //创建socket-》连接connect-》发送send-》接受recv-》释放closesocke SOCKET clientSocket=socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (clientSocket == INVALID_SOCKET) { printf("new socket error!"); } SOCKADDR_IN sockAddr; //一定要把主机字节序换成网络字节序 并是short类型 htons() sockAddr.sin_port = htons(10086); sockAddr.sin_family = AF_INET; //IP地址表示方法 /*方法1: m_addr.sin_addr.S_un.S_un_b.s_b1 = 192; m_addr.sin_addr.S_un.S_un_b.s_b2 = 168; m_addr.sin_addr.S_un.S_un_b.s_b3 = 0; m_addr.sin_addr.S_un.S_un_b.s_b4 = 1; 方法2: m_addr.sin_addr.S_un.S_un_w.s_w1 = (168 << 8) | 192; m_addr.sin_addr.S_un.S_un_w.s_w2 = (1 << 8) | 0; 方法3: m_addr.sin_addr.S_un.S_addr = (1 << 24) | (0 << 16) | (168 << 8) | 192*/ sockAddr.sin_addr.S_un.S_un_b.s_b1 = 127; sockAddr.sin_addr.S_un.S_un_b.s_b2 = 0; sockAddr.sin_addr.S_un.S_un_b.s_b3 = 0; sockAddr.sin_addr.S_un.S_un_b.s_b4 = 1; if (connect(clientSocket, (sockaddr*)&sockAddr, sizeof(SOCKADDR_IN)) != SOCKET_ERROR) { printf("communication sucess!\n"); } char pClientBuf[MAXBYTE] = {0}; //请求连接 while (TRUE) { gets_s(pClientBuf); send(clientSocket, pClientBuf, MAXBYTE, 0); recv(clientSocket, pClientBuf, MAXBYTE, 0); printf("return from sever:%s\n", pClientBuf); } WSACleanup(); closesocket(clientSocket); return 0; }