fork download
  1. /*
  2. Program: minser.c
  3. Author: Dr Beco, 2011-04-03
  4. Objective:
  5.   show a minimum server program that can
  6.   create a socket, accept a client, read a byte, write a byte, disconnect
  7. */
  8.  
  9. #include <stdio.h>
  10. #include <unistd.h>
  11. #include <netinet/in.h>
  12. #include <sys/un.h>
  13. #include <string.h>
  14. #include <unistd.h>
  15. #include <arpa/inet.h>
  16.  
  17. #define BUFFER 200
  18.  
  19. int main(void)
  20. {
  21. int port = 8234;
  22. char data[BUFFER];
  23. struct sockaddr_in dir;
  24. struct sockaddr client;
  25. socklen_t long_client;
  26. int id, idReuse=1, son, aux;
  27.  
  28. memset(&dir,0,sizeof(dir));
  29. dir.sin_port = port;
  30. dir.sin_family = AF_INET;
  31. //dir.sin_addr.s_addr = INADDR_ANY;
  32. dir.sin_addr.s_addr = inet_addr("192.168.0.201");
  33.  
  34. id = socket(AF_INET, SOCK_STREAM, 0);
  35. if (id == -1) {
  36. return -1;
  37. }
  38.  
  39. if(setsockopt(id,SOL_SOCKET,SO_REUSEADDR,&idReuse,sizeof(idReuse)) == -1) {
  40. return -1;
  41. }
  42.  
  43. if(bind(id, (struct sockaddr *)&dir, sizeof(dir)) == -1)
  44. {
  45. close (id);
  46. return -1;
  47. }
  48.  
  49. if (listen(id , 1) == -1)
  50. {
  51. printf("Failed to listen on the socket\n");
  52. close(id);
  53. return -1;
  54. }
  55.  
  56. long_client = sizeof (client);
  57. son = accept(id, &client, &long_client);
  58. aux = read(son, data , 1);
  59. //aux = send(son, "S", 1, MSG_NOSIGNAL);
  60. aux = send(son, "S", 1, SO_NOSIGPIPE); // for macOS
  61. printf("done!\n");
  62.  
  63. return 0;
  64. }
  65.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.c: In function ‘main’:
prog.c:59:29: error: ‘SO_NOSIGPIPE’ undeclared (first use in this function); did you mean ‘SO_OOBINLINE’?
     aux = send(son, "S", 1, SO_NOSIGPIPE);
                             ^~~~~~~~~~~~
                             SO_OOBINLINE
prog.c:59:29: note: each undeclared identifier is reported only once for each function it appears in
prog.c:26:29: warning: variable ‘aux’ set but not used [-Wunused-but-set-variable]
     int id, idReuse=1, son, aux;
                             ^~~
stdout
Standard output is empty