(2)windows下的套接字库
WSAStartup() 加载套接字库
WSACleanup() 清除套接字库上述两个函数通常成对使用
typedef struct sockaddr_in { ADDRESS_FAMILY sin_family; USHORT sin_port; IN_ADDR sin_addr; CHAR sin_zero[8]; } SOCKADDR_IN, *PSOCKADDR_IN; typedef struct in_addr { union { struct { UCHAR s_b1; UCHAR s_b2; UCHAR s_b3; UCHAR s_b4; } S_un_b; struct { USHORT s_w1; USHORT s_w2; } S_un_w; ULONG S_addr; } S_un; } IN_ADDR, *PIN_ADDR, *LPIN_ADDR;
完整代码:
#include <winsock2.h> #include <stdio.h> #pragma comment(lib,"ws2_32.lib") /*udp发送*/ void udp_send() { WORD version = MAKEWORD(2, 2); WSADATA wsaData; WSAStartup(version, &wsaData); //打开windows socket资源 SOCKET sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); struct sockaddr_in addr; addr.sin_family = AF_INET; addr.sin_port = htons(8080); addr.sin_addr.S_un.S_addr = inet_addr("192.168.0.130"); sendto(sock, "Hello world!", strlen("Hello world!"), 0, (struct sockaddr*)&addr, sizeof(addr)); closesocket(sock); WSACleanup(); } /*udp接收*/ void udp_recv() { WORD version = MAKEWORD(2, 2); WSADATA wsaData; if (0 != WSAStartup(version, &wsaData)) //打开windows socket资源 { return; } SOCKET sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); struct sockaddr_in addr; memset(&addr, 0, sizeof(addr)); addr.sin_family = AF_INET; addr.sin_port = htons(8080); addr.sin_addr.S_un.S_addr = INADDR_ANY; int rt = bind(sock, (struct sockaddr*)&addr, sizeof(addr)); struct sockaddr_in client; memset(&client, 0, sizeof(client)); char buf[512] = {0}; int len = sizeof(client); rt = recvfrom(sock, buf, 512, 0, (struct sockaddr*)&client, &len); printf("%s\n", buf); closesocket(sock); WSACleanup(); }
用图来表示上述过程:
将上述代码移植到linux下:
//#include <winsock2.h> #include <stdio.h> #include <sys/types.h> /* See NOTES */ #include <sys/socket.h> #include <arpa/inet.h> #include <netinet/in.h> #include <string.h> #include <unistd.h> //#pragma comment(lib,"ws2_32.lib") #define SOCKET int /*udp发送*/ void udp_send() { /* WORD version = MAKEWORD(2, 2); WSADATA wsaData; WSAStartup(version, &wsaData); //打开windows socket资源 */ SOCKET sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); struct sockaddr_in addr; memset(&addr,0,sizeof(addr)); addr.sin_family = AF_INET; addr.sin_port = htons(8080); addr.sin_addr.s_addr = inet_addr("127.0.0.1"); sendto(sock, "Hello world!", strlen("Hello world!"), 0, (struct sockaddr*)&addr, sizeof(addr)); close(sock); // closesocket(sock); //WSACleanup(); } /*udp接收*/ void udp_recv() { /* WORD version = MAKEWORD(2, 2); WSADATA wsaData; if (0 != WSAStartup(version, &wsaData)) //打开windows socket资源 { return; } */ SOCKET sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); struct sockaddr_in addr; memset(&addr, 0, sizeof(addr)); addr.sin_family = AF_INET; addr.sin_port = htons(8080); addr.sin_addr.s_addr = INADDR_ANY; int rt = bind(sock, (struct sockaddr*)&addr, sizeof(addr)); struct sockaddr_in client; memset(&client, 0, sizeof(client)); char buf[512] = {0}; socklen_t len = sizeof(client); rt = recvfrom(sock, buf, 512, 0, (struct sockaddr*)&client, &len); printf("%s\n", buf); close(sock); // closesocket(sock); // WSACleanup(); }