Advertisement
vvccs

TCP

May 31st, 2023 (edited)
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.44 KB | None | 0 0
  1. // SERVER SIDE :
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <unistd.h>
  6. #include <arpa/inet.h>
  7.  
  8. int main(){
  9.  
  10.   char *ip = "127.0.0.1";
  11.   int port = 5566;
  12.  
  13.   int server_sock, client_sock;
  14.   struct sockaddr_in server_addr, client_addr;
  15.   socklen_t addr_size;
  16.   char buffer[1024];
  17.   int n;
  18.  
  19.   server_sock = socket(AF_INET, SOCK_STREAM, 0);
  20.   if (server_sock < 0){
  21.     perror("Socket error");
  22.     exit(1);
  23.   }
  24.   printf("TCP server socket created.\n");
  25.  
  26.   memset(&server_addr, '\0', sizeof(server_addr));
  27.   server_addr.sin_family = AF_INET;
  28.   server_addr.sin_port = port;
  29.   server_addr.sin_addr.s_addr = inet_addr(ip);
  30.  
  31.   n = bind(server_sock, (struct sockaddr*)&server_addr, sizeof(server_addr));
  32.   if (n < 0){
  33.     perror("Bind error");
  34.     exit(1);
  35.   }
  36.   printf("Bind to the port number: %d\n", port);
  37.  
  38.   listen(server_sock, 5);
  39.   printf("Listening...\n");
  40.   while(1){
  41.     addr_size = sizeof(client_addr);
  42.     client_sock = accept(server_sock, (struct sockaddr*)&client_addr, &addr_size);
  43.     printf("Client connected.\n");
  44.  
  45.     bzero(buffer, 1024);
  46.     recv(client_sock, buffer, sizeof(buffer), 0);
  47.     printf("Client: %s\n", buffer);
  48.  
  49.     bzero(buffer, 1024);
  50.     printf("Enter the Response.\n");
  51.     gets(buffer);
  52.     printf("Server: %s\n", buffer);
  53.     send(client_sock, buffer, strlen(buffer), 0);
  54.    
  55.     close(client_sock);
  56.     printf("Client disconnected.\n\n");
  57.   }
  58.   return 0;
  59. }
  60.  
  61. // CLIENT SIDE:
  62. #include <stdio.h>
  63. #include <stdlib.h>
  64. #include <string.h>
  65. #include <unistd.h>
  66. #include <arpa/inet.h>
  67.  
  68. int main(){
  69.  
  70.   char *ip = "127.0.0.1";
  71.   int port = 5566;
  72.  
  73.   int sock;
  74.   struct sockaddr_in addr;
  75.   socklen_t addr_size;
  76.   char buffer[1024];
  77.   int n;
  78.  
  79.   sock = socket(AF_INET, SOCK_STREAM, 0);
  80.   if (sock < 0){
  81.     perror("Socket error");
  82.     exit(1);
  83.   }
  84.   printf("TCP server socket created.\n");
  85.  
  86.   memset(&addr, '\0', sizeof(addr));
  87.   addr.sin_family = AF_INET;
  88.   addr.sin_port = port;
  89.   addr.sin_addr.s_addr = inet_addr(ip);
  90.  
  91.   connect(sock, (struct sockaddr*)&addr, sizeof(addr));
  92.   printf("Connected to the server.\n");
  93.   printf("Enter the message to send.\n");
  94.   bzero(buffer, 1024);
  95.   gets(buffer);
  96.   printf("Client: %s\n", buffer);
  97.   send(sock, buffer, strlen(buffer), 0);
  98.  
  99.   bzero(buffer, 1024);
  100.   recv(sock, buffer, sizeof(buffer), 0);
  101.   printf("Server: %s\n", buffer);
  102.  
  103.   close(sock);
  104.   printf("Disconnected from the server.\n");
  105.  
  106.   return 0;
  107.  
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement