Facebook
From asdf, 1 Month ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 147
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <sys/socket.h>
  5. #include <netinet/in.h>
  6. #include <unistd.h>
  7.  
  8. #define SERVER_ADDRESS "127.0.0.1"
  9. #define SERVER_PORT```c
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <sys/socket.h>
  14. #include <netinet/in.h>
  15. #include <unistd.h>
  16.  
  17. #define SERVER_ADDRESS "127.0.0.1"
  18. #define SERVER_PORT 9002
  19. #define MAX_MESSAGE_LENGTH 1024
  20.  
  21. int main() {
  22.     int client_fd;
  23.     struct sockaddr_in server_address;
  24.     char message[MAX_MESSAGE_LENGTH];
  25.  
  26.     // Create socket
  27.     client_fd = socket(AF_INET, SOCK_STREAM, 0);
  28.     if (client_fd < 0) {
  29.         perror("socket");
  30.         exit(1);
  31.     }
  32.  
  33.     // Set server address
  34.     memset(&server;_address, 0, sizeof(server_address));
  35.     server_address.sin_family = AF_INET;
  36.     server_address.sin_port = htons(SERVER_PORT);
  37.     if (inet_pton(AF_INET, SERVER_ADDRESS, &(server_address.sin_addr)) <= 0) {
  38.         perror("inet_pton");
  39.         exit(1);
  40.     }
  41.  
  42.     // Connect to server
  43.     if (connect(client_fd, (struct sockaddr *)&server;_address, sizeof(server_address)) < 0) {
  44.         perror("connect");
  45.         exit(1);
  46.     }
  47.  
  48.     // Receive welcome message from server
  49.     memset(message, 0, MAX_MESSAGE_LENGTH);
  50.     if (recv(client_fd, message, MAX_MESSAGE_LENGTH, 0) < 0) {
  51.         perror("recv");
  52.         exit(1);
  53.     }
  54.     printf("Server sent the data: %s\n", message);
  55.  
  56.     // Close client socket
  57.     close(client_fd);
  58.  
  59.     return 0;
  60. }