Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help with my IM (instant messaging) java program, I created the Server, Client, and Message class. I somehow can't get both the server

I need help with my IM (instant messaging) java program, I created the Server, Client, and Message class. I somehow can't get both the server and client to message to each other. I am at a roadblock. Here is the question below.

Create an IM (instant messaging) java program so that client and server communicate via serialized objects (e.g. of type Message). Each Message object encapsulates the name of the sender and the response typed by the sender. You may assume that the names are provided as command-line arguments to the client and server respectively.

------------------------------------------------------------------------------------------

import java.io.Serializable;

public class Message implements Serializable { // instance variables - replace the example below with your own private String name; private String response; public Message () { this.name = null; this.response = null; } public Message(String name, String response) { this.name = name; this.response = response; }

public String getName() { return name; } public String getResponse() { return response; } public void setName(String name) { this.name = name; } public void setResponse(String response){ this.response = response; } }

import java.net.*; import java.io.*;

public class Server { public static void main(String[] args) throws IOException, ClassNotFoundException{ if (args.length != 1) { System.err.println("Usage: java Server "); System.exit(1); }

int portNumber = Integer.parseInt(args[0]);

try ( ServerSocket serverSocket = new ServerSocket(portNumber); Socket clientSocket = serverSocket.accept(); ObjectOutputStream out = new ObjectOutputStream(clientSocket.getOutputStream()); ObjectInputStream in = new ObjectInputStream(clientSocket.getInputStream()); ) { BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in)); Message inMessage, outMessage; String name; String inResponse; String outResponse; System.out.println("Connection Established"); while ((inMessage = (Message) in.readObject()) != null) {

System.out.println(inMessage.getName() + " " + inMessage.getResponse()); outResponse = stdIn.readLine(); System.out.println("Server: " + outResponse); if (outResponse != null) { System.out.println("Server: " + outResponse); out.writeObject(new Message("Server: ", outResponse)); out.flush(); } } } catch (IOException e) { System.out.println("Exception caught when trying to listen on port " + portNumber + " or listening for a connection"); System.out.println(e.getMessage()); } } }

import java.io.*; import java.net.*;

public class Client { public static void main(String[] args) throws IOException, ClassNotFoundException { if (args.length != 2) { System.err.println( "Usage: java EchoClient "); System.exit(1); }

String hostName = args[0]; int portNumber = Integer.parseInt(args[1]);

try ( Socket kkSocket = new Socket(hostName, portNumber); ObjectOutputStream out = new ObjectOutputStream(kkSocket.getOutputStream()); ObjectInputStream in = new ObjectInputStream(kkSocket.getInputStream()); ) { BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in)); Message message = new Message(); Message inMessage; String name; String inResponse; String OutResponse; System.out.println("Connection Established"); while (((inMessage = (Message) in.readObject()) != null)) { System.out.println("Connection Established"); System.out.println(inMessage.getName() + inMessage.getResponse()); OutResponse = stdIn.readLine(); out.writeObject(new Message("Client: ", OutResponse)); } } catch (UnknownHostException e) { System.err.println("Don't know about host " + hostName); System.exit(1); } catch (IOException e) { System.err.println("Couldn't get I/O for the connection to " + hostName); System.exit(1); } } }

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

Students also viewed these Databases questions

Question

b. Is the government supportive of the economic changes occurring?

Answered: 1 week ago