0% found this document useful (0 votes)
11 views2 pages

Didl

This C program initializes a UDP client using Winsock, allowing the user to send commands to a server at a specified IP address and port. It handles sending messages and receiving responses in a loop, terminating if the server sends a 'STOP' message. The program also includes error handling for socket creation and data transmission failures.

Uploaded by

rovanrex
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views2 pages

Didl

This C program initializes a UDP client using Winsock, allowing the user to send commands to a server at a specified IP address and port. It handles sending messages and receiving responses in a loop, terminating if the server sends a 'STOP' message. The program also includes error handling for socket creation and data transmission failures.

Uploaded by

rovanrex
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <stdio.

h>
#include <string.h>
#include <winsock2.h>

#pragma comment(lib, "ws2_32.lib")

int main() {
WSADATA wsa;
SOCKET server_socket;
struct sockaddr_in server;
char message[1000], server_response[500];
int server_len, recv_size;

printf("Initializing Winsock...\n");
if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0) {
printf("Winsock initialization failed. Error Code: %d\n",
WSAGetLastError());
return 1;
}
printf("Winsock initialized.\n");

// Create UDP socket


server_socket = socket(AF_INET, SOCK_DGRAM, 0);
if (server_socket == INVALID_SOCKET) {
printf("Socket creation failed. Error Code: %d\n", WSAGetLastError());
WSACleanup();
return 1;
}

// Server address configuration


server.sin_family = AF_INET;
server.sin_addr.s_addr = inet_addr("192.168.137.144"); // Server IP address
server.sin_port = htons(8888); // Server port

// Get the server address length


server_len = sizeof(server);

printf("Enter a command (e.g., 't 1 2 3 4 5' to process numbers or 'p aba 121'
to check palindrome):\n");
fgets(message, sizeof(message), stdin);
message[strcspn(message, "\n")] = '\0'; // Remove trailing newline

// Send the message to the server


if (sendto(server_socket, message, strlen(message), 0, (struct sockaddr
*)&server, server_len) == SOCKET_ERROR) {
printf("Send failed. Error Code: %d\n", WSAGetLastError());
closesocket(server_socket);
WSACleanup();
return 1;
}

while (1) {
// Receive response from the server
recv_size = recvfrom(server_socket, server_response,
sizeof(server_response) - 1, 0, (struct sockaddr *)&server, &server_len);
if (recv_size == SOCKET_ERROR) {
printf("Receive failed. Error Code: %d\n", WSAGetLastError());
break;
}
server_response[recv_size] = '\0'; // Null terminate the response
printf("Server: %s\n", server_response);

// If the server sends "STOP", terminate the client


if (strcmp(server_response, "STOP") == 0) {
printf("Server has terminated the connection.\n");
break;
}
}

// Cleanup
closesocket(server_socket);
WSACleanup();

return 0;
}

You might also like