Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

question regarding server socket if server socket is other machine

 
rakesh kadulkar
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am trying to create a simple chat application that will run on two instances of command promp.
If I run the program on single machine there is o problem, it works fine.

Now he problem is I am in a LAN enviroment.

Suppose there are two machines and i want one machine to run server socket and other to run client socket.

Now for server socket there is no issue as I will simple create a server socket using \



Now the ServerSocket will run on one machine.

But now the problem is i want to connect to this server socket using other machine.

So how can I achieve this. Because there is no method provided in the InetAddress class where I can eplicitly pass the Ip address of the machine that is running server socket.


"Also I nees to know what is the meaning of Raw IP address "
 
Rob Spoor
Sheriff
Pie
Posts: 20526
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure there is - the static getByXXX methods.
 
rakesh kadulkar
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes the problem is solved but as such there is no static getByXXX method which directly takes the IP
address of some other machine. We have to take the IP address in a byte array and then pass this byte array in

InetAddress.getByAddress(byteArray)

Now there is no issue till now but there will be when I will start enhancing that code with a proper GUI
 
Rob Spoor
Sheriff
Pie
Posts: 20526
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
getByName also accepts IP addresses in String format.
 
rakesh kadulkar
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well that is correct.
The method signature confused me.
It says getByName(String host). I thought ther emust be a name assigned to the host.

Well now it works fine .
Thanks you.
 
Paul Santa Maria
Ranch Hand
Posts: 236
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Type in these two examples:

Simple Java.net "Hello World"

There is absolutely no practical difference if you ran the client and server in two different windows ... or on two completely different machines.

And as you also see, you pass the remote hostname (or IP address - it doesn't matter) from the command line when you invoke the program.

Instead of a command line argument from "main (String[] args)", you could, of course just as easily read the server name from a GUI: it doesn't matter.

Finally "raw IP" (in the context I believe you're asking), is simply the numeric ID (e.g. "127.0.01"), instead of a meaningful hostname (e.g. "localhost").

'Hope that helps .. PSM
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic