473,396 Members | 2,024 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

remote socket communication

4
I'm trying to learn how to use sockets in java. I found a great tutorial at: http://java.sun.com/docs/books/tutorial/networking/sockets/clientServer.html

I was able to get the KnockKnockServer and Client classes they provide there to work between two processes on one machine. However, I am unable to get it to function between different machines over the internet. I have tried using the various different constructors of both the ServerSocket and the Socket classes to get it to work but it still only works when they're running on the same machine. I have tried using the javax.net.ServerSocketFactory and the javax.net.SocketFactory as well to no avail. I think the problem might be solved if I was able to use an instance of the socket class other than the default PlainSocketImpl but I'm not sure how to do that. thanks for any help.
Jul 8 '08 #1
11 2790
Nepomuk
3,112 Expert 2GB
I'm trying to learn how to use sockets in java. I found a great tutorial at: http://java.sun.com/docs/books/tutor...entServer.html
...
Hi neoy! Welcome to bytes.com!
I haven't had a closer look at that tutorial (at least not lately - I think I did read it quite some time ago and will have another look at it soon, but not now), but just to offer you another source, here's a little tutorial on sockets that I'm writing myself. It's still incomplete, as I have been quite busy with other tasks, but nevertheless it might help.

I find, that often the call of these constructors can be wrong in a way you wouldn't necessarily realize, so why don't you post the calls you tried? (I'm thinking client side, but maybe you should post both client and server calls.)

Also, make sure, that the ports you're using aren't being blocked by a router or similar.

Greetings,
Nepomuk
Jul 9 '08 #2
JosAH
11,448 Expert 8TB
There's no need to use those fuzzy classes, all you need are the Socket
and ServerSocket classes. You do need to turn off your firewall.

kind regards,

Jos
Jul 9 '08 #3
Nepomuk
3,112 Expert 2GB
You do need to turn off your firewall.
NO! Do NOT turn of your firewall! Just enable Port forwarding and allow your client to access the internet. Switching the firewall off is a very big risk and should not be attempted while connected to the internet.

Greetings,
Nepomuk
Jul 9 '08 #4
JosAH
11,448 Expert 8TB
NO! Do NOT turn of your firewall! Just enable Port forwarding and allow your client to access the internet. Switching the firewall off is a very big risk and should not be attempted while connected to the internet.
So has spoken the MS Windows user ;-)

kind regards,

Jos (<--- Linux and ipchains)
Jul 9 '08 #5
Nepomuk
3,112 Expert 2GB
So has spoken the MS Windows user ;-)

kind regards,

Jos (<--- Linux and ipchains)
Actually, so has spoken the Linux user, who expects that the OP probably uses MS Windows.

As "turn of the firewall" doesn't make much sense in Linux (ok, there's firestarter and similar, but software that would match the common use of "firewall" is hardly used in Linux), it must be assumed, that the OP, had he/she followed your advice, would just shut down the software firewall (e.g. Windows Protector, ZoneAlarm, Norton Personal Firewall,...) and would have possibly been nearly completely unprotected.

Greetings,
Nepomuk (<--- Linux, iptables via guarddog, hardware firewall)
Jul 9 '08 #6
neoy
4
Thanks for your responses. To clarify some things I am running a PC with windows XP and a laptop with slax (linux distro).

My code basically looks like this:
server side:
Expand|Select|Wrap|Line Numbers
  1. port = 4321;
  2. Socket socket = new ServerSocket(port).accept();
  3. //get streams and do io
  4.  
client side:
Expand|Select|Wrap|Line Numbers
  1. port = 4321;
  2. //replace "0.0.0.0" with ip address of host computer.
  3. Socket socket = new ServerSocket("0.0.0.0", port);
  4. //get streams and do io
  5.  
Part of my problem, I believe, may have to do with what Nepomuk said:
Also, make sure, that the ports you're using aren't being blocked by a router or similar.
I noticed that my laptop is telling me my ip address is one thing, while a site like "what's my ip" is saying something else. Even when I tried connecting directly through an ethernet cable instead of a wireless router this is still the case. My PC does not have this same problem, and it's connected the same way my laptop is now. I'd think that this would only be a problem when the laptop is the host, but it doesn't work either way.

The error it's giving me every time is:
java.net.ConnectException: connection timed out.
like I said in my first post, this is just when I try to connect over the internet. It works everytime when Client and Server are run from the same computer.

I'm not sure how exactly to enable port forwarding. I have java as one of the exceptions to allow on the windows firewall, and I added the port I was using as well, but I'm not sure what to put as the name when I add a port to allow. But even so, (temporarily) disabling the firewall did not work either.

If you have any suggestions for how I could independently test the server and the client side, that would be helpful as well.
Jul 9 '08 #7
Nepomuk
3,112 Expert 2GB
Hi again!
Thanks for your responses.
You're welcome! :-)
To clarify some things I am running a PC with windows XP and a laptop with slax (linux distro).
Interesting combination! :-D
I tried slax myself a while ago, but decided to stick with other distributions. (Not that that was relevant in any way for this thread. ^^)
My code basically looks like this:
server side:
Expand|Select|Wrap|Line Numbers
  1. port = 4321;
  2. Socket socket = new ServerSocket(port).accept();
  3. //get streams and do io
  4.  
client side:
Expand|Select|Wrap|Line Numbers
  1. port = 4321;
  2. //replace "0.0.0.0" with ip address of host computer.
  3. Socket socket = new ServerSocket("0.0.0.0", port);
  4. //get streams and do io
  5.  
I'm sure, that's not a ServerSocket, is it? It should be a normal Socket. And of course, the IP isn't "0.0.0.0", but I guess you just put that there for us.
I noticed that my laptop is telling me my ip address is one thing, while a site like "what's my ip" is saying something else. Even when I tried connecting directly through an ethernet cable instead of a wireless router this is still the case. My PC does not have this same problem, and it's connected the same way my laptop is now. I'd think that this would only be a problem when the laptop is the host, but it doesn't work either way.

The error it's giving me every time is:
java.net.ConnectException: connection timed out.
like I said in my first post, this is just when I try to connect over the internet. It works everytime when Client and Server are run from the same computer.
You might be using the wrong IP - the error "java.net.ConnectException: connection timed out" suggests that. Check the IPs of your computers (both of them!) with a site like www.whatismyip.com and compare them - if they are identical, then the problem is this: The router is making the connection, right? You have to activate port forwarding.
I'm not sure how exactly to enable port forwarding.
That's a router thing. (Check the routers manual for that.) If the computers make the connections themselves and not the router (even if they make it through the router), it's not necessary.

Now, if the IPs are different, make sure you're using those given by the site.
I have java as one of the exceptions to allow on the windows firewall, and I added the port I was using as well, but I'm not sure what to put as the name when I add a port to allow.
The name should be irrelevant - you could call it "Porks pie" or "Mr. Spock" or anything else you fancy.

If you've activated iptables on Slax (Slackware being one rare exception in the Linux world to the "all ports are blocked as a standard" rule and Slax being a slightly modified version of Slackware), you'll probably have to allow that port on your laptop too, when using it as the server. Check http://www.netfilter.org to find out, how to do that.
If you have any suggestions for how I could independently test the server and the client side, that would be helpful as well.
Test the computers using the IPs from www.whatismyip.com instead of 127.0.0.1 or localhost or the IP given by the computers themselves (which I guess you were using until now) while running server and client on the same system. That way, the connection will go from the client into the internet and back to the server (which is running on the same computer as the client).

Greetings,
Nepomuk
Jul 10 '08 #8
JosAH
11,448 Expert 8TB
Maybe the router also acts as a dhcp server; the OP can possibly inspect those
tables and simply use the private IP addresses instead; then there's no need to
route over the internet.

kind regards,

Jos
Jul 10 '08 #9
neoy
4
Thanks once again for all your help. I think I can figure it out from here. I'll probably post again on what the actual problem was when I get it working.
Jul 10 '08 #10
neoy
4
I got the client side to work for sure.

Expand|Select|Wrap|Line Numbers
  1. Socket socket = new Socket("google.com", 80);
  2. //print out raw html
  3.  
still working on the server
Jul 11 '08 #11
Nepomuk
3,112 Expert 2GB
I got the client side to work for sure.

Expand|Select|Wrap|Line Numbers
  1. Socket socket = new Socket("google.com", 80);
  2. //print out raw html
  3.  
still working on the server
Glad to hear that your making progress! :-)

Greetings,
Nepomuk
Jul 11 '08 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Lindstrom Greg - glinds | last post by:
Hello- I have a python routine (Python 2.2.3 on Windows 2000 "Professional") using a socket connection. Problem is, the backend service I'm connecting to will hang every so often and the socket...
5
by: ChrisH | last post by:
I have a python script (A) that monitors a process (3rd party) on the local machine (Windows). I would like to create a second program on a remote Windows computer that receives an update from...
2
by: tantiboh | last post by:
I'm not a new programmer, but this one's got me stymied; hopefully it's a fairly trivial problem. I'm using a socket connection to receive communication from a server. Normally, the entire message...
3
by: Ricardo Quintanilla | last post by:
i had a problem whom i do not know how to explain. i was using a TcpClient (System.Net.Sockets.TcpClient) object to send and receive data to an AS400 socket. Two months ago it started to work...
4
by: Sačo Zagoranski | last post by:
Hi! I'm writing a simple 3D First person shooter game. It is a multiplayer game, where all the players connect to one server.
3
by: Fredric Ragnar | last post by:
Hi, I am making a prototype system with Remoting in the bottom of the system. An XML Web Service is using the remote object on an IIS to present data. I am using a TcpChannel for communicating...
0
by: =?Utf-8?B?QWxwZXIgQUtDQVlPWg==?= | last post by:
Hello, First of all I wish you a good day. My help request is about .NET asynchrounus socket communication. I have developed Server-Client Windows Forms .NET applications in VC++ .NET v2003. I...
10
by: mairhtin o'feannag | last post by:
Hello, I'm having problems connecting to my new v9 db box. The pertinent information is below: DB2_db2inst1 60000/tcp DB2_db2inst1_1 60001/tcp DB2_db2inst1_2 60002/tcp DB2_db2inst1_END...
0
by: Mangabasi | last post by:
Howdy, I would like to use the Synthesis Toolkit for a demo. I downloaded the STK from http://ccrma.stanford.edu/software/stk/index.html. It seems very powerful and user friendly. There are...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.