p23cs025 Lab3
p23cs025 Lab3
p23cs025 Lab3
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <winsock2.h>
int main() {
WSADATA wsaData;
SOCKET serverSocket, clientSocket;
struct sockaddr_in serverAddr, clientAddr;
int addrSize = sizeof(struct sockaddr_in);
memset(&serverAddr, 0, sizeof(serverAddr));
serverAddr.sin_family = AF_INET;
serverAddr.sin_addr.s_addr = INADDR_ANY;
serverAddr.sin_port = htons(PORT);
if (bind(serverSocket, (struct sockaddr*)&serverAddr,
sizeof(serverAddr)) == SOCKET_ERROR) {
perror("Error binding socket");
closesocket(serverSocket);
WSACleanup();
return EXIT_FAILURE;
}
if (listen(serverSocket, 1) == SOCKET_ERROR) {
perror("Error listening for connections");
closesocket(serverSocket);
WSACleanup();
return EXIT_FAILURE;
}
char buffer[BUFFER_SIZE];
while (1) {
memset(buffer, 0, sizeof(buffer));
closesocket(clientSocket);
closesocket(serverSocket);
WSACleanup();
return 0;
}
Step 2: client.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <winsock2.h>
#define PORT 12345
int main() {
WSADATA wsaData;
SOCKET clientSocket;
return EXIT_FAILURE;
if (clientSocket == INVALID_SOCKET) {
WSACleanup();
return EXIT_FAILURE;
memset(&serverAddr, 0, sizeof(serverAddr));
serverAddr.sin_family = AF_INET;
serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
serverAddr.sin_port = htons(PORT);
closesocket(clientSocket);
WSACleanup();
return EXIT_FAILURE;
char message[BUFFER_SIZE];
while (1) {
memset(message, 0, sizeof(message));
if (bytesSent <= 0) {
break;
}
if (strcmp(message, "quit\n") == 0) {
break;
double loadPercentage;
if (bytesRead <= 0) {
break;
closesocket(clientSocket);
WSACleanup();
return 0;
}
Step 3: Compile & Run the server code