单机上的UDP客户端与服务器端
服务端:
#include <stdio.h> #include <stdlib.h> #include <winsock2.h> #pragma comment(lib, "ws2_32") static SOCKET Udp; int udp_init(char *ip, int port) { struct sockaddr_in ser; //服务器端地址 WSADATA wsaData; if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) { printf("Failed to load Winsock.\n"); //Winsock 初始化错误 return -1; } ser.sin_family = AF_INET; //初始化服务器地址信息 ser.sin_port = htons(port); //端口转换为网络字节序 ser.sin_addr.s_addr = inet_addr(ip); //IP 地址转换为网络字节序 Udp = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); //创建UDP套接字 if (Udp == INVALID_SOCKET) { printf("socket() Failed: %d\n", WSAGetLastError()); return -1; } if (bind(Udp, (LPSOCKADDR)&ser, sizeof(ser)) == SOCKET_ERROR) { printf("绑定IP和端口\n"); return 0; } printf("udp init ok\n"); } int udp_send(char *ip, int port, unsigned char *buff, int len) { struct sockaddr_in s; int ret, addrlen; s.sin_family = AF_INET; s.sin_port = htons(port); s.sin_addr.s_addr = inet_addr(ip); addrlen = sizeof(s); sendto(Udp, buff, len, 0, (SOCKADDR *)&s, addrlen); return 0; } int udp_rcv(char *ip, int *port, unsigned char *buff, int *len) { struct sockaddr_in s; int ret, addrlen; addrlen = sizeof(s); ret = recvfrom(Udp, buff, 1024, 0, (SOCKADDR *)&s, &addrlen); if (ret > 0) { memcpy(ip, inet_ntoa(s.sin_addr), strlen(inet_ntoa(s.sin_addr)));//copy ip to from_ip *port = ntohs(s.sin_port); // printf("rcv ok\n"); } *len = ret; return ret; } int close_udp() { closesocket(Udp); //关闭 socket WSACleanup(); }
客户端:
//#include <stdio.h> //#include <winsock2.h> // //#pragma comment(lib,"ws2_32.lib") // // //int main(void) //{ // WORD sockVision = MAKEWORD(2, 2); // WSADATA wsadata; // // struct sockaddr_in SerAddr; // const char senddata[300] = {0}; // UINT8 revdata[400]; // int ret = -1; // int len = -1; // if (WSAStartup(sockVision, &wsadata) != 0) // { // printf("WSA初始化失败\n"); // return 0; // } // // // //创建套接字 // SOCKET sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); // if (sock == INVALID_SOCKET) // { // printf("socket客户端创建失败\n"); // return 0; // } // //客户端设置服务器地址信息 // SerAddr.sin_family = AF_INET; // SerAddr.sin_port = htons(10060); // SerAddr.sin_addr.S_un.S_addr = inet_addr("192.168.1.53"); // // // // // //获取数据发送给服务器 // struct sockaddr fromAddr; // int addrLen = sizeof(fromAddr); // // while (1) // { // memset(revdata, 0, sizeof(revdata)); // //printf("设备:"); // //scanf("%s", senddata); // //sendto(sock, senddata, strlen(senddata), 0, (SOCKADDR*)&SerAddr, sizeof(SerAddr)); // //if (strcmp(senddata, "bye") == 0) // // break; // //ret = recvfrom(sclient, revdata, sizeof(revdata), 0, (SOCKADDR*)&clientAddr, &len); // // ret = recvfrom(sock, revdata, sizeof(revdata), 0, (SOCKADDR*)&fromAddr, &addrLen); // revdata[sizeof(revdata)-1] = 0; // if (ret>0) // printf("服务器返回数据:%s\r\n",revdata); // } // closesocket(sock); // WSACleanup(); // system("pause"); // return 0; //} #include <stdio.h> #include <WinSock2.h> #pragma comment(lib, "ws2_32.lib") //加载 ws2_32.dll #define BUF_SIZE 100 int main(){ //初始化DLL WSADATA wsaData; WSAStartup(MAKEWORD(2, 2), &wsaData); //创建套接字 SOCKET sock = socket(PF_INET, SOCK_DGRAM, 0); //服务器地址信息 struct sockaddr_in servAddr; memset(&servAddr, 0, sizeof(servAddr)); //每个字节都用0填充 servAddr.sin_family = PF_INET; servAddr.sin_addr.s_addr = inet_addr("127.0.0.1"); servAddr.sin_port = htons(1234); //不断获取用户输入并发送给服务器,然后接受服务器数据 struct sockaddr fromAddr; int addrLen = sizeof(fromAddr); while(1){ char buffer[BUF_SIZE] = {0}; printf("Input a string: "); gets(buffer); sendto(sock, buffer, strlen(buffer), 0, (struct sockaddr*)&servAddr, sizeof(servAddr)); int strLen = recvfrom(sock, buffer, BUF_SIZE, 0, &fromAddr, &addrLen); buffer[strLen] = 0; printf("Message form server: %s\n", buffer); } closesocket(sock); WSACleanup(); return 0; }