zero_shubham1

tcp-server.c

Jan 20th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.28 KB | None | 0 0
  1. #include <netdb.h>
  2. #include <stdio.h>
  3. #include <unistd.h>
  4. #include <netinet/in.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <sys/socket.h>
  8. #include <sys/types.h>
  9. #define MAX 80
  10. #define PORT 8080
  11. #define SA struct sockaddr
  12.  
  13. // Function designed for chat between client and server.
  14. void func(int sockfd)
  15. {
  16.     char buff[MAX];
  17.     int n;
  18.     // infinite loop for chat
  19.     for (;;) {
  20.         bzero(buff, MAX);
  21.  
  22.         // read the message from client and copy it in buffer
  23.         read(sockfd, buff, sizeof(buff));
  24.         // print buffer which contains the client contents
  25.         printf("From client: %s\t To client : ", buff);
  26.         bzero(buff, MAX);
  27.         n = 0;
  28.         // copy server message in the buffer
  29.         while ((buff[n++] = getchar()) != '\n')
  30.             ;
  31.  
  32.         // and send that buffer to client
  33.         write(sockfd, buff, sizeof(buff));
  34.  
  35.         // if msg contains "Exit" then server exit and chat ended.
  36.         if (strncmp("exit", buff, 4) == 0) {
  37.             printf("Server Exit...\n");
  38.             break;
  39.         }
  40.     }
  41. }
  42.  
  43. // Driver function
  44. int main()
  45. {
  46.     int sockfd, connfd, len;
  47.     struct sockaddr_in servaddr, cli;
  48.  
  49.     // socket create and verification
  50.     sockfd = socket(AF_INET, SOCK_STREAM, 0);
  51.     if (sockfd == -1) {
  52.         printf("socket creation failed...\n");
  53.         exit(0);
  54.     }
  55.     else
  56.         printf("Socket successfully created..\n");
  57.     bzero(&servaddr, sizeof(servaddr));
  58.  
  59.     // assign IP, PORT
  60.     servaddr.sin_family = AF_INET;
  61.     servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
  62.     servaddr.sin_port = htons(PORT);
  63.  
  64.     // Binding newly created socket to given IP and verification
  65.     if ((bind(sockfd, (SA*)&servaddr, sizeof(servaddr))) != 0) {
  66.         printf("socket bind failed...\n");
  67.         exit(0);
  68.     }
  69.     else
  70.         printf("Socket successfully binded..\n");
  71.  
  72.     // Now server is ready to listen and verification
  73.     if ((listen(sockfd, 5)) != 0) {
  74.         printf("Listen failed...\n");
  75.         exit(0);
  76.     }
  77.     else
  78.         printf("Server listening..\n");
  79.     len = sizeof(cli);
  80.  
  81.     // Accept the data packet from client and verification
  82.     connfd = accept(sockfd, (SA*)&cli, &len);
  83.     if (connfd < 0) {
  84.         printf("server acccept failed...\n");
  85.         exit(0);
  86.     }
  87.     else
  88.         printf("server acccept the client...\n");
  89.  
  90.     // Function for chatting between client and server
  91.     func(connfd);
  92.  
  93.     // After chatting close the socket
  94.     close(sockfd);
  95. }
Add Comment
Please, Sign In to add comment