Question: Q1 : Modify the programs to create a chat between the server and the client. The client send a text to the server, the server

Q1 : Modify the programs to create a chat between the server and the client. The client send a text to the server, the server will print in and sends an answer to the client. Use a loop to repeat the task.

import java.io.*;

import java.net.*;

import java.util.Scanner;

public class Server{

public static void main(String argv[]) throws Exception {

/*1.connection*/

ServerSocket serverSocket = new ServerSocket(6789);

Socket connectionSocket = serverSocket.accept();

/*2. reading from the socket using scanner or BufferedReader*/

Scanner scanner = new Scanner(connectionSocket.getInputStream());

//read

String clientSentence = scanner.nextLine();

System.out.println("Received: " + clientSentence);

/*3. modification of the sentence*/

String modifiedSentence =clientSentence.toUpperCase();

/*4. writing in the socket*/

DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());

outToClient.writeBytes(modifiedSentence+ ' ');

scanner.close();

connectionSocket.close();

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!