Client and Server Side (java)

  • remon479
  • Novice
  • Novice
  • remon479
  • Posts: 19

Post 3+ Months Ago

Hi all, I need some help in trying to run server and client side on two different computers. I have configured the two programs (Client.java , Server.java)so they are able to run on the same machine using two command windows. So one command is running java Server and the other one running java Client.

Basically, I need to change line of code (Line 9) in the Client class so I am able to run the Client on one computer and the server on another. Am i right by assuming that i have to change local host to something like remotehost?.. so the client and server side are able to communciate while they are operating in two different computers..

Code: [ Select ]
class Client
{
    
    public static void main(String[] args)
    {
     try
     {

      Socket s = new Socket("localhost", 5000);
      System.out.println("Obtained port: " + s.getLocalPort());
        DataInputStream j = new DataInputStream(s.getInputStream());
      String k = j.readUTF();
      System.out.println(k);
      DataOutputStream o = new DataOutputStream(s.getOutputStream());
      String m = new String("How are you?");
      o.writeUTF(m);
      DataInputStream i = new DataInputStream(s.getInputStream());
      System.out.println(i.readUTF());
      o.close(); s.close(); i.close();
     }
     catch (Exception e) {}
    }
}
  1. class Client
  2. {
  3.     
  4.     public static void main(String[] args)
  5.     {
  6.      try
  7.      {
  8.       Socket s = new Socket("localhost", 5000);
  9.       System.out.println("Obtained port: " + s.getLocalPort());
  10.         DataInputStream j = new DataInputStream(s.getInputStream());
  11.       String k = j.readUTF();
  12.       System.out.println(k);
  13.       DataOutputStream o = new DataOutputStream(s.getOutputStream());
  14.       String m = new String("How are you?");
  15.       o.writeUTF(m);
  16.       DataInputStream i = new DataInputStream(s.getInputStream());
  17.       System.out.println(i.readUTF());
  18.       o.close(); s.close(); i.close();
  19.      }
  20.      catch (Exception e) {}
  21.     }
  22. }


Code: [ Select ]
class Server
{
    public static void main(String[] args)
    {
     try
     {
      ServerSocket s = new ServerSocket(5000);
      Socket temp = s.accept();
      DataOutputStream p = new DataOutputStream(temp.getOutputStream());
      String k = new String("START");
      p.writeUTF(k);
      DataInputStream i = new DataInputStream(temp.getInputStream());
      String m = i.readUTF();
        System.out.println(m);
        DataOutputStream o = new DataOutputStream(temp.getOutputStream());
        o.writeUTF("Reasonably good");
      i.close(); temp.close(); s.close(); o.close();
     }
     
      catch (Exception e) {}
    }
}
  1. class Server
  2. {
  3.     public static void main(String[] args)
  4.     {
  5.      try
  6.      {
  7.       ServerSocket s = new ServerSocket(5000);
  8.       Socket temp = s.accept();
  9.       DataOutputStream p = new DataOutputStream(temp.getOutputStream());
  10.       String k = new String("START");
  11.       p.writeUTF(k);
  12.       DataInputStream i = new DataInputStream(temp.getInputStream());
  13.       String m = i.readUTF();
  14.         System.out.println(m);
  15.         DataOutputStream o = new DataOutputStream(temp.getOutputStream());
  16.         o.writeUTF("Reasonably good");
  17.       i.close(); temp.close(); s.close(); o.close();
  18.      }
  19.      
  20.       catch (Exception e) {}
  21.     }
  22. }



Looking forward to your replies :)
  • PolishHurricane
  • Mastermind
  • Mastermind
  • User avatar
  • Posts: 1585

Post 3+ Months Ago

Yes, if those 2 programs work, you need to change localhost to the IP address of the server computer. Also, since the server is already listening on port 5000 and the client is connecting to 5000, you do not need to change that number
  • remon479
  • Novice
  • Novice
  • remon479
  • Posts: 19

Post 3+ Months Ago

Should have posted saying that i resolved the problem.

You are correct, thanks anyway :)
  • PolishHurricane
  • Mastermind
  • Mastermind
  • User avatar
  • Posts: 1585

Post 3+ Months Ago

It's no big deal, took 2 seconds of my time only. :)

Post Information

  • Total Posts in this topic: 4 posts
  • Users browsing this forum: No registered users and 58 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.