Facebook
From TIET, 10 Months ago, written in C.
Embed
Download Paste or View Raw
Hits: 358
  1. // kill -9 $(lsof -ti :8080)
  2.  
  3. // TCP Server
  4. #include <netinet/in.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <sys/socket.h>
  9. #include <unistd.h>
  10. #define PORT 8080
  11.  
  12. int main()
  13. {
  14.     struct sockaddr_in serverAddr, clientAddr;
  15.     int serverAddrLen = sizeof(serverAddr);
  16.     int clientAddrLen = sizeof(clientAddr);
  17.  
  18.     // Create a socket
  19.     int server_fd = socket(AF_INET, SOCK_STREAM, 0);
  20.  
  21.     if (server_fd < 0)
  22.     {
  23.         perror("nCouldn't create socket");
  24.         exit(EXIT_FAILURE);
  25.     }
  26.  
  27.     // Initialize the server address structure
  28.     serverAddr.sin_family = AF_INET;
  29.     serverAddr.sin_addr.s_addr = INADDR_ANY;
  30.     serverAddr.sin_port = htons(PORT);
  31.  
  32.     // Bind the socket to the server address
  33.     if (bind(server_fd, (struct sockaddr *)&serverAddr;, serverAddrLen) < 0)
  34.     {
  35.         perror("nCouldn't bind");
  36.         exit(EXIT_FAILURE);
  37.     }
  38.  
  39.     // Start listening for incoming connections
  40.     if (listen(server_fd, 10) < 0)
  41.     {
  42.         perror("nCouldn't listen");
  43.         exit(EXIT_FAILURE);
  44.     }
  45.  
  46.     // Accept a client connections
  47.     // int comm_fd = accept(server_fd, (struct sockaddr *)&serverAddr;, (socklen_t *)&serverAddrLen;);
  48.     int comm_fd = accept(server_fd, (struct sockaddr *)&clientAddr;, &clientAddrLen;);
  49.  
  50.     if (comm_fd < 0)
  51.     {
  52.         perror("nCouldn't accept");
  53.         exit(EXIT_FAILURE);
  54.     }
  55.  
  56.     char messageReceived[100];
  57.  
  58.     while (1)
  59.     {
  60.         bzero(messageReceived, 100);
  61.        
  62.         // Receive data from the client
  63.         recv(comm_fd, messageReceived, 100, 0);
  64.  
  65.         printf("nMessage from client:n%s", messageReceived);
  66.  
  67.         // Send the received data back to the client
  68.         send(comm_fd, messageReceived, strlen(messageReceived), 0);
  69.  
  70.         if(strncmp(messageReceived, "Exit", 4) == 0) {
  71.             printf("nServer exiting...n");
  72.             break;
  73.         }
  74.     }
  75.     // Close the client connection
  76.     close(comm_fd);
  77.     close(server_fd);
  78. }
  79.  
  80. // TCP Client
  81.  
  82. #include <arpa/inet.h>
  83. #include <stdio.h>
  84. #include <stdlib.h>
  85. #include <string.h>
  86. #include <sys/socket.h>
  87. #include <unistd.h>
  88. #define PORT 8080
  89.  
  90. int main()
  91. {
  92.     struct sockaddr_in serverAddr;
  93.  
  94.     // Create a socket
  95.     int client_fd = socket(AF_INET, SOCK_STREAM, 0);
  96.  
  97.     if (client_fd < 0)
  98.     {
  99.         perror("nCouldn't create socket");
  100.         exit(EXIT_FAILURE);
  101.     }
  102.  
  103.     // Initialize the server address structure
  104.     serverAddr.sin_family = AF_INET;
  105.     serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
  106.     serverAddr.sin_port = htons(PORT);
  107.  
  108.     // if (inet_pton(AF_INET, "127.0.0.1", &serverAddr;.sin_addr) <= 0)
  109.     // {
  110.     //     printf("nInvalid address / Address not supported");
  111.     //     exit(EXIT_FAILURE);
  112.     // }
  113.  
  114.     // Establish a connection to the server
  115.     if (connect(client_fd, (struct sockaddr *)&serverAddr;, sizeof(serverAddr)) < 0)
  116.     {
  117.         perror("nCouldn't connect");
  118.         exit(EXIT_FAILURE);
  119.     }
  120.  
  121.     char messageSent[100];
  122.     char messageReceived[100];
  123.  
  124.     while (1)
  125.     {
  126.         bzero(messageSent, 100);
  127.         bzero(messageReceived, 100);
  128.  
  129.         // Read a line of text from the user
  130.         printf("nEnter message:n");
  131.         fgets(messageSent, 100, stdin);
  132.  
  133.         // Send the user's input to the server
  134.         send(client_fd, messageSent, sizeof(messageSent), 0);
  135.        
  136.         // Receive the response from the server
  137.         recv(client_fd, messageReceived, 100, 0);
  138.  
  139.         // Print the server's response
  140.         printf("nMessage from server(echo):n%s", messageReceived);
  141.  
  142.         if (strncmp(messageSent, "Exit", 4) == 0)
  143.         {
  144.             printf("nClient exiting...n");
  145.             break;
  146.         }
  147.     }
  148.  
  149.     close(client_fd);
  150. }
  151.  
  152. // UDP Server
  153.  
  154. #include <netinet/in.h>
  155. #include <stdio.h>
  156. #include <stdlib.h>
  157. #include <string.h>
  158. #include <sys/socket.h>
  159. #include <unistd.h>
  160. #define PORT 8080
  161.  
  162. int main()
  163. {
  164.     struct sockaddr_in serverAddr, clientAddr;
  165.  
  166.     // Create a socket
  167.     int server_fd = socket(AF_INET, SOCK_DGRAM, 0);
  168.  
  169.     if (server_fd < 0)
  170.     {
  171.         perror("nCouldn't create socket");
  172.         exit(EXIT_FAILURE);
  173.     }
  174.  
  175.     // Initialize the server address structure
  176.     serverAddr.sin_family = AF_INET;
  177.     serverAddr.sin_addr.s_addr = INADDR_ANY;
  178.     serverAddr.sin_port = htons(PORT);
  179.  
  180.     // Bind the socket to the server address
  181.     if (bind(server_fd, (struct sockaddr *)&serverAddr;, sizeof(serverAddr)) < 0)
  182.     {
  183.         perror("nCouldn't bind");
  184.         exit(EXIT_FAILURE);
  185.     }
  186.  
  187.     char messageReceived[100];
  188.     socklen_t clientAddrLen = sizeof(clientAddr);
  189.  
  190.     while (1)
  191.     {
  192.         bzero(messageReceived, 100);
  193.  
  194.         // Receive data from the client
  195.         recvfrom(server_fd, messageReceived, 100, 0, (struct sockaddr *)&clientAddr;, &clientAddrLen;);
  196.  
  197.         printf("nMessage from client:n%s", messageReceived);
  198.  
  199.         // Send the received data back to the client
  200.         sendto(server_fd, messageReceived, strlen(messageReceived), 0, (struct sockaddr *)&clientAddr;, clientAddrLen);
  201.  
  202.         if(strncmp(messageReceived, "Exit", 4) == 0) {
  203.             printf("nServer exiting...n");
  204.             break;
  205.         }
  206.     }
  207.    
  208.     close(server_fd);
  209. }
  210.  
  211. // UDP Client
  212.  
  213. #include <netinet/in.h>
  214. #include <stdio.h>
  215. #include <stdlib.h>
  216. #include <string.h>
  217. #include <sys/socket.h>
  218. #include <unistd.h>
  219. #define PORT 8080
  220.  
  221. int main()
  222. {
  223.     struct sockaddr_in serverAddr;
  224.  
  225.     // Create a socket
  226.     int client_fd = socket(AF_INET, SOCK_DGRAM, 0);
  227.  
  228.     if (client_fd < 0)
  229.     {
  230.         perror("nCouldn't create socket");
  231.         exit(EXIT_FAILURE);
  232.     }
  233.  
  234.     // Initialize the server address structure
  235.     serverAddr.sin_family = AF_INET;
  236.     serverAddr.sin_port = htons(PORT);
  237.     serverAddr.sin_addr.s_addr = INADDR_ANY;
  238.  
  239.     char messageSent[100];
  240.     char messageReceived[100];
  241.     socklen_t serverAddrLen = sizeof(serverAddr);
  242.  
  243.     while (1)
  244.     {
  245.         bzero(messageSent, 100);
  246.         bzero(messageReceived, 100);
  247.  
  248.         // Read a line of text from the user
  249.         printf("nEnter message:n");
  250.         fgets(messageSent, 100, stdin);
  251.  
  252.         // Send the user's input to the server
  253.         sendto(client_fd, messageSent, sizeof(messageSent), 0, (struct sockaddr *)&serverAddr;, serverAddrLen);
  254.  
  255.         // Receive the response from the server
  256.         recvfrom(client_fd, messageReceived, 100, 0, (struct sockaddr *)&serverAddr;, &serverAddrLen;);
  257.  
  258.         // Print the server's response
  259.         printf("nMessage from server(echo):n%s", messageReceived);
  260.  
  261.         if(strncmp(messageSent, "Exit", 4) == 0) {
  262.             printf("nClient exiting...n");
  263.             break;
  264.         }
  265.     }
  266.  
  267.     close(client_fd);
  268. }
  269.  
  270. // Chat Server
  271.  
  272. #include <netinet/in.h>
  273. #include <stdio.h>
  274. #include <stdlib.h>
  275. #include <string.h>
  276. #include <sys/socket.h>
  277. #include <unistd.h>
  278. #define PORT 8080
  279.  
  280. int main()
  281. {
  282.     struct sockaddr_in serverAddr;
  283.     int serverAddrLen = sizeof(serverAddr);
  284.  
  285.     // Create a socket
  286.     int server_fd = socket(AF_INET, SOCK_STREAM, 0);
  287.  
  288.     if (server_fd < 0)
  289.     {
  290.         perror("nCouldn't create socket");
  291.         exit(EXIT_FAILURE);
  292.     }
  293.  
  294.     // Initialize the server address structure
  295.     serverAddr.sin_family = AF_INET;
  296.     serverAddr.sin_addr.s_addr = INADDR_ANY;
  297.     serverAddr.sin_port = htons(PORT);
  298.  
  299.     // Bind the socket to the server address
  300.     if (bind(server_fd, (struct sockaddr *)&serverAddr;, sizeof(serverAddr)) < 0)
  301.     {
  302.         perror("nCouldn't bind");
  303.         exit(EXIT_FAILURE);
  304.     }
  305.  
  306.     // Start listening for incoming connections
  307.     if (listen(server_fd, 10) < 0)
  308.     {
  309.         perror("nCouldn't listen");
  310.         exit(EXIT_FAILURE);
  311.     }
  312.  
  313.     // Accept a client connections
  314.     int comm_fd = accept(server_fd, (struct sockaddr *)&serverAddr;, (socklen_t *)&serverAddrLen;);
  315.  
  316.     if (comm_fd < 0)
  317.     {
  318.         perror("nCouldn't accept");
  319.         exit(EXIT_FAILURE);
  320.     }
  321.  
  322.     while (1)
  323.     {
  324.         char messageReceived[100];
  325.         char messageSent[100];
  326.  
  327.         // Receive data from the client
  328.         recv(comm_fd, messageReceived, 100, 0);
  329.  
  330.         printf("nMessage from client:n%s", messageReceived);
  331.  
  332.         printf("nEnter message:n");
  333.         fgets(messageSent, 100, stdin);
  334.  
  335.         // Send the received data back to the client
  336.         send(comm_fd, messageSent, strlen(messageSent), 0);
  337.  
  338.         if(strncmp(messageSent, "Exit", 4) == 0) {
  339.             printf("nServer exiting...n");
  340.             break;
  341.         }
  342.     }
  343.  
  344.     // Close the client connection
  345.     close(comm_fd);
  346.     close(server_fd);
  347. }
  348.  
  349. // Chat Client
  350.  
  351. #include <arpa/inet.h>
  352. #include <stdio.h>
  353. #include <stdlib.h>
  354. #include <string.h>
  355. #include <sys/socket.h>
  356. #include <unistd.h>
  357. #define PORT 8080
  358.  
  359. int main()
  360. {
  361.     struct sockaddr_in serverAddr;
  362.  
  363.     // Create a socket
  364.     int client_fd = socket(AF_INET, SOCK_STREAM, 0);
  365.  
  366.     if (client_fd < 0)
  367.     {
  368.         perror("nCouldn't create socket");
  369.         exit(EXIT_FAILURE);
  370.     }
  371.  
  372.     // Initialize the server address structure
  373.     serverAddr.sin_family = AF_INET;
  374.     serverAddr.sin_port = htons(PORT);
  375.  
  376.     if (inet_pton(AF_INET, "127.0.0.1", &serverAddr;.sin_addr) <= 0)
  377.     {
  378.         printf("nInvalid address / Address not supported");
  379.         exit(EXIT_FAILURE);
  380.     }
  381.  
  382.     // Establish a connection to the server
  383.     if (connect(client_fd, (struct sockaddr *)&serverAddr;, sizeof(serverAddr)) < 0)
  384.     {
  385.         perror("nCouldn't connect");
  386.         exit(EXIT_FAILURE);
  387.     }
  388.  
  389.     while (1)
  390.     {
  391.         char messageSent[100];
  392.         char messageReceived[100];
  393.        
  394.         // Read a line of text from the user
  395.         printf("nEnter message:n");
  396.         fgets(messageSent, 100, stdin);
  397.  
  398.         // Send the user's input to the server
  399.         send(client_fd, messageSent, sizeof(messageSent), 0);
  400.  
  401.         // Receive the response from the server
  402.         recv(client_fd, messageReceived, 100, 0);
  403.  
  404.         // Print the server's response
  405.         printf("nMessage from server:n%s", messageReceived);
  406.  
  407.         if(strncmp(messageReceived, "Exit", 4) == 0) {
  408.             printf("nClient exiting...n");
  409.             break;
  410.         }
  411.     }
  412.    
  413.     close(client_fd);
  414. }