Please convert both programs to C++ Program 1: import java.net.*; public class s
ID: 674551 • Letter: P
Question
Please convert both programs to C++
Program 1:
import java.net.*;
public class serverSocketTest {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = null;
int cost = 0;
try {
serverSocket = new ServerSocket(12804);
} catch (IOException e) {
System.err.println("Could not listen on port: 12804.");
System.exit(1);
}
Socket clientSocket = null;
System.out.println ("Waiting for connection.....");
try {
clientSocket = serverSocket.accept();
} catch (IOException e) {
System.err.println("Connection failed");
System.exit(1);
}
System.out.println ("Connection successful");
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String address = clientSocket.getRemoteSocketAddress().toString();
System.out.println("Client Routing Number: " + in.readLine());
System.out.println("Initial Client Link Cost: " + in.readLine());
out.println("Server Routing Number: "+ address);
out.println("Initial Server Link Cost: "+ cost);
out.close();
in.close();
clientSocket.close();
serverSocket.close();
}
Program 2:
import java.io.*;
public class clientSocketTest {
public static void main(String[] args) throws IOException {
String serverHostname = new String ("afsconnect2.mit.edu");
Socket clientSocket = null;
PrintWriter out = null;
BufferedReader in = null;
int cost = 1;
try {
clientSocket = new Socket("afsconnect2.mit.edu", 12804);
out = new PrintWriter(clientSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
} catch (UnknownHostException e) {
System.err.println("Cannot Find: " + serverHostname);
System.exit(1);
} catch (IOException e) {
System.err.println("I/O failure for: " + serverHostname + " " + e.getMessage());
// e.printStackTrace();
System.exit(1);
}
System.out.println("Connected with host");
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
String address = clientSocket.getRemoteSocketAddress().toString();
out.println(address);
out.println(cost);
System.out.println(in.readLine());
System.out.println(in.readLine());
out.close();
in.close();
stdIn.close();
clientSocket.close();
}
}
Explanation / Answer
For executing client server programs we can do it on two systems or one local system
Note: These commands will work on linux or unix environment
executing in two systems
exceution steps in this program
Server
g++ ServerSocket.cpp -lpthread -o ServerSocket
./ServerSocket <port number>
--- we have change compilation step at client side as
g++ ClientSocket.cpp -o ClientSocket
./Client <ipaddress> <port number>
or
g++ ClientSocket.cpp -o -lpthread ClientSocket
./ClientSocket <ipaddress> <port number>
executing at local system
we have to open two command prompts and execute the program
Server
g++ ServerSocket.cpp -lpthread -o ServerSocket
./ServerSocket <port number>
Client
g++ ClientSocket.cpp -o ClientSocket
./ClientSocket localhost <port number>
ServerSocket.cpp
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <iostream>
#include <fstream>
#include <strings.h>
#include <stdlib.h>
#include <string>
#include <pthread.h>
using namespace std;
void *task1(void *);
static int connFd;
int main(int argc, char* argv[])
{
int pId, portNo, listenFd;
socklen_t len; //store size of the address
bool loop = false;
struct sockaddr_in svrAdd, clntAdd;
pthread_t threadA[3];
if (argc < 2)
{
cerr << "Syntam : ./server <port>" << endl;
return 0;
}
portNo = 12804;
//create socket
listenFd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
if(listenFd < 0)
{
cerr << "Cannot open socket" << endl;
return 0;
}
bzero((char*) &svrAdd, sizeof(svrAdd));
svrAdd.sin_family = AF_INET;
svrAdd.sin_addr.s_addr = INADDR_ANY;
svrAdd.sin_port = htons(portNo);
//bind socket
if(bind(listenFd, (struct sockaddr *)&svrAdd, sizeof(svrAdd)) < 0)
{
cerr << "Cannot bind" << endl;
return 0;
}
listen(listenFd, 5);
int noThread = 0;
while (noThread < 3)
{
socklen_t len = sizeof(clntAdd);
cout << "Listening" << endl;
//this is where client connects. svr will hang in this mode until client conn
connFd = accept(listenFd, (struct sockaddr *)&clntAdd, &len);
if (connFd < 0)
{
cerr << "Cannot accept connection" << endl;
return 0;
}
else
{
cout << "Connection successful" << endl;
}
pthread_create(&threadA[noThread], NULL, task1, NULL);
noThread++;
}
for(int i = 0; i < 3; i++)
{
pthread_join(threadA[i], NULL);
}
}
void *task1 (void *dummyPt)
{
cout << "Thread No: " << pthread_self() << endl;
char test[300];
bzero(test, 301);
bool loop = false;
while(!loop)
{
bzero(test, 301);
read(connFd, test, 300);
string tester (test);
cout << tester << endl;
if(tester == "exit")
break;
}
cout << " Closing thread and conn" << endl;
close(connFd);
}
compile
g++ ServerSocket.cpp -lpthread -o ServerSocket
./ServerSocket 12804
ClientSocket.CPP
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <iostream>
#include <fstream>
#include <strings.h>
#include <stdlib.h>
#include <string>
#include <pthread.h>
using namespace std;
void *task1(void *);
static int connFd;
int main(int argc, char* argv[])
{
int pId, portNo, listenFd;
socklen_t len; //store size of the address
bool loop = false;
struct sockaddr_in svrAdd, clntAdd;
pthread_t threadA[3];
if (argc < 2)
{
cerr << "Syntam : ./server <port>" << endl;
return 0;
}
portNo = 12804;
//create socket
listenFd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
if(listenFd < 0)
{
cerr << "Cannot open socket" << endl;
return 0;
}
bzero((char*) &svrAdd, sizeof(svrAdd));
svrAdd.sin_family = AF_INET;
svrAdd.sin_addr.s_addr = INADDR_ANY;
svrAdd.sin_port = htons(portNo);
//bind socket
if(bind(listenFd, (struct sockaddr *)&svrAdd, sizeof(svrAdd)) < 0)
{
cerr << "Cannot bind" << endl;
return 0;
}
listen(listenFd, 5);
int noThread = 0;
while (noThread < 3)
{
socklen_t len = sizeof(clntAdd);
cout << "Listening" << endl;
//this is where client connects. svr will hang in this mode until client conn
connFd = accept(listenFd, (struct sockaddr *)&clntAdd, &len);
if (connFd < 0)
{
cerr << "Cannot accept connection" << endl;
return 0;
}
else
{
cout << "Connection successful" << endl;
}
pthread_create(&threadA[noThread], NULL, task1, NULL);
noThread++;
}
for(int i = 0; i < 3; i++)
{
pthread_join(threadA[i], NULL);
}
}
void *task1 (void *dummyPt)
{
cout << "Thread No: " << pthread_self() << endl;
char test[300];
bzero(test, 301);
bool loop = false;
while(!loop)
{
bzero(test, 301);
read(connFd, test, 300);
string tester (test);
cout << tester << endl;
if(tester == "exit")
break;
}
cout << " Closing thread and conn" << endl;
close(connFd);
}
compile
g++ Client.cpp -o Client
./Client localhost 12804
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.