Practice Lab Exam on Computer Networks Socket programming
Practice Lab Exam on Computer Networks Socket programming
TASK:
SOCKET PROGRAMMING
TASK1:
SERVER:
#include <iostream>
#include <winsock.h>
#define PORT 9090
int main()
{
WSADATA ws;
if (statusflag < 0) {
cout << endl << "WSAStartup failed";
exit(EXIT_FAILURE);
}
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));
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;
// Cleanup
closesocket(nClient);
closesocket(Listener);
WSACleanup();
return 0;
} // main
CLIENT:
#include <iostream>
#include <winsock.h>
#define PORT 9090
int main()
{
WSADATA ws;
if (statusflag < 0) {
cout << endl << "WSAStartup failed";
exit(EXIT_FAILURE);
}
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));
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
// Cleanup
closesocket(ClientS);
WSACleanup();
return 0;
} // main
OUTPUT:
TASK2:
SERVER:
#include <iostream>
#include <winsock.h>
#include <thread>
#include <vector>
while (true) {
int statusflag = recv(client_socket, msgbuf, 1024, 0);
if (statusflag > 0) {
cout << "Message from client: " << msgbuf << endl;
int main() {
WSADATA ws;
int statusflag = WSAStartup(MAKEWORD(2, 2), &ws);
if (statusflag < 0) {
cout << "WSAStartup 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);
}
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);
WSACleanup();
return 0;
}
CLIENT:
#include <iostream>
#include <winsock.h>
#include <thread>
#include <string>
int main() {
WSADATA ws;
int statusflag = WSAStartup(MAKEWORD(2, 2), &ws);
if (statusflag < 0) {
cout << "WSAStartup 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));
while (true) {
char msgbuf[1024] = {0,};
cout << "Enter Msg: ";
cin.getline(msgbuf, 1024);
closesocket(ClientS);
WSACleanup();
return 0;
}
OUTPUT: