Advertisement
ringneckparrot

TCP Message Server

Apr 9th, 2012
403
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.75 KB | None | 0 0
  1. /****************************************************************************
  2.     ringneckparrot (c)
  3.     License: http://creativecommons.org/licenses/by-nc-sa/3.0/
  4.    
  5.     Contact Me:
  6.     Email: ringneckparrot@hotmail.com
  7.     Facebook: http://www.facebook.com/ringneckparrot
  8.     Twitter ID: pp4rr0t
  9.     SecurityTube: http://www.securitytube.net/user/ringneckparrot
  10.  
  11. ****************************************************************************/
  12.  
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <netinet/in.h>
  16. #include <sys/types.h>
  17. #include <sys/socket.h>
  18. #include <string.h>
  19. #include <strings.h>
  20. #include <errno.h>
  21.  
  22. main()
  23. {
  24.     int sock;
  25.     int bindsock;
  26.     int listensock;
  27.     int acceptsock;
  28.     int sendsock;
  29.     int len;
  30.     struct sockaddr_in server;
  31.     struct sockaddr_in accept_structure;
  32.     char *mesg = "Welcome !\n";
  33.  
  34.     len = sizeof(struct sockaddr_in);
  35.    
  36.     sock = socket(AF_INET, SOCK_STREAM, 0);
  37.     if (sock == -1)
  38.     {
  39.         perror("Socket Error: ");
  40.         exit(-1);
  41.     }
  42.  
  43.     bzero(&server, len);
  44.     server.sin_family = AF_INET;
  45.     server.sin_port = htons(1234);
  46.     server.sin_addr.s_addr = INADDR_ANY;
  47.  
  48.     bindsock = bind(sock, (struct sockaddr *)&server, len);
  49.     if (bindsock == -1)
  50.     {
  51.         perror("Bind Error: ");
  52.         exit(-1);
  53.     }
  54.  
  55.     listensock = listen(sock, 4);
  56.     if (listensock == -1)
  57.     {
  58.         perror("Listen Error: ");
  59.         exit(-1);
  60.     }
  61.  
  62.     while(1)
  63.     {
  64.         acceptsock = accept(sock, (struct sockaddr *)&accept_structure, &len);
  65.         if (acceptsock == -1)
  66.         {
  67.             perror("Accept Error: ");
  68.             exit(-1);
  69.  
  70.         }
  71.    
  72.         sendsock = send(acceptsock, mesg, strlen(mesg), 0);
  73.         if (sendsock == -1)
  74.         {
  75.             perror("Send Error: ");
  76.             exit(-1);
  77.  
  78.         }  
  79.  
  80.         printf("IP of the client: %s\n", (char *)inet_ntoa(accept_structure.sin_addr));
  81.         printf("Bytes Sent: %d\n", sendsock);
  82.  
  83.         close(acceptsock);
  84.     }
  85.  
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement