Link to home
Start Free TrialLog in
Avatar of dr_country
dr_country

asked on

Java socket code to bypass routers

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);
Avatar of Mick Barry
Mick Barry
Flag of Australia image

What errors are you getting, may be a firewall blocking that port.
If check your firewall configuration and use an open port.
Avatar of dr_country
dr_country

ASKER

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.
>> 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?)
Yeah, there's not difference between wired and wireless in socket programming. it must be a configuration issue.
can you ping the machine your are connecting to?
What about telnetting on the port?
Do you mean no messages are sent? Or the large ones for example?
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?
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.
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.
>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.
It has already been stated that the connection was made.
>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.
For a connection to reset it must first have been made musn't it?
>> 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.
My apologies for preferring not to make assumptions and going on the facts at hand.
And please refrain from making personal remarks.
> 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.
>And please refrain from making personal remarks.
Where exactly personal remarks?
I thought it was you that once suggested reading https://www.experts-exchange.com/help.jsp#hi64 but it seems that you keep violating this rule as many others (especially https://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.
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.

aozarov,

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

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!
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?
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.
"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.
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?

No, the other end just hangs like nothing ever attempted to connect.
are you sure you are connecting to the right thing :)
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.
SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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 :)