下面未处理异常
tcpserver.cpp
#include "WinSock2.h" #include <stdio.h> #pragma comment(lib, "ws2_32.lib") #define PORT 1666 int main() { WORD wVersion = MAKEWORD(2, 2); int err; WSADATA wsaData; err = WSAStartup(wVersion, &wsaData); SOCKET sockSrv = socket(AF_INET, SOCK_STREAM, NULL); sockaddr_in addSrv; addSrv.sin_addr.S_un.S_addr = htonl(INADDR_ANY); //或用inet_addr("192.168.1.1"); addSrv.sin_family = AF_INET; addSrv.sin_port = htons(PORT); bind(sockSrv, (sockaddr FAR *)&addSrv, sizeof(addSrv)); int iResult = listen(sockSrv, SOMAXCONN);//等待连结数, 最大连接请求数 SOMAXCONN printf("server ip:%s ", inet_ntoa(addSrv.sin_addr)); printf("server port:%d ", ntohs(addSrv.sin_port)); printf("start listening... "); while(TRUE) { sockaddr_in addClient; int iLen = sizeof(addClient); SOCKET sockConn = accept(sockSrv, (sockaddr FAR *)&addClient, &iLen); printf("client ip:%s ", inet_ntoa(addClient.sin_addr)); printf("client port:%d ", ntohs(addClient.sin_port)); char sendBuf[100]; sprintf(sendBuf, "Welcome %s to server", inet_ntoa(addClient.sin_addr)); send(sockConn, sendBuf, strlen(sendBuf) + 1, 0); //多写一个字节, 可以多个 char recvBuf[100]; recv(sockConn, recvBuf, 100, 0); //tcp用recv, udp用recvfrom printf("%s ", recvBuf); closesocket(sockConn); } closesocket(sockSrv); WSACleanup(); return 0; }
tcpclient.cpp
#include "WinSock2.h" #include <stdio.h> #pragma comment(lib, "ws2_32.lib") #define PORT 1666 int main() { WORD wVersion = MAKEWORD(2, 2); int err; WSADATA wsaData; err = WSAStartup(wVersion, &wsaData); SOCKET sockClient = socket(AF_INET, SOCK_STREAM, NULL); sockaddr_in addSrv; addSrv.sin_addr.S_un.S_addr = inet_addr("127.0.0.1"); //服务器地址 addSrv.sin_family = AF_INET; addSrv.sin_port = htons(PORT); connect(sockClient, (sockaddr FAR *)&addSrv, sizeof(addSrv)); char recvBuf[100]; recv(sockClient, recvBuf, 100, 0); printf("%s ", recvBuf); send(sockClient, "hello123", strlen("hello123") + 1, 0); closesocket(sockClient); WSACleanup(); system("pause"); return 0; }
udpserver.cpp
#include "WinSock2.h" #include <stdio.h> #pragma comment(lib, "ws2_32.lib") #define PORT 1666 int main() { WORD wVersion = MAKEWORD(2, 2); int err; WSADATA wsaData; err = WSAStartup(wVersion, &wsaData); SOCKET sockSrv = socket(AF_INET, SOCK_DGRAM, NULL); sockaddr_in addSrv; addSrv.sin_addr.S_un.S_addr = htonl(INADDR_ANY); //或用inet_addr("192.168.1.1"); addSrv.sin_family = AF_INET; addSrv.sin_port = htons(PORT); bind(sockSrv, (sockaddr FAR *)&addSrv, sizeof(addSrv)); printf("server ip:%s ", inet_ntoa(addSrv.sin_addr)); printf("server port:%d ", ntohs(addSrv.sin_port)); while(TRUE) { sockaddr_in addClient; int iLen = sizeof(addClient); char recvBuf[100]; recvfrom(sockSrv, recvBuf, 100, 0, (sockaddr*)&addClient, &iLen); printf("client ip:%s ", inet_ntoa(addClient.sin_addr)); printf("client port:%d ", ntohs(addClient.sin_port)); printf("%s", recvBuf); } closesocket(sockSrv); WSACleanup(); return 0; }
udpclient.cpp
#include "WinSock2.h" #include <stdio.h> #pragma comment(lib, "ws2_32.lib") #define PORT 1666 int main() { WORD wVersion = MAKEWORD(2, 2); int err; WSADATA wsaData; err = WSAStartup(wVersion, &wsaData); SOCKET sockClient = socket(AF_INET, SOCK_DGRAM, NULL); sockaddr_in addSrv; addSrv.sin_addr.S_un.S_addr = inet_addr("127.0.0.1"); //或用inet_addr("192.168.1.1"); addSrv.sin_family = AF_INET; addSrv.sin_port = htons(PORT); sendto(sockClient, "hello", strlen("hello") + 1, 0, (SOCKADDR*)&addSrv, sizeof(addSrv)); closesocket(sockClient); WSACleanup(); system("pause"); return 0; }