• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how to connect to other machine rather than localhost.

 
Ranch Hand
Posts: 1143
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i have a client and server program, i mean client is communicating to the server, but my server is my same system, (localhost)..
but i want to ping other system instead of localhost, to do this i need to change the localhost to that system ip-address but that system is not connected to my system(neither wired nor wireless). but that system has access to internet??
how can i connect or ping to that system..??
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Punit Jain wrote:i have a client and server program, i mean client is communicating to the server, but my server is my same system, (localhost)..
but i want to ping other system instead of localhost, to do this i need to change the localhost to that system ip-address but that system is not connected to my system(neither wired nor wireless). but that system has access to internet??
how can i connect or ping to that system..??



Well, it depends on how the other machine is connected to the internet -- specifically, the machine that will be running the server side application. If the server is directly connected to the internet, then you can just use the ip address. The client machine should have no issues resolving the address, assuming that it is connected (directly or indirectly) to the internet. If the server is not directly connected to the internet, meaning behind a firewall and/or router, and using a NAT server, then ... the client needs to connect to the ip address of the machine (router) that is on the internet; and the router and firewalls has to be configured to forward the needed ports to allow the client to get to the server application.

Henry
 
Punit Jain
Ranch Hand
Posts: 1143
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
here is what i m doing is:

Server.java


Client.java


192.168.1.5 is the client system IP Address, i found this by simply firing the command ipconfig in command prompt.
both the system are connected to the same router.
and i m running both the programs on the server(i mean on my computer), same time in two command prompts.
but it's no working it also giving me an exception in Client.java "java.net.ConnectionException: Connection refused: connect"

but earlier when i am doing this with localhost on same machine it was working.
please let me know what i am doing wrong, i m new in socket programming.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[EDIT] You can pretty much ignore this long diatribe for now. I posted it when I had misunderstood your setup. I'll leave it here though, as it addresses a common "next step" for people new to socket programming. My next post is more relevant to your immediate problem.

When you do new Socket(address, port), that address has to be reachable from your client computer, and the server at that address has to be listening for connections on that port. You Java code to connect and communicate with that server will be identical, regardless of whether it's localhost, or a server on your LAN, or a server on the internet. All that will change is the address. (And note that you can provide either the IP address or the name of the server, such as www.coderanch.com, as long as there's a name service that can look it up.)

Now, if that server is behind a router--like if someone out there on the internet is trying to connect to your home computer--then that router has to be configured to port-forward from its public IP address to the server's LAN address. Note that this will not affect your Java code at all, and neither the server nor the client will know its happening. But the person administering the server has to know about it and make sure the router is configured properly, and then he has to tell the person administering the client the public IP address of the router, not the private IP of the server.

http://en.wikipedia.org/wiki/Port_forwarding
http://en.wikipedia.org/wiki/Private_IP_address

Again, this has nothing to do with Java, and your Java code does not know or care about any of it.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

192.168.1.5 is the client system IP Address
i found this by simply firing the command ipconfig in command prompt.
both the system are connected to the same router.
and i m running both the programs on the server(i mean on my computer), same time in two command prompts.



So that's the IP address of both the client and the server?

And you're starting the server first?

After starting the server what do the following do? (Make sure your server is running for the last two.)

ping localhost
ping 192.168.1.5
telnet localhost 1001
telnet 192.168.1.5 1001

 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know this is speculating -- and apologies for detouring the path a bit.... In the few occasions (that I encountered) where using localhost to connect two applications on the same machine worked, but using the network to connect those same applications on the same machine failed, it was caused by the firewall (on that machine).

Henry
 
Punit Jain
Ranch Hand
Posts: 1143
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


When you do new Socket(address, port), that address has to be reachable from your client computer


but the address is of my client computer.
do i need to run my client.java on client computer??
or both (Server.java and Client.java) on server computer.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Punit Jain wrote:


When you do new Socket(address, port), that address has to be reachable from your client computer


but the address is of my client computer.
do i need to run my client.java on client computer??
or both (Server.java and Client.java) on server computer.



Yes, as I said in my edit, you can ignore that post for now, as it's not relevant to your current problem.

Try the suggestions in my post after that one, and look into what Henry suggested about the firewall. You should find "Windows Firewall" in your Control Panel. If it's on, the simplest thing to do it turn it off completely and try your code again. If that works, then you can turn it back on, and figure out how to configure it to just allow what you need for your program to work.
 
Punit Jain
Ranch Hand
Posts: 1143
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


So that's the IP address of both the client and the server?


not this is only client's ip address my is 192.168.1.3.


And you're starting the server first?



ping localhost
ping 192.168.1.5


no lost of packets.


telnet localhost 1001
telnet 192.168.1.5 1001


both saying: could not open the connection to the host on port 23, connection failed.

so is this a port problem??
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Punit Jain wrote:


So that's the IP address of both the client and the server?


not this is only client's ip address my is 192.168.1.3.



Okay, now you're totally confusing me. I thought you said they were both on the same computer. Now it sounds like you're saying you have two computers on the same LAN. Is that the case?

It also seems like you're mixing up the terms "client" and "server". Previously you said:


which means the server is at IP 1.5.

You also said

192.168.1.5 is the client system IP Address


which means the client is at IP 1.5.

Now you're saying:

this [JV: "this" being IP 1.5] is only client's ip address



and

my [JV: "my" is the server?] is 192.168.1.3.



So now it sounds like the server is at IP 1.3and the client is at IP 1.5. If this is the case, then here's what you need to do.

In the server (192.168.1.3):


In the client (192.168.1.5)


Not that you have to tell the client to connect to the server's IP address. In the code you posted earlier, it looks like the client is trying to connect to its own IP address, which isn't how it works.







telnet localhost 1001
telnet 192.168.1.5 1001


both saying: could not open the connection to the host on port 23, connection failed.



So either the host at 192.168.1.5 did not open a ServerSocket on port 1001, or else, as Henry suggested, the firewall on that host is preventing you from connecting.

HOWEVER I suggested that when I thought the server was at 1.5, whereas not it sounds like the server is at 1.3, so you should try:

ping 192.168.1.3
telnet 192.168.1.3 1001 (when the server is running)
 
Punit Jain
Ranch Hand
Posts: 1143
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
works for me...
thank you very much...
one more thing i just want to know..
in this case both of the system are connected to same router..
but i want to do this, with some other machine which is not connected to this router, only connected to internet..
that time also will it work??

one more think also if other machine is not connected to internet, that time also is it possible???

Thank you...
 
Punit Jain
Ranch Hand
Posts: 1143
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
also i want to know, we have 4 machines connected to same router.
i want to create a application using which we can chat to each other.
how can i do this?
i mean do i need to give ip address of all the machines??
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Punit Jain wrote:works for me...
thank you very much...
one more thing i just want to know..
in this case both of the system are connected to same router..
but i want to do this, with some other machine which is not connected to this router, only connected to internet..
that time also will it work??

one more think also if other machine is not connected to internet, that time also is it possible???

Thank you...



It depends on the specific configuration. If they're two different routers in the same company or school, then you have to talk to the admins about what connectivity there is between the networks in question.

In the general case, though--the case I'm assuming you mean by "only connected to the internet"--the machine that is acting as the server has to either have a public IP address, or be receiving port forwarding from a router that has a public IP address. If that server is your home computer, then, depending on the specific configuration, you may already have a public IP address, or your router may have one (so you can set up port forwarding, if your ISP allows you to accept incoming connections), or you may be completely out of luck.

Click on the links I provided in my earlier post, and look at your router's instruction manual, and talk to your ISP if necessary.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Punit Jain wrote:also i want to know, we have 4 machines connected to same router.
i want to create a application using which we can chat to each other.
how can i do this?
i mean do i need to give ip address of all the machines??



What do you mean "give IP address"?

When you open a connection from A to B, where B is listening on a ServerSocket.accept() call, A must know B's IP address.

So if you're doing peer-to-peer, and any machine can accept a connection from any other, then they'll all have to know each others' respective IP addresses.

On the other hand, if you're doing a more conventional chat architecture, where one machine acts as a server and relays messages between chat participants, then all that's necessary is that you pick one machine as the host, and all the other machines know that host's IP and which port it's listening on.
 
Punit Jain
Ranch Hand
Posts: 1143
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
okay...
one more thing, i tried to connect one system with broadband and disconnect from router..
but now it's not working..
showing me exception.
java.net.connectionException: connection timed out: connect.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Punit Jain wrote:okay...
one more thing, i tried to connect one system with broadband and disconnect from router..



I have no idea what you mean by that.
 
Punit Jain
Ranch Hand
Posts: 1143
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i mean, earlier client and server both the machines are connected to the same router.
but now i disconnected client machine to that router and and directly connected client machine to other broadband.
now it's not working.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Punit Jain wrote:i mean, earlier client and server both the machines are connected to the same router.
but now i disconnected client machine to that router and and directly connected client machine to other broadband.
now it's not working.



It's not clear exactly what your configuration is, so I can't say for sure, but it sounds like you need to set up your router to port-forward to your server.

Again, none of this has anything to do with Java. Your java code will never know or care anything about these details of which host is behind which router, and you won't change your Java code based on the configuration.
 
Punit Jain
Ranch Hand
Posts: 1143
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Again, none of this has anything to do with Java. Your java code will never know or care anything about these details of which host is behind which router, and you won't change your Java code based on the configuration.



okay..thank you..
 
Who knew that furniture could be so violent? Put this tiny ad out there to see what happens:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic