Consider the following pseudocode for a chat program, running over a connected socket. How does a web server accept a second connection, when a TCP socket is defined by <Source IP, Source Port, Destination IP, Destination Port> indicating that the host port can only be used by a single socket, even if it is using threads?
Client | Server |
Read from socket | Read from socket |
Print to screen | Print to screen |
Read from keyboard | Read from keyboard |
Send message over socket | Send message over socket |
Expert Answer
/*instruction to student
first run the server program in one window
then open another window run the clinet program then start chating */
//import input and out put files and networking requirements
import java.io.*;
import java.net.*;
public class tcpserver // creating server socket
{
public static void main(String[] args) throws Exception //main program
{
ServerSocket ssk = new ServerSocket(2000); //creating server socket to communicate with clients
System.out.println(“Server is ready for chatt with any client”); //display ready message
Socket sock = ssk.accept( ); //server socket ready to accept the data
// reading from keyboard
BufferedReader keyRead = new BufferedReader(new InputStreamReader(System.in));
OutputStream ostream = sock.getOutputStream();
PrintWriter pwriter = new PrintWriter(ostream, true);// sending to client
InputStream istream = sock.getInputStream();// receiving from server
BufferedReader receivemessage = new BufferedReader(new InputStreamReader(istream));
String receiveMessage, sendMessage;
while(true) //repeat the loop until message is null
{
if((receiveMessage = receivemessage.readLine()) != null) //check message is null or not
{
System.out.println(receiveMessage); //received message display on the screen
}
sendMessage = keyRead.readLine(); //read message from the key board
pwriter.println(sendMessage); //message send to client
pwriter.flush(); //clear the buffer
}
}
}
import java.io.*; //import input and out put files
import java.net.*; //networking requirements
public class tcpclient
{
public static void main(String[] args) throws Exception // creating clinet socket
{
Socket sock = new Socket(“127.0.0.1”, 2000); //create socket with default ip and server port
BufferedReader keyRead = new BufferedReader(new InputStreamReader(System.in));// reading from keyboard
OutputStream ostream = sock.getOutputStream();
PrintWriter pwrite = new PrintWriter(ostream, true);// sending to client
InputStream istream = sock.getInputStream(); // receiving from server
BufferedReader receiveRead = new BufferedReader(new InputStreamReader(istream));
System.out.println(“Start the chat, type your message and press Enter”); //creating your chat message
String receiveMessage, sendMessage; //string objects to generate chat messages
while(true) //repeat the loop until message is null
{
sendMessage = keyRead.readLine(); // readdata from the keyboard
pwrite.println(sendMessage); // message sending to server
pwrite.flush(); // clear the buffer
if((receiveMessage = receiveRead.readLine()) != null) //receive message from server
{
System.out.println(receiveMessage); // displaying on the keyboard
}
}
}
}