Advertisement
metapy

c++ network error

Mar 27th, 2017
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.91 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <unistd.h>
  5. #include <arpa/inet.h>
  6. #include <sys/types.h>
  7. #include <netinet/in.h>
  8. #include <sys/socket.h>
  9.  
  10. #define PORTNUM 2300
  11.  
  12. int main(int argc, char *argv[])
  13. {
  14. char* msg = "Hello World !\n";
  15.  
  16. struct sockaddr_in dest; /* socket info about the machine connecting to us */
  17. struct sockaddr_in serv; /* socket info about our server */
  18. int mysocket; /* socket used to listen for incoming connections */
  19. socklen_t socksize = sizeof(struct sockaddr_in);
  20.  
  21. memset(&serv, 0, sizeof(serv)); /* zero the struct before filling the fields */
  22. serv.sin_family = AF_INET; /* set the type of connection to TCP/IP */
  23. serv.sin_addr.s_addr = htonl(INADDR_ANY); /* set our address to any interface */
  24. serv.sin_port = htons(PORTNUM); /* set the server port number */
  25.  
  26. mysocket = socket(AF_INET, SOCK_STREAM, 0);
  27.  
  28. /* bind serv information to mysocket */
  29. bind(mysocket, (struct sockaddr *)&serv, sizeof(struct sockaddr));
  30.  
  31. /* start listening, allowing a queue of up to 1 pending connection */
  32. listen(mysocket, 1);
  33. int consocket = accept(mysocket, (struct sockaddr *)&dest, &socksize);
  34.  
  35. while(consocket)
  36. {
  37. printf("Incoming connection from %s - sending welcome\n", inet_ntoa(dest.sin_addr));
  38. send(consocket, msg, strlen(msg), 0);
  39. close(consocket);
  40. consocket = accept(mysocket, (struct sockaddr *)&dest, &socksize);
  41. }
  42.  
  43. close(mysocket);
  44. return EXIT_SUCCESS;
  45. }
  46. ***************************************************************
  47. error::
  48.  
  49.  
  50. root@kali:~# g++ ex1.cpp
  51. ex1.cpp: In function β€˜int main(int, char**)’:
  52. ex1.cpp:14:17: warning: ISO C++ forbids converting a string constant to β€˜char*’ [-Wwrite-strings]
  53. char* msg = "Hello World !\n";
  54. ^~~~~~~~~~~~~~~~~
  55. root@kali:~#
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement