123456789101112131415161718192021222324252627282930313233343536373839 |
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <unistd.h>
- #include <arpa/inet.h>
- #define PORT 8080
- #define BUFFER_SIZE 1024
- int main() {
- int sockfd;
- char buffer[BUFFER_SIZE];
- struct sockaddr_in server_addr;
-
- if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
- perror("Socket creation failed");
- exit(EXIT_FAILURE);
- }
- memset(&server_addr, 0, sizeof(server_addr));
- server_addr.sin_family = AF_INET;
- server_addr.sin_port = htons(PORT);
- server_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); // Change to server IP if needed
-
- printf("Enter message to send to server: ");
- scanf("%[^\n]", buffer);
- sendto(sockfd, buffer, strlen(buffer), 0, (const struct sockaddr *)&server_addr, sizeof(server_addr));
- char buf[20];
- recvfrom(sockfd,buf, sizeof(buf), 0, NULL, NULL);
- printf("Received message length from server: %s\n",buf);
- close(sockfd);
- return 0;
- }
|