TCP_server:
#include<windows.h> #pragma comment (lib, "ws2_32")//显式连接套接字库 int main(int argc, char *argv) { WSADATA wsaData; WSAStartup(MAKEWORD(2,2),&wsaData);//初始化套接字库 SOCKET s=socket(PF_INET,SOCK_STREAM,IPPROTO_TCP); sockaddr_in socketaddr; socketaddr.sin_family = PF_INET; socketaddr.sin_addr.S_un.S_addr = inet_addr("127.1.1.0"); socketaddr.sin_port = htons(827); bind(s,(SOCKADDR*)&socketaddr,sizeof(SOCKADDR)); listen(s,1); SOCKADDR clientAddr; int nSize = sizeof(SOCKADDR); SOCKET clientSock; clientSock = accept(s,(SOCKADDR*)&clientAddr,&nSize); send(clientSock,"hello client \r\n",strlen("hello client \r\n")+sizeof(char),NULL); closesocket(clientSock); closesocket(s); WSACleanup();//释放套接字库 return 0; }
TCP_client:
#include<stdio.h> #include<winsock.h> #pragma comment (lib,"ws2_32")//显示连接套接字库 int main() { WSADATA wsaData; WSAStartup(MAKEWORD(2,2),&wsaData);//初始化套接字库 SOCKET s = socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);//创建TCP套接字 sockaddr_in socketAddr; socketAddr.sin_family = PF_INET; socketAddr.sin_addr.S_un.S_addr = inet_addr("127.1.1.0"); socketAddr.sin_port = htons(827); connect(s,(SOCKADDR*)&socketAddr,sizeof(SOCKADDR)); char szBuffer[MAXBYTE] = {0}; recv(s,szBuffer,MAXBYTE,NULL); printf("szBuffer = %s \r\n",szBuffer); closesocket(s); WSACleanup();//释放套接字库 system("pause"); return 0; }
UDP_server:
#include <winsock2.h> #include <stdio.h> #pragma comment(lib, "ws2_32.lib") int main() { WSADATA wsData; SOCKET server; SOCKADDR_IN sockAddr; int sClitAddLen; SOCKET sClient; SOCKADDR_IN sClientAddr; int tmp; char buffer[512]; WSAStartup(MAKEWORD(2,2), &wsData); //初始化winsock server = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); //创建winsock /*配置winsock*/ sockAddr.sin_family = AF_INET; sockAddr.sin_addr.s_addr = htonl(INADDR_ANY); sockAddr.sin_port = htons(5503); memset(&(sockAddr.sin_zero), 0, sizeof(sockAddr.sin_zero)); /*将winsock绑定到端口*/ if(bind(server, (SOCKADDR *)&sockAddr, sizeof(SOCKADDR)) != 0) { printf("bind is failure\n"); return 0; } listen(server, 10); /*在该端口监听*/ while(1) { memset(buffer, 0, sizeof(buffer)); sClitAddLen = sizeof(SOCKADDR); /*响应客户端服务*/ sClient = accept(server, (SOCKADDR *)&sClientAddr, &sClitAddLen); if(sClient == INVALID_SOCKET) { printf("accept is failure\n"); } else { printf("Server is connect with a Client\n"); if(recv(sClient, buffer, 512, 0) == SOCKET_ERROR) { printf("recv failed!\n"); } else { printf("%s\n", buffer); } } closesocket(sClient); } closesocket(server); WSACleanup(); return 0; }
UDP_client:
#define _CRT_SECURE_NO_DEPRECATE //屏蔽scanf的警告 #include <winsock2.h> #include <stdio.h> #pragma comment(lib, "ws2_32.lib") int main() { int tmp; WSADATA wsData; SOCKET client; SOCKADDR_IN sockAddr; char buffer[512]; WSAStartup(MAKEWORD(2,2), &wsData); //初始化winsock client = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); //创建winsock /*配置winsock*/ sockAddr.sin_family = AF_INET; sockAddr.sin_addr.s_addr = inet_addr("127.0.0.1"); sockAddr.sin_port = htons(5503); memset(&(sockAddr.sin_zero), 0, sizeof(sockAddr.sin_zero)); /*连接到服务端*/ if(connect(client, (SOCKADDR *)&sockAddr, sizeof(sockAddr)) == SOCKET_ERROR) { printf("connet is failure \n"); return 0; } else { printf("connet with the Server \n"); } scanf("%s",buffer,255); send(client, buffer, (int)strlen(buffer), 0); /*向服务端发送数据*/ /*关闭socket并释放其占用的资源*/ closesocket(client); WSACleanup(); return 0; }