Question

I need help with my IM (instant messaging) java program, I created the Server, Client, and...

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 <port number>");
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 <host name> <port number>");
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);
}
}
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1
//for this we have these employee class
import java.io.*;
import java.util.*;

public class Employee implements Serializable {

   private int employeeNumber;
   private String employeeName;

   Employee(int num, String name) {
      employeeNumber = num;
      employeeName= name;
   }

    public int getEmployeeNumber() {
      return employeeNumber ;
   }

   public void setEmployeeNumber(int num) {
      employeeNumber = num;
   }

   public String getEmployeeName() {
      return employeeName ;
   }

   public void setEmployeeName(String name) {
      employeeName = name;
   }
}

//for client

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

public class Client {

   public static void main(String[] arg) {
      try {
         Employee joe = new Employee(150, "Joe");

         System.out.println("employeeNumber= "
                            + joe .getEmployeeNumber());
         System.out.println("employeeName= "
                            + joe .getEmployeeName());

         Socket socketConnection = new Socket("127.0.0.1", 11111);


         ObjectOutputStream clientOutputStream = new
            ObjectOutputStream(socketConnection.getOutputStream());
         ObjectInputStream clientInputStream = new 
            ObjectInputStream(socketConnection.getInputStream());

         clientOutputStream.writeObject(joe);

         joe= (Employee)clientInputStream.readObject();

         System.out.println("employeeNumber= "
                            + joe .getEmployeeNumber());
         System.out.println("employeeName= "
                            + joe .getEmployeeName());

         clientOutputStream.close();
         clientInputStream.close();

      } catch (Exception e) {System.out.println(e); }
   }
}

//for server

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

public class Server {

   public static void main(String[] arg) {

      Employee employee = null;

      try {

         ServerSocket socketConnection = new ServerSocket(11111);

         System.out.println("Server Waiting");

         Socket pipe = socketConnection.accept();

         ObjectInputStream serverInputStream = new    
            ObjectInputStream(pipe.getInputStream());

         ObjectOutputStream serverOutputStream = new 
            ObjectOutputStream(pipe.getOutputStream());

         employee = (Employee )serverInputStream.readObject();

         employee .setEmployeeNumber(256);
         employee .setEmployeeName("John");

         serverOutputStream.writeObject(employee);

         serverInputStream.close();
         serverOutputStream.close();


      }  catch(Exception e) {System.out.println(e); 
      }
   }

}
Add a comment
Know the answer?
Add Answer to:
I need help with my IM (instant messaging) java program, I created the Server, Client, and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Using java socket programming rewrite the following program to handle multiple clients simultaneously (multi threaded programming)...

    Using java socket programming rewrite the following program to handle multiple clients simultaneously (multi threaded programming) import java.io.*; import java.net.*; public class WelcomeClient { public static void main(String[] args) throws IOException {    if (args.length != 2) { System.err.println( "Usage: java EchoClient <host name> <port number>"); System.exit(1); } String hostName = args[0]; int portNumber = Integer.parseInt(args[1]); try ( Socket kkSocket = new Socket(hostName, portNumber); PrintWriter out = new PrintWriter(kkSocket.getOutputStream(), true); BufferedReader in = new BufferedReader( new InputStreamReader(kkSocket.getInputStream())); ) { BufferedReader...

  • Modify the socket-based date server below so that the server services each client request in a...

    Modify the socket-based date server below so that the server services each client request in a separate thread. import java.net.*; import java.io.*; public class DateClient {   public static void main(String[] args)  {     try {       /* make connection to server socket */       Socket sock = new Socket(“127.0.0.1”,6013);       InputStream in = sock.getInputStream();       BufferedReader bin = new         BufferedReader(new InputStreamReader(in));       /* read the date from the socket */       String line;       while ( (line = bin.readLine()) != null)         System.out.println(line);       /* close the socket connection*/       sock.close();     ...

  • I have to modify a server program and chat program to work as the following instructions...

    I have to modify a server program and chat program to work as the following instructions but I am completely clueless as to where to start. I'd appreciate any help on how to atleast get started. This must be done in java. Diffie-Hellman Two parties use a key agreement protocol to generate identical secret keys for encryption without ever having to transmit the secret key. The protocol works by both parties agreeing on a set of values (a) and (q)....

  • please help. About java tcp. this is what i have so far. how to convert input...

    please help. About java tcp. this is what i have so far. how to convert input value into ASCII sequence For example the message: “Hello World” would become “Ifmmp!Xpsme”For example the message: “Hello World” would become “Ifmmp!Xpsme”in server.java. Client.java: import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintStream; import java.net.Socket; import java.net.SocketTimeoutException; public class Client { public static void main(String[] args) throws IOException {    Socket client = new Socket("127.0.0.1", 20006); client.setSoTimeout(10000);    BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); PrintStream out...

  • Error: Main method not found in class ServiceProvider, please define the main method as: public static...

    Error: Main method not found in class ServiceProvider, please define the main method as: public static void main(String[] args) or a JavaFX application class must extend javafx.application.Application This is the error im getting while executing the following code Can you modify the error import java.net.*; import java.io.*; public class ServiceProvider extends Thread { //initialize socket and input stream private Socket socket = null; private ServerSocket server = null; private DataInputStream in = null; private DataOutputStream out = null; private int...

  • java read integers from this binary file and when the value is 0 then stop reads...

    java read integers from this binary file and when the value is 0 then stop reads and print its summation here is my code import java.io.FileOutputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Random; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.FileInputStream; class Main { public static void main(String[] args) {        String fname = "out.txt";        prepare(fname);               ObjectInputStream inputStream = null;        // create code here    }    public static void prepare(String fname) {   ...

  • The DictionaryClient program featured in the class lecture also used the try-catch when creating a socket....

    The DictionaryClient program featured in the class lecture also used the try-catch when creating a socket. Socket are auto-closeable and thus can use a try-with-resources instead. Edit the dictionary program to use try-with-resources instead. package MySockets; import java.net.*; import java.io.*; public class DictionaryClient {       private static final String SERVER = "dict.org";    private static final int PORT = 2628;    private static final int TIMEOUT = 15000;    public static void main(String[] args) {        Socket   ...

  • My client java a simple client program java .net import java.io*i public class MyClient (public static...

    My client java a simple client program java .net import java.io*i public class MyClient (public static void main(String args() thrown IO Exception (part I: initialize rocket and stream BufferedReader inFromUser - new BufferedReader(new inputStreamReader(System in)); Socket clientSocket - new Socket(___. ___); DataOutputStream oldToServer - new DataOutputStream(clientSocket.getOutputStrea, ())); part 2: interact with server

  • Q7 The following Client side Java code can send a message to the server side via...

    Q7 The following Client side Java code can send a message to the server side via UDP socket. The client side Java code and the server side Java code are running in two different hosts respectively. The server side host name is “MyFileServer”. The server side receives the message and converts all the letters in the message received to uppercase, then sends the modified message back to the client side. Please read the code carefully, and fill out the blanks...

  • The following Client side Java code can send a message to the server side via UDP...

    The following Client side Java code can send a message to the server side via UDP socket. The client side Java code and the server side Java code are running in two different hosts respectively. The server side host name is “MyFileServer”. The server side receives the message and converts all the letters in the message received to uppercase, then sends the modified message back to the client side. Please read the code carefully, and fill out the blanks with...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT