UDP受信プログラム #include <stdio.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> int main() { int sock; struct sockaddr_in addr; char buf[2048]; sock = socket(AF_INET, SOCK_DGRAM, 0); addr.sin_family = AF_INET; addr.sin_port = htons(12345); addr.sin_addr.s_addr = INADDR_ANY; bind(sock, (struct sockaddr *)&addr, sizeof(addr)); memset(buf, 0, sizeof(buf)); recv(sock, buf, sizeof(buf), 0); printf("%s\n", buf); close(sock); return 0; }
UDP送信プログラム #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> int main() { int sock; struct sockaddr_in addr; sock = socket(AF_INET, SOCK_DGRAM, 0); addr.sin_family = AF_INET; addr.sin_port = htons(12345); addr.sin_addr.s_addr = inet_addr("127.0.0.1"); sendto(sock, "HELLO", 5, 0, (struct sockaddr *)&addr, sizeof(addr)); close(sock); return 0; }
/*********************************************************** ** Function : UDP受信テストプログラム(MinGW on Win7) ** File Name: udp_recv_test.c ** Debug : gcc -g udp_recv_test.c -o udp_recv_test -lwsock32 -lws2_32 ** Author : ** create : ** modify : ***********************************************************/ //---------------------------------------------------------- // Includes //---------------------------------------------------------- #include <stdio.h> #include <sys/types.h> #include <winsock2.h> #include <ws2tcpip.h> //#include <sys/socket.h> //#include <netinet/in.h> //#include <arpa/inet.h> //#include <netdb.h> //---------------------------------------------------------- // Defines //---------------------------------------------------------- #define _POSIX_SOURCE 1 /* POSIX comliant source (POSIX)*/ #define BUFFSIZE 2048 #define FALSE 0 #define TRUE 1 //---------------------------------------------------------- // Globals //---------------------------------------------------------- //---------------------------------------------------------- // Prototype //---------------------------------------------------------- int main(int argc, char *argv[]); //---------------------------------------------------------- // Code //---------------------------------------------------------- int main(int argc, char *argv[]) { WSADATA wsaData; SOCKET sock; struct sockaddr_in addr; //int sock; char buf[2048]; char *deststr; unsigned int **addrptr; /******************************************************* ** Initialize ********************************************************/ if (WSAStartup(MAKEWORD(2,0), &wsaData) == SOCKET_ERROR) { printf ("Error initialising WSA.\n"); return -1; } sock = socket(AF_INET, SOCK_DGRAM, 0); if (sock < 0) { perror("socket"); return 1; } /******************************************************* ** Process *******************************************************/ //------------------------------------------------------ // (1) //------------------------------------------------------ addr.sin_family = AF_INET; addr.sin_port = htons(12345); addr.sin_addr.s_addr = INADDR_ANY; //------------------------------------------------------ // (2)bind //------------------------------------------------------ bind(sock, (struct sockaddr *)&addr, sizeof(addr)); //------------------------------------------------------ // (3)recv //------------------------------------------------------ memset(buf, 0, sizeof(buf)); recv(sock, buf, sizeof(buf), 0); printf("%s\n", buf); /******************************************************* ** Terminate *******************************************************/ //close(sock); closesocket(sock); WSACleanup(); return 0; }
/*********************************************************** ** Function : UDP受信テストプログラム(MinGW on Win7) ** File Name: udp_recv_test.c ** Debug : gcc -g udp_recv_test.c -o udp_recv_test -lwsock32 -lws2_32 ** Author : ** create : ** modify : ***********************************************************/ //---------------------------------------------------------- // Includes //---------------------------------------------------------- #include <stdio.h> #include <sys/types.h> #include <winsock2.h> #include <ws2tcpip.h> //#include <sys/socket.h> //#include <netinet/in.h> //#include <arpa/inet.h> //#include <netdb.h> //---------------------------------------------------------- // Defines //---------------------------------------------------------- #define _POSIX_SOURCE 1 /* POSIX comliant source (POSIX)*/ #define BUFFSIZE 2048 #define FALSE 0 #define TRUE 1 //---------------------------------------------------------- // Globals //---------------------------------------------------------- //---------------------------------------------------------- // Prototype //---------------------------------------------------------- int main(int argc, char *argv[]); //---------------------------------------------------------- // Code //---------------------------------------------------------- int main(int argc, char *argv[]) { WSADATA wsaData; SOCKET sock; struct sockaddr_in addr; //int sock; char buf[2048]; char *deststr; unsigned int **addrptr; /******************************************************* ** Initialize ********************************************************/ if (WSAStartup(MAKEWORD(2,0), &wsaData) == SOCKET_ERROR) { printf ("Error initialising WSA.\n"); return -1; } sock = socket(AF_INET, SOCK_DGRAM, 0); if (sock < 0) { perror("socket"); return 1; } /******************************************************* ** Process *******************************************************/ //------------------------------------------------------ // (1) //------------------------------------------------------ addr.sin_family = AF_INET; addr.sin_port = htons(12345); addr.sin_addr.s_addr = INADDR_ANY; //------------------------------------------------------ // (2)bind //------------------------------------------------------ bind(sock, (struct sockaddr *)&addr, sizeof(addr)); //------------------------------------------------------ // (3)recv //------------------------------------------------------ memset(buf, 0, sizeof(buf)); recv(sock, buf, sizeof(buf), 0); printf("%s\n", buf); /******************************************************* ** Terminate *******************************************************/ //close(sock); closesocket(sock); WSACleanup(); return 0; }