#include #include #include #include #include #define PORT 8080 #define BUFFER_SIZE 1024 void main() { int socket_value; char message[BUFFER_SIZE]; struct sockaddr_in server_addr={0}; struct sockaddr_in client_addr; socklen_t client_len = sizeof(client_addr); // socket creation if((socket_value=socket(AF_INET,SOCK_DGRAM,0)) <0) { perror("server socket creation error"); exit(EXIT_FAILURE); } //server addr speciifications server_addr.sin_family = AF_INET; server_addr.sin_addr.s_addr = INADDR_ANY; server_addr.sin_port = htons(PORT); // bind the socket to the port in server if(bind(socket_value,(struct sockaddr*)&server_addr,sizeof(server_addr))<0) { perror("bind fail"); close(socket_value); exit(EXIT_FAILURE); } printf("server is listening on port\n"); // receive message from client.. while(1) { int len=recvfrom(socket_value, (char *)message, BUFFER_SIZE, MSG_WAITALL, (struct sockaddr *)&client_addr, &client_len); message[len]='\0'; printf("%s/n",message); // Send the length of the received message back to the client int message_length = strlen(message); snprintf(message, sizeof(message), "%d", message_length); // Convert length to string sendto(socket_value, (const char *)message, strlen(message), MSG_CONFIRM, (const struct sockaddr *)&client_addr, client_len); printf("Sent message length to client: %d\n", message_length); } close(socket_value); }