client.c 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <unistd.h>
  5. #include <arpa/inet.h>
  6. #include <fcntl.h>
  7. #include <errno.h>
  8. #define PORT 8081
  9. #define SERVER_IP "127.0.0.1"
  10. int main() {
  11. int sockfd;
  12. struct sockaddr_in server_address;
  13. if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
  14. perror("Socket creation failed");
  15. exit(EXIT_FAILURE);
  16. }
  17. printf("enter into program\n");
  18. int flags = fcntl(sockfd, F_GETFL, 0);
  19. fcntl(sockfd, F_SETFL, flags | O_NONBLOCK);
  20. server_address.sin_family = AF_INET;
  21. server_address.sin_port = htons(PORT);
  22. inet_pton(AF_INET, SERVER_IP, &server_address.sin_addr);
  23. int connect_status;
  24. label:
  25. connect_status = connect(sockfd, (struct sockaddr *)&server_address, sizeof(server_address));
  26. if (connect_status == 0) {
  27. printf("conncetion succcessfully\n");
  28. while(1)
  29. {
  30. char s[20];
  31. printf("enter the data two send\n");
  32. scanf("%s",s);
  33. send(sockfd,s,strlen(s),0);
  34. }
  35. }
  36. else
  37. {
  38. while (1) {
  39. printf("still trying to connect\n");
  40. sleep(1);
  41. goto label;
  42. }
  43. }
  44. close(sockfd);
  45. return 0;
  46. }