Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

use java Socket Programming -- Single Thread Chat Room In this assignment, you will create a simple chat program on local area network. Two person

use java

Socket Programming -- Single Thread Chat Room

In this assignment, you will create a simple chat program on local area network. Two person can send multiple instant messages to each other. Either the chat server or chat client can type the "Quit" message to quit the chatting. You will need to write

a. chatClient.java --- Modified from TCPClient.java

b. chatServer.java --- Modified from TCPServer.java

TCPClient:

import java.io.*;

import java.net.*;

import java.util.*;

class TCPClient {

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

// InputStream from the Keyborad using Scanner

Scanner inFromUser = new Scanner(System.in);

// Create a socket which connect to Server (HOSTNAME, PORTNUM)

String Hostname = "129.252.199.132";

int Port = 25;

Socket clientSocket = null;

try {

clientSocket = new Socket(Hostname, Port);

//InputStream from the Server -- Note scanner can not handle binary data

Scanner inFromServer = new Scanner(clientSocket.getInputStream());

//OutputStream to the Server ---DataOutputStream Class is used to handle binary data

DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());

// get the sever hello message

String serverhello = inFromServer.nextLine();

System.out.println(serverhello);

// Hello back from client

String command = "HELO usca.edu ";

outToServer.writeBytes(command);

System.out.println(command);

serverhello = inFromServer.nextLine();

System.out.println(serverhello);

/*Read data from the user and sent it over the internet

String sentence = inFromUser.nextLine() + " ";

//System.out.println("From Client: " + sentence);

outToServer.writeBytes(sentence);

//Read response from the server and display it on screen

String modifiedSentence = inFromServer.nextLine();

System.out.println("FROM SERVER: " + modifiedSentence);

*/

// Close the connection

clientSocket.close();

} catch (IOException e) {

System.err.print("Caught Exception" + e.getMessage());

} finally{

if (clientSocket !=null ) clientSocket.close(); // no need in java 7 above

}

}

}

TCPServer:

import java.io.*;

import java.net.*;

import java.util.*;

import java.nio.file.Files;

import java.nio.file.Path;

import java.nio.file.Paths;

class TCPServer {

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

String clientSentence = " ";

String serverSentence = " ";

// Create Server Socket

try {

ServerSocket welcomeSocket = new ServerSocket(80);

while (true) {

// create client socket, blocking if no incoming request.

Socket connectionSocket = welcomeSocket.accept();

System.out.println("Accept connection from" + connectionSocket.getRemoteSocketAddress());

// input stream

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

// ouput stream

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

String methods = inFromClient.next();

System.out.println(methods);

String filename = inFromClient.next();

System.out.println("Filename "+filename);

//File Name

//String response="HTTP/1.1 200 OK Content-type: text/html data data";

String statusline = "HTTP/1.1 200 OK ";

String contentType = "Content-type: text/html ";

String contentLength = "Content-Length: 125 ";

//Read the file

Path filetoRead = Paths.get("."+filename);

byte[] buffer = Files.readAllBytes(filetoRead);

//updat contentLength buffer.length

//response

String response = statusline+contentType+contentLength+" ";

outToClient.writeBytes(response);

//body

outToClient.write(buffer, 0, buffer.length);

/* get the message from client

clientSentence = inFromClient.nextLine();

System.out.println("From Client:" + clientSentence);

// construct the response message and send it to client

serverSentence = clientSentence.toUpperCase(); //change to uppercase

outToClient.writeBytes(serverSentence);

*/

// close stream and socket

inFromClient.close();

outToClient.close();

connectionSocket.close();

}

} catch (IOException e) {

System.err.println("Caught Exception " + e.getMessage());

}

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Pro Database Migration To Azure Data Modernization For The Enterprise

Authors: Kevin Kline, Denis McDowell, Dustin Dorsey, Matt Gordon

1st Edition

1484282299, 978-1484282298

More Books

Students also viewed these Databases questions

Question

Does it exceed two pages in length?

Answered: 1 week ago

Question

Does it avoid typos and grammatical errors?

Answered: 1 week ago