• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 752
  • Last Modified:

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);
0
dr_country
Asked:
dr_country
  • 12
  • 8
  • 6
  • +2
2 Solutions
 
objectsCommented:
What errors are you getting, may be a firewall blocking that port.
If check your firewall configuration and use an open port.
0
 
dr_countryAuthor Commented:
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
 
aozarovCommented:
>> 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
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
limaidealCommented:
Yeah, there's not difference between wired and wireless in socket programming. it must be a configuration issue.
0
 
objectsCommented:
can you ping the machine your are connecting to?
What about telnetting on the port?
0
 
cincin77Commented:
Do you mean no messages are sent? Or the large ones for example?
0
 
dr_countryAuthor Commented:
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
 
aozarovCommented:
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
 
objectsCommented:
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
 
aozarovCommented:
>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
 
objectsCommented:
It has already been stated that the connection was made.
0
 
aozarovCommented:
>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
 
objectsCommented:
For a connection to reset it must first have been made musn't it?
0
 
aozarovCommented:
>> 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
 
objectsCommented:
My apologies for preferring not to make assumptions and going on the facts at hand.
And please refrain from making personal remarks.
0
 
objectsCommented:
> 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
 
aozarovCommented:
>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
 
objectsCommented:
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
 
objectsCommented:
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
 
dr_countryAuthor Commented:
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
 
dr_countryAuthor Commented:
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
 
dr_countryAuthor Commented:
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
 
cincin77Commented:
"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
 
objectsCommented:
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
 
dr_countryAuthor Commented:
No, the other end just hangs like nothing ever attempted to connect.
0
 
objectsCommented:
are you sure you are connecting to the right thing :)
0
 
dr_countryAuthor Commented:
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
 
objectsCommented:
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
 
cincin77Commented:
hmm..
You should buy a static IP from your Service Provider or you should have a VPN to your server.

regards.
0
 
dr_countryAuthor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

  • 12
  • 8
  • 6
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now