Question: Program 1 : Consider the following two programs. Write and execute them. ( You can use line commands to execute the programs or run two
Program :
Consider the following two programs. Write and execute them. You can use line commands to execute the programs or run two Eclipse programs
import java.io; import java.net.;
import java.util.Scanner;
public class Server
public static void mainString argv throws Exception
connection
ServerSocket serverSocket new ServerSocket;
Socket connectionSocket serverSocket.accept;
A scanner or a BufferedReader for reading from the socket
Scanner scanner new ScannerconnectionSocketgetInputStream;
A DataOutputStream object to write in the socket
DataOutputStream outToClientnew DataOutputStreamconnectionSocketgetOutputStream;
reading from the socket
String clientSentence scanner.nextLine;
System.out.printlnReceived: clientSentence;
modification of the sentence
String modifiedSentence clientSentence.toUpperCase;
writing in the socket
outToClient.writeBytesmodifiedSentence
;
closing connections
scanner.close;
connectionSocket.close;
import java.io;
import java.net.;
import java.util.Scanner;
public class Client
public static void mainString argv throws Exception
Connection
Socket connectionSocket new Socket localhost;
Scanner scanner, scannerFromSocket;
DataOutputStream outToServer;
String sentenceFromServer;
String clientSentence;
Scanner to read from keyboard
scanner new ScannerSystemin;
Scanner to read from the socket
scannerFromSocket new ScannerconnectionSocketgetInputStream;
DataOutputStream to write in the socket
outToServer new DataOutputStreamconnectionSocketgetOutputStream;
receiving and sending messages
clientSentence scanner.nextLine;
outToServer.writeBytesclientSentence
;
sentenceFromServerscannerFromSocket.nextLine;
System.out.printlnsentenceFromServer;
scannerFromSocket.close;
scanner.close;
connectionSocket.close;
What happens if you execute the client program before the server one?
Add a loop to the programs to make it repeat the task many times.
Modify the programs to create a chat between the server and the client: The client sends a text to the server. The server prints the received text and sends an answer to the client. Use a loop to repeat the task
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
