SHARE
TWEET

client.c

deni-parvanov May 28th, 2020 843 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <sys/socket.h>
  3. #include <netinet/in.h>
  4. #include <string.h>
  5. #include <arpa/inet.h>
  6. #include <stdlib.h>
  7. #include <fcntl.h>
  8. #include <unistd.h>
  9. #include <errno.h>
  10.  
  11. #define SERVER_PORT 8090
  12.  
  13. int main() {
  14.     int clientSocket;
  15.     char message[1024], buffer[1040];
  16.  
  17.     struct sockaddr_in serverAddress;
  18.  
  19.     socklen_t addressSize;
  20.  
  21.     clientSocket = socket(PF_INET, SOCK_STREAM, 0);
  22.  
  23.     serverAddress.sin_family = AF_INET;
  24.     serverAddress.sin_port = htons(SERVER_PORT);
  25.     serverAddress.sin_addr.s_addr = inet_addr("127.0.0.1");
  26.  
  27.     memset(serverAddress.sin_zero, '\0', sizeof serverAddress.sin_zero);
  28.  
  29.     addressSize = sizeof(serverAddress);
  30.     if (connect(clientSocket, (struct sockaddr *) &serverAddress, addressSize) != 0) {
  31.         printf("Connect Error: %s\n", strerror(errno));
  32.     }
  33.  
  34.     strcpy(message, "Hello socket server!");
  35.     if (send(clientSocket, message, strlen(message), 0) < 0) {
  36.         printf("Socket send failed\n");
  37.     }
  38.  
  39.     if (recv(clientSocket, buffer, 2048, 0) < 0) {
  40.         printf("Socket receive failed\n");
  41.     }
  42.  
  43.     printf("Received from server: %s\n", buffer);
  44.     close(clientSocket);
  45. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top