0% found this document useful (0 votes)
3 views

Practice Lab Exam on Computer Networks Socket programming

The document contains two tasks related to socket programming, one for a server and client implementation in C++. The first task demonstrates basic client-server communication using TCP sockets, while the second task enhances the server to handle multiple clients using threads. Both implementations include error handling and message exchange between the server and client.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Practice Lab Exam on Computer Networks Socket programming

The document contains two tasks related to socket programming, one for a server and client implementation in C++. The first task demonstrates basic client-server communication using TCP sockets, while the second task enhances the server to handle multiple clients using threads. Both implementations include error handling and message exchange between the server and client.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

COMPUTER NETWORKS LAB

TASK:
SOCKET PROGRAMMING

TASK1:

SERVER:

#include <iostream>
#include <winsock.h>
#define PORT 9090

using namespace std;

struct sockaddr_in srv;

int main()
{
WSADATA ws;

int statusflag = WSAStartup(MAKEWORD(2, 2), &ws);

if (statusflag < 0) {
cout << endl << "WSAStartup failed";
exit(EXIT_FAILURE);
}

int Listener = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

if (Listener < 0) {
cout << "\nSocket failed";
exit(EXIT_FAILURE);
}

srv.sin_family = AF_INET;
srv.sin_port = htons(PORT); // Host to network
srv.sin_addr.s_addr = INADDR_ANY;
memset(&srv.sin_zero, 0, sizeof(srv.sin_zero));

statusflag = bind(Listener, (struct sockaddr*)&srv, sizeof(srv));

if (statusflag < 0) {
cout << "\nBinding failed";
exit(EXIT_FAILURE);
}
statusflag = listen(Listener, 4);
if (statusflag < 0) {
cout << "\nListen failed";
exit(EXIT_FAILURE);
}
int sz = sizeof(srv);
cout << "\nListening state / waiting for client";
int nClient = accept(Listener, NULL, &sz);

if (nClient < 0) {
cout << "\nClient socket not connected";
exit(EXIT_FAILURE);
}

else {
char msgbuf[1024] = { 0, };
while (true) {
// Receive message from the client
statusflag = recv(nClient, msgbuf, sizeof(msgbuf), 0);
if (statusflag > 0) {
cout << "\nReceived: " << msgbuf;

// Prepare to send a response back to the client


cout << "\nEnter your response: ";
char response[1024];
cin.getline(response, sizeof(response));

// Send response to the client


statusflag = send(nClient, response, strlen(response), 0);
if (statusflag < 0) {
cout << "\nFailed to send response";
exit(EXIT_FAILURE);
}
} else if (statusflag == 0) {
cout << "\nClient disconnected";
break; // Exit the loop if the client disconnected
} else {
cout << "\nError receiving message";
exit(EXIT_FAILURE);
}
} // while
} // else

// Cleanup
closesocket(nClient);
closesocket(Listener);
WSACleanup();

return 0;
} // main
CLIENT:

#include <iostream>
#include <winsock.h>
#define PORT 9090

using namespace std;

struct sockaddr_in srv;

int main()
{
WSADATA ws;

int statusflag = WSAStartup(MAKEWORD(2, 2), &ws);

if (statusflag < 0) {
cout << endl << "WSAStartup failed";
exit(EXIT_FAILURE);
}

int ClientS = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

if (ClientS < 0) {
cout << "\nSocket failed";
exit(EXIT_FAILURE);
}

srv.sin_family = AF_INET;
srv.sin_port = htons(PORT); // Host to network
srv.sin_addr.s_addr = inet_addr("172.20.5.22"); // Server IP
memset(&srv.sin_zero, 0, sizeof(srv.sin_zero));

statusflag = connect(ClientS, (sockaddr*)&srv, sizeof(srv));

if (statusflag < 0) {
cout << "\nConnection failed";
exit(EXIT_FAILURE);
}

while (true) {
char msgbuf[1024] = { 0, };
cout << "\nEnter Msg: ";
cin.getline(msgbuf, sizeof(msgbuf)); // Use getline instead of gets_s

// Send message to the server


statusflag = send(ClientS, msgbuf, strlen(msgbuf), 0);
if (statusflag < 0) {
cout << "\n Msg not sent";
break; // Exit if sending fails
} else {
cout << "\nMsg Sent!!!";
}

// Receive response from the server


char response[1024] = { 0, };
statusflag = recv(ClientS, response, sizeof(response), 0);
if (statusflag > 0) {
cout << "\nServer response: " << response;
} else if (statusflag == 0) {
cout << "\nServer disconnected";
break; // Exit if the server disconnects
} else {
cout << "\nError receiving response";
break; // Exit on error
}
} // while

// Cleanup
closesocket(ClientS);
WSACleanup();

return 0;
} // main

OUTPUT:
TASK2:
SERVER:

#include <iostream>
#include <winsock.h>
#include <thread>
#include <vector>

#define PORT 9090

using namespace std;

struct sockaddr_in srv;


vector<int> client_sockets;

void handleClient(int client_socket) {


char msgbuf[1024] = {0,};

while (true) {
int statusflag = recv(client_socket, msgbuf, 1024, 0);
if (statusflag > 0) {
cout << "Message from client: " << msgbuf << endl;

// Optionally send a reply back to the client


string reply = "Message received!";
send(client_socket, reply.c_str(), reply.size(), 0);
} else {
cout << "Client disconnected" << endl;
break;
}
}

// Remove the client socket from the vector


closesocket(client_socket);
}

int main() {
WSADATA ws;
int statusflag = WSAStartup(MAKEWORD(2, 2), &ws);

if (statusflag < 0) {
cout << "WSAStartup failed" << endl;
exit(EXIT_FAILURE);
}

int Listener = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);


if (Listener < 0) {
cout << "Socket failed" << endl;
exit(EXIT_FAILURE);
}

srv.sin_family = AF_INET;
srv.sin_port = htons(PORT); // host to network
srv.sin_addr.s_addr = INADDR_ANY;
memset(&srv.sin_zero, 0, sizeof(srv.sin_zero));
statusflag = bind(Listener, (struct sockaddr*)&srv, sizeof(srv));
if (statusflag < 0) {
cout << "Binding failed" << endl;
exit(EXIT_FAILURE);
}

statusflag = listen(Listener, 4);


if (statusflag < 0) {
cout << "Listen failed" << endl;
exit(EXIT_FAILURE);
}

cout << "Listening state / waiting for clients..." << endl;

while (true) {
int sz = sizeof(srv);
int nClient = accept(Listener, NULL, &sz);
if (nClient < 0) {
cout << "Client socket not connected" << endl;
continue;
} else {
cout << "Client connected!" << endl;
client_sockets.push_back(nClient);

// Create a thread for the new client


std::thread(clientHandler, nClient).detach();
}
}

WSACleanup();
return 0;
}

CLIENT:

#include <iostream>
#include <winsock.h>
#include <thread>
#include <string>

#define PORT 9090

using namespace std;

struct sockaddr_in srv;

void receiveMessages(int client_socket) {


char msgbuf[1024] = {0,};
while (true) {
int statusflag = recv(client_socket, msgbuf, 1024, 0);
if (statusflag > 0) {
cout << "Message from server: " << msgbuf << endl;
} else {
cout << "Server disconnected" << endl;
break;
}
}
}

int main() {
WSADATA ws;
int statusflag = WSAStartup(MAKEWORD(2, 2), &ws);

if (statusflag < 0) {
cout << "WSAStartup failed" << endl;
exit(EXIT_FAILURE);
}

int ClientS = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);


if (ClientS < 0) {
cout << "Socket failed" << endl;
exit(EXIT_FAILURE);
}

srv.sin_family = AF_INET;
srv.sin_port = htons(PORT); // host to network
srv.sin_addr.s_addr = inet_addr("172.20.5.22");
memset(&srv.sin_zero, 0, sizeof(srv.sin_zero));

statusflag = connect(ClientS, (sockaddr*)&srv, sizeof(srv));


if (statusflag < 0) {
cout << "Connection failed" << endl;
exit(EXIT_FAILURE);
}

// Start a thread to receive messages


std::thread(receiveMessages, ClientS).detach();

while (true) {
char msgbuf[1024] = {0,};
cout << "Enter Msg: ";
cin.getline(msgbuf, 1024);

statusflag = send(ClientS, msgbuf, strlen(msgbuf), 0);


if (statusflag < 0) {
cout << "Message not sent" << endl;
exit(EXIT_FAILURE);
} else {
cout << "Msg Sent!!!" << endl;
}
}

closesocket(ClientS);
WSACleanup();
return 0;
}
OUTPUT:

You might also like