?
Solved

Java socket code to bypass routers

Posted on 2005-05-11
30
Medium Priority
?
674 Views
Last Modified: 2008-01-09
Hello, I am kind of new to java programming. I recently wrote a small chat program that works with wired connections from PC to PC. But when I tried it using a wireless connection I believe my router(linksys) would not allow me to do so, even if I was connected to with a wire to the router the same problem. Is there any specific code in Java to eliminate this problem?

Here's what I have to do the connections:

ServerSocket       ssock;
       Socket            client;

//server side
ssock  = new ServerSocket(sock_num);
                  client = ssock.accept();

//client side
client = new Socket(IP,sock_num);
0
Comment
Question by:dr_country
  • 12
  • 8
  • 6
  • +2
30 Comments
 
LVL 92

Expert Comment

by:objects
ID: 13983111
What errors are you getting, may be a firewall blocking that port.
If check your firewall configuration and use an open port.
0
 
LVL 1

Author Comment

by:dr_country
ID: 13983186
Yes, I've disabled my firewall and also added this port as allowed on both sides. I've even tried placing my router and my own PC in the DMZ.
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13983377
>> I've even tried placing my router and my own PC in the DMZ.
Make sure that your server side is in the DMZ but if both of your comptures are part of the linksys network then there isn't need for
that. Can you provide the stackTrace from your exceptions? Do you run the server before the client (so it will bind and wait before
any clients connection?)
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Expert Comment

by:limaideal
ID: 13983662
Yeah, there's not difference between wired and wireless in socket programming. it must be a configuration issue.
0
 
LVL 92

Expert Comment

by:objects
ID: 13983785
can you ping the machine your are connecting to?
What about telnetting on the port?
0
 
LVL 3

Expert Comment

by:cincin77
ID: 13984295
Do you mean no messages are sent? Or the large ones for example?
0
 
LVL 1

Author Comment

by:dr_country
ID: 13990251
I run the server before the client, but the connection resets when I try to send to the server an object stream. Yes, I can telnet and ping from one PC to another. Another error I get is null, but I haven't tried using the printStackTrace exception which I will try out now. Thanks aozarov. The only thing is I don't really know what you mean by if the server will bind and wait before any clients connection, could you show me a piece of code that does this?
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13992428
see this: http://java.sun.com/docs/books/tutorial/networking/sockets/clientServer.html
You want to make sure that the server created the ServerSocket before the client created its Socket
othewise you will loose that call. If you are using Object streams it is advicable to create first the ObjectOutputStream and then
the ObjectInputStream.
0
 
LVL 92

Expert Comment

by:objects
ID: 13992438
The server would appear to already being created before the client, otherwise no connection would be made.

> Another error I get is null

Sounds like maybe a NullPointerException somewhere, check the line it occurs on to see if you are referencing a null variable.
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13992459
>otherwise no connection would be made.
I will not be surprised that this is actually the case as well as trying to get from the client Socket (which is null as the constructor
failed) an OuputStream which leads to the NullPointerException.
0
 
LVL 92

Expert Comment

by:objects
ID: 13992500
It has already been stated that the connection was made.
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13992524
>It has already been stated that the connection was made.
The only thing which come close to that is "but the connection resets when I try to send to the server an object stream" which again leads
me to the same theory.
0
 
LVL 92

Expert Comment

by:objects
ID: 13992539
For a connection to reset it must first have been made musn't it?
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13992565
>> For a connection to reset it must first have been made musn't it?
Well, I think (and I can see that you don't) that the asker confuse terms and uses "reset" for the case where new Socket(....) failed (or maybe that
he is not aware that this is the point of failure). Adding the stackTrace as I suggested can clear this out pretty easy.
0
 
LVL 92

Expert Comment

by:objects
ID: 13992596
My apologies for preferring not to make assumptions and going on the facts at hand.
And please refrain from making personal remarks.
0
 
LVL 92

Expert Comment

by:objects
ID: 13992607
> I run the server before the client, but the connection resets when I try to send to the server an object stream.

Is there any error occurring on the server when this occurs? (Make sure you aren't ignoring any exceptions)
If so, please post the stack trace for that error.
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13992705
>And please refrain from making personal remarks.
Where exactly personal remarks?
I thought it was you that once suggested reading http://www.experts-exchange.com/help.jsp#hi64 but it seems that you keep violating this rule as many others (especially http://www.experts-exchange.com/help.jsp#hi241). This doesn't matter as I am sure you will be happy to hear that I no longer desire to participate in this site (except of answering any open questions I have) as you and CEHJ make my experience in this site very unpleasant. I wish you both all the best with your pursuit for the precious E.E points.
0
 
LVL 92

Expert Comment

by:objects
ID: 13992783
Where exactly do you feel I have violated the rules? Please accept my apology if I have made your experience unpleasant, it was never my intention. I will say though that sometimes your comments can come across as being a little aggressive, which may have been a misinterpretation and I apologise if so. Thats one of the problems of this form of low bandwidth communication.
Can't speak for CEHJ but I can say he does his best to try and make my life difficult on EE as well :(
As for points, they are worth nothing and in no way drive my participation here, I'm here to help peple solve there problems. Unfortunately there are some who are driven by points at this site (typically ones that are the ones always complaining about points distribution) but theres not much we can do about that.

0
 
LVL 92

Expert Comment

by:objects
ID: 13992855
aozarov,

This is not really the place for this discussion so feel free to email me directly if you want to discuss this further :)

0
 
LVL 1

Author Comment

by:dr_country
ID: 13997007
Hey guys, I really appreciate your interest in trying to help me out with this issue. I still haven't had a chance to print the stack trace in the connection block, but once I have a chance I'm sure I'll come back with a more specific question.

Cheers!
0
 
LVL 1

Author Comment

by:dr_country
ID: 14007668
Hello everyone, this time I tried to connect from a public wireless network to a DSL. I had the server side running on the PC with DSL and I ran the client side from the public wireless network. Still no connection...

Here's the output from the printStackTrace:

Error: Connection reset
java.net.SocketException: Connection reset
        at java.net.SocketInputStream.read(Unknown Source)
        at java.io.ObjectInputStream$PeekInputStream.read(Unkown Source)
        at java.io.ObjectInputStream$PeekInputStream.readFully(Unkown Source)
        at java.io.ObjectInputStream$BlockDataInputStream.readShort(Unkown Source)

        at java.io.ObjectInputStream.readStreamHeader(Unkown Source)
        at java.io.ObjectInputStream.<init>(Unknown Source)
        at SecureChat.<init>(SecureChat.java:257)
        at SecureChat.main(SecureChat.java:394)

Line 257:          ObjectInputStream ois = new ObjectInputStream(client.getInputStream());
Line 394:          is in a catch statement, the last line of code in main

Once again, I am trying to establish a connection, from wireless to wireless, or wired to wireless with/without linksys router. It works for 2 PCs that are connected to their corresponding ISPs directly through their modem. Is there any code in java that can bypass this?
0
 
LVL 1

Author Comment

by:dr_country
ID: 14007709
By the way, Objects, if you are still in this thread. Please let me know how you do it to get credit for your solutions. I've solved 2 problems but I did not get any reply and I still have 0 answered questions. Thanks.
0
 
LVL 3

Expert Comment

by:cincin77
ID: 14008231
"Connection Reset" means that you try to connect to a wrong port on the Server. There is no application that listens to the port you try to connect to. Or there may be  a firewall on the server that does not let you connect. The firewall may drop your packets or may send a Reset.
0
 
LVL 92

Expert Comment

by:objects
ID: 14008666
It looks like you are getting a connection, and the other end is closing the connection.
Are there any exceptions occurring at the other end?

0
 
LVL 1

Author Comment

by:dr_country
ID: 14015783
No, the other end just hangs like nothing ever attempted to connect.
0
 
LVL 92

Expert Comment

by:objects
ID: 14015799
are you sure you are connecting to the right thing :)
0
 
LVL 1

Author Comment

by:dr_country
ID: 14015987
Well, it is possible. There is the gateway IP address and then the actual IP address of the host. I try connecting to the host. This is where I think I'm wrong because it is a DSL connection and other computers in the vicinity, that have DSL by the same provider, have the same IP address. I have to somehow connect through the gateway and then have it route to my target IP address.
0
 
LVL 92

Assisted Solution

by:objects
objects earned 150 total points
ID: 14016169
Yes it sounds more like a network problem than something to do with your code.
What it sounds like is the connection is being made to something other than your app, and whatever that is disconnects you.
0
 
LVL 3

Accepted Solution

by:
cincin77 earned 150 total points
ID: 14016609
hmm..
You should buy a static IP from your Service Provider or you should have a VPN to your server.

regards.
0
 
LVL 1

Author Comment

by:dr_country
ID: 14020318
Yes, this was my question in the first place. Since the router sends me back an error message(disconnects/resets) I cannot get to the target host to actually connect to my app. I wanted to know if there was some solid code that could catch this and deal with it appropriately. Oh well, can't have everything. Thanks for your the help guys :)
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month14 days, 2 hours left to enroll

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question