// INET で DGRAM な UDPソケットによる通信プログラムの雛型 (サーバー側) #include #include #include #include #include "integnet.h" // 接続のポート #define PORT 12345 int main( void ) { // INET で DGRAM なソケットの作成 int s = socket( AF_INET, SOCK_DGRAM, 0 ); // 接続の目印の指定 sockaddr_in addr = SOCKADDR_IN_INIT( AF_INET, htons(PORT), InAddr(htonl(INADDR_ANY)) ); // 目印の公開 if( bind( s, (sockaddr*)&addr, sizeof(addr) ) < 0 ){ perror("bind"); exit(1); } // データの受信 char msg[64]; int len; len = recvfrom( s, msg, sizeof(msg), 0, NULL, NULL ); msg[len] = '\0'; // データの表示 printf("Server received : %s\n", msg ); // ソケットの廃止 close(s); return 0; }