fork(2) download
  1. /*minitalk サーバプログラム*/
  2. #include <stdio.h>
  3. #include <sys/types.h>
  4. #include <sys/socket.h>
  5. #include <netdb.h>
  6. #include <netinet/in.h>
  7.  
  8.  
  9. int CanIRecv(int fd){
  10. fd_set fdset;
  11. struct timeval timeout;
  12. FD_ZERO( &fdset );
  13. FD_SET( fd , &fdset );
  14. timeout.tv_sec = 0;
  15. timeout.tv_usec = 0;
  16. return select( fd+1 , &fdset , NULL , NULL , &timeout );
  17. }
  18.  
  19.  
  20. int main(int argc, char *argv[])
  21. {
  22. int srv_sock, sock, port;
  23. struct sockaddr_in srv_addr, cli_addr;
  24. int addrlen, sendlen, recvlen;
  25. char buf[256];
  26.  
  27. /* ポート番号を引数から取得 */
  28. port = strtol(argv[1], NULL, 10);
  29.  
  30. /* ソケットの作成 */
  31. srv_sock = socket(AF_INET, SOCK_STREAM, 0);
  32. if (srv_sock < 0) {
  33. perror("cannot create socket");
  34. exit(-1);
  35. }
  36.  
  37. /* サーバとしてバインド */
  38. srv_addr.sin_family = AF_INET;
  39. srv_addr.sin_port = htons(port);
  40. srv_addr.sin_addr.s_addr = INADDR_ANY;
  41. if (bind(srv_sock, (struct sockaddr*)&srv_addr, sizeof(srv_addr)) < 0) {
  42. perror("bind failed");
  43. exit(-1);
  44. }
  45.  
  46. /* 待ち受け人数を1人に設定 */
  47. listen(srv_sock, 1);
  48.  
  49. /* クライアントからの接続を受け付ける */
  50. addrlen = sizeof(cli_addr);
  51. sock = accept(srv_sock, (struct sockaddr*)&cli_addr, &addrlen);
  52. if (sock == -1) {
  53. perror("accept failed");
  54. exit(-1);
  55. }
  56. printf("connected\n");
  57.  
  58. /* これ以上クライアントを受け付けないので、サーバのソケットを閉じる */
  59. close(srv_sock);
  60.  
  61. /* 実際の通信 */
  62. while (feof(stdin) == 0) {
  63.  
  64. if(CanIRecv(0)){
  65. /* 標準入力からキーボード入力された文字を読み込む */
  66. printf("message : ");
  67. fgets(buf, sizeof(buf), stdin);
  68. /* 読み込んだ文字をソケットに書き込む */
  69. sendlen = write(sock, buf, strlen(buf) + 1);
  70. if (sendlen < 0) {
  71. perror("cannot send a message");
  72. }
  73. }
  74.  
  75.  
  76. if (CanIRecv(sock)) {
  77. /* ソケットからのデータを読み込む */
  78. recvlen = read(sock, buf, sizeof(buf));
  79. if (recvlen <= 0) {
  80. /* クライアント側が EOF */
  81. break;
  82. }
  83. printf(">> %s", buf);
  84. }
  85.  
  86.  
  87. }
  88. close(sock);
  89. }
  90.  
  91.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In function ‘int main(int, char**)’:
prog.cpp:28: error: ‘strtol’ was not declared in this scope
prog.cpp:34: error: ‘exit’ was not declared in this scope
prog.cpp:43: error: ‘exit’ was not declared in this scope
prog.cpp:51: error: invalid conversion from ‘int*’ to ‘socklen_t*’
prog.cpp:51: error:   initializing argument 3 of ‘int accept(int, sockaddr*, socklen_t*)’
prog.cpp:54: error: ‘exit’ was not declared in this scope
prog.cpp:59: error: ‘close’ was not declared in this scope
prog.cpp:69: error: ‘strlen’ was not declared in this scope
prog.cpp:69: error: ‘write’ was not declared in this scope
prog.cpp:78: error: ‘read’ was not declared in this scope
prog.cpp:67: warning: ignoring return value of ‘char* fgets(char*, int, FILE*)’, declared with attribute warn_unused_result
stdout
Standard output is empty