client1.c 967 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <unistd.h>
  5. #include <arpa/inet.h>
  6. #define PORT 8080
  7. #define BUFFER_SIZE 1024
  8. int main() {
  9. int sockfd;
  10. char buffer[BUFFER_SIZE];
  11. struct sockaddr_in server_addr;
  12. if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
  13. perror("Socket creation failed");
  14. exit(EXIT_FAILURE);
  15. }
  16. memset(&server_addr, 0, sizeof(server_addr));
  17. server_addr.sin_family = AF_INET;
  18. server_addr.sin_port = htons(PORT);
  19. server_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); // Change to server IP if needed
  20. printf("Enter message to send to server: ");
  21. scanf("%[^\n]", buffer);
  22. sendto(sockfd, buffer, strlen(buffer), 0, (const struct sockaddr *)&server_addr, sizeof(server_addr));
  23. char buf[20];
  24. recvfrom(sockfd,buf, sizeof(buf), 0, NULL, NULL);
  25. printf("Received message length from server: %s\n",buf);
  26. close(sockfd);
  27. return 0;
  28. }