Guest User

Untitled

a guest
Oct 18th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <sys/socket.h>
  3. #include <netinet/in.h>
  4. #include <arpa/inet.h>
  5. #include <string.h>
  6.  
  7. int main(){
  8. int clientSocket;
  9. char buffer[1024];
  10. struct sockaddr_in serverAddr;
  11. socklen_t addr_size;
  12.  
  13. /*---- Create the socket. The three arguments are: ----*/
  14. /* 1) Internet domain 2) Stream socket 3) Default protocol (TCP in this case) */
  15. clientSocket = socket(PF_INET, SOCK_STREAM, 0);
  16.  
  17. /*---- Configure settings of the server address struct ----*/
  18. /* Address family = Internet */
  19. serverAddr.sin_family = AF_INET;
  20. /* Set port number, using htons function to use proper byte order */
  21. serverAddr.sin_port = htons(7891);
  22. /* Set IP address to localhost */
  23. serverAddr.sin_addr.s_addr = inet_addr("127.168.14.129");
  24. /* Set all bits of the padding field to 0 */
  25. memset(serverAddr.sin_zero, '\0', sizeof serverAddr.sin_zero);
  26.  
  27. /*---- Connect the socket to the server using the address struct ----*/
  28. /*---- Connect the socket to the server using the address struct ----*/
  29. addr_size = sizeof serverAddr;
  30. connect(clientSocket, (struct sockaddr *) &serverAddr, addr_size);
  31.  
  32. /*---- Read the message from the server into the buffer ----*/
  33. recv(clientSocket, buffer, 1024, 0);
  34.  
  35. /*---- Print the received message ----*/
  36. printf("Data received: %s",buffer);
  37.  
  38. return 0;
  39. }
Add Comment
Please, Sign In to add comment