aspose file tools*
The moose likes Sockets and Internet Protocols and the fly likes Sending a message to a remote server Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Sending a message to a remote server" Watch "Sending a message to a remote server" New topic
Author

Sending a message to a remote server

Alex Sinclair
Greenhorn

Joined: Feb 07, 2012
Posts: 7
I've been using Java for a few years now and, until now, I've always felt like I could overcome any obstacle in Java with a little Googling and some trial and error. Recently I decided to give Sockets a try after avoiding learning anything network-related for my entire programming career, but I don't think I can go any further on my own. To state it bluntly, I really have no idea how the internet works insofar as programming is concerned.

Currently all I want to do is to be able to send a message to a server across the internet, have the server process that message, and then have the server send messages back to multiple clients. I'm going to post bits and pieces of code rather than posting everything for now because, at this point, the issue is really just fundamentals. I also think it's important to explain what I hoped the code did. What I have written works fine when I run it locally, but that's probably expected. If nothing looks amiss here I will post the full thing, but like I said, I think the problem is just my understanding of the code rather than the code itself.

Server:

1. I create a server:

2. I then wait for and accept all clients attempting to connect to the server:

3. I obtain some sort of way to read messages from the client (a thread is started for each client by this point):

4. I read messages from the cilent and process them in some way:

5. I obtain some way to write message to the client:

6. I write a message to the client:


Client:

1. I connect to a server:

2. I obtain some way to write messages to the server:

3. I write a message to the server:

4. I obtain some sort of way to read messages from the server:

5. I read messages from the server and process them in some way:


When creating the Socket for the server in the client, the host I use is the IPv4 address I get when I run ipconfig. I used port 1982. I don't really know what either of those mean, but it worked in testing so I ran with it. I also obtain the same address when I run:



When I give that address to a friend across the country running the client, however, absolutely nothing works. I guess I sort of expected some wizardry to take over and for everything to work out fine by sharing a single IP address, but now I'm back at square one and hours and hours of Googling haven't lead me any closer to an answer. Any help would be appreciated.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Alex Sinclair wrote:I've been using Java for a few years now and, until now, I've always felt like I could overcome any obstacle in Java with a little Googling and some trial and error.


I strongly suggest you google for something like java networking tutorial, and work through at least one of them very carefully, and probably get a good networking book too.

Currently all I want to do is to be able to send a message to a server across the internet, have the server process that message,


Server has to be running, listening on an IP address that you can reach--either a public IP address or one that's been port-forwarded--and on a port known to you. You open a socket to that server and port. Server has to know how to interpret the message you send it. You send the message, server receives and processes it.

and then have the server send messages back to multiple clients.


Then either those clients all have to already be connected to the server, or the server has to be able to open a connection to them, meaning they have to have public or port-forwarded IPs, and they have to be listening on ports known to the server.

I'm going to post bits and pieces of code rather


You'll have a better chance of getting people to look at it if you post an SSCCE.

When creating the Socket for the server in the client, the host I use is the IPv4 address I get when I run ipconfig. I used port 1982. I don't really know what either of those mean,


You shouldn't proceed any further then until you do some research and find out.

When I give that address to a friend across the country running the client, however, absolutely nothing works.


private ip address
port forwarding
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

Alex Sinclair wrote:
When creating the Socket for the server in the client, the host I use is the IPv4 address I get when I run ipconfig. I used port 1982. I don't really know what either of those mean, but it worked in testing so I ran with it. I also obtain the same address when I run:



When I give that address to a friend across the country running the client, however, absolutely nothing works. I guess I sort of expected some wizardry to take over and for everything to work out fine by sharing a single IP address, but now I'm back at square one and hours and hours of Googling haven't lead me any closer to an answer. Any help would be appreciated.


One thing to understand, just because you can get to the internet from a machine, it does not mean that the machine is on the internet. For example, my home has two computers, two laptops, two game consoles, and a whole bunch of devices, including cell phones that can surf the internet. However, none of those machines can be reached from the internet. The only machine that is on the internet is the wifi router that is connected to the cable modem. It is the only device that is actually on the internet. All the other devices in the home uses a private IP address that is connected to the router. None of these devices can be seen from the internet.


So, if the machine (with the serversocket application) is running a private !P address, it won't work. You need to have the client connect to the machine that is actually on the internet, then you need to configure that machine to route the port to the machine that you want it to go (via it's private address).

[EDIT: darn. beaten to the answer by three minutes]

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Alex Sinclair
Greenhorn

Joined: Feb 07, 2012
Posts: 7
Thanks for the replies.

I've looked into port forwarding in Java, but unfortunately I haven't been able to find a clear example for beginners.

Henry, you said that the machine needs to be configured. By this do you mean that user running the client needs to manually change system settings in some way? I was hoping the setup between the person running the server and the person running the client would be a lot easier than that.

I'll explain what I'm trying to do in case there is an easier way that I have not considered. In essence, I want to create an application that would function similarly to a chat room, only instead of chatting, users are simply toggling a busy/available status that is displayed to everyone in the chat room. For example, one user might press a hotkey to toggle into "busy" mode, and then all the other users would receive a message saying that said user is now busy and processes it accordingly. Ideally, the setup for this "chatroom" would be as simple as possible. I was hoping one person could simply host a server, distribute his IP address, and then users could connect to it, like you might when hosting your own Ventrilo server.

If there's no better way than the way that I'm currently not understanding, then I'm afraid I might be in trouble.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Alex Sinclair wrote:Thanks for the replies.

I've looked into port forwarding in Java, but unfortunately I haven't been able to find a clear example for beginners.


Port forwarding has nothing to do with Java. Your code will never know or care that it's happening. Port forwarding means, roughly, that you configure your router (which has a public IP address) to pass on connection requests and data from one or more of its ports to a port on a box on your LAN (which does not have a public IP address). I provided a link to the Wikipedia page, and I'm sure you can find plenty more info out there.

Henry, you said that the machine needs to be configured. By this do you mean that user running the client needs to manually change system settings in some way? I was hoping the setup between the person running the server and the person running the client would be a lot easier than that.


He's talking about configuring the router (or whatever machine actually faces the public internet) to do port forwarding, as I described above.

I'll explain what I'm trying to do in case there is an easier way that I have not considered. In essence, I want to create an application that would function similarly to a chat room, only instead of chatting, users are simply toggling a busy/available status that is displayed to everyone in the chat room. For example, one user might press a hotkey to toggle into "busy" mode, and then all the other users would receive a message saying that said user is now busy and processes it accordingly. Ideally, the setup for this "chatroom" would be as simple as possible. I was hoping one person could simply host a server, distribute his IP address, and then users could connect to it, like you might when hosting your own Ventrilo server.


There has to be at least one host with a public IP. The easiest way to make this work, though not necessarily the easiest way to code it, is to have one central server with a public IP address, and then all the clients connect to it. That server acts as a broker between any clients that want to communicate. This is how all the main instant messenger clients work. This way, only one host has to have a public IP. If you want the clients to connect directly to each other (easier to code, maybe, but harder to set up), then every single one of them has to have a public IP address, or receive port forwarding from their router or other local box that does.
Alex Sinclair
Greenhorn

Joined: Feb 07, 2012
Posts: 7
I suppose what I'm looking for just isn't possible, as there appears to be more user setup than I would like. For instance, I do not know the login information for my router, and I doubt this would be an uncommon problem. I could probably dig it up, but I barely want to do that - why would anyone else?

Anyway, thanks for the help. At least I learned something through all of this.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Alex Sinclair wrote:I suppose what I'm looking for just isn't possible, as there appears to be more user setup than I would like. For instance, I do not know the login information for my router, and I doubt this would be an uncommon problem. I could probably dig it up, but I barely want to do that - why would anyone else?


If you do it, and you use the single central server model I described, then you can be the server, and nobody else has to do it.
Alex Sinclair
Greenhorn

Joined: Feb 07, 2012
Posts: 7
I have been creating it with one server in mind from the start, so that's good.

I found my router information and forwarded a port. When I try to connect to my server from a client on the same machine using the external IP, I get a ConnectException. I've read that some routers won't let the internal network to connect to the external IP of the router. I sent the client to a friend and she was not able to connect, but http://www.canyouseeme.org/ says that it can see my service. Is there an easy way to test the connection on one machine? I can test all the other functionality by using localhost.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

If canyouseeme is reporting success, then you have a server running, and there is a network path from the outside world to that server.

So if your friend is "not able to connect", then it's one of 3 things:

1) You friend can connect, but there's a problem with your server, so that, although the connection is established, it's not handling its connection to the client properly.

2) There's something wrong with the client your friend is using, so that, although it can establish a connection, it's not handling its connection to the server properly.

3) Your friend is behind a firewall or has some other such network restriction such that she actually cannot establish a connection.

Since you didn't provide any details about what "not able to connect" means--such as exactly how she tried to connect and exactly what error she received--it's impossible to say more.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Sending a message to a remote server