This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Java in General and the fly likes Why Won't a Simple Socket Program Work when a Windows 7 Machine Acts as Server? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "Why Won Watch "Why Won New topic
Author

Why Won't a Simple Socket Program Work when a Windows 7 Machine Acts as Server?

Kevin Simonson
Ranch Hand

Joined: Oct 22, 2011
Posts: 113
I don't know if this is a problem for this Java forum, or if my problem is in determining my laptop's IP address, but here goes.

I've got a couple of very simple Java files, for the server:

and for the client:

As you can see, all I'm doing is entering a byte value (from 0 to 255) on the command line when I execute "SmSe.java", and as long as I use the same <port> value for both programs, and include the IP address of the machine I execute <SmSe> on as the second argument to <SmRc> that byte value should be transmitted across the socket from the server to the client.

This works just fine if I execute <SmSe> on a Linux machine and <SmRc> on my laptop, which is a Windows 7 machine. But this should also work in reverse, shouldn't it? I mean, if I have "SmSe.java" on my laptop too, and "SmRc.java" on the Linux machine, shouldn't I be able to type in "java SmSe 13267 68" on my laptop and then "java 13267 <ip-address>" on the Linux machine, where <ip-address> is the IP address of my laptop?

I found a webpage at "http://kb.iu.edu/data/aapa.html#cmd" that says:

How do I determine my computer's IP address?

..........<snip>..........

Windows 7, without using the command prompt

1. In the system tray, click the network connection icon and select Open Network and Sharing Center.
2. To view the IP address of a wired connection, click Local Area Connection.
To view the IP address of a wireless adapter, click Wireless Network Connection (Network Name).
3. Click Details... Your IP address will appear next to "IPv4 Address".

I followed these instructions and the IP address next to "IPv4 Address" was "10.88.36.152". So I typed in "java SmSe 13267 68" on my laptop, and then on the Linux machine I typed in "java SmRc 13267 10.88.36.152". Nothing happened on either machine for a few seconds, but eventually my program on the Linux machine halted abruptly when an <IOException> was thrown; I got the message in my exception handler, "I/O exception!" and my program ended. Nothing happened in the window on my laptop at all; program "SmSe.java" there appears to be still waiting for someone to make the socket connection. Does anybody know what I'm doing wrong here?

Kevin Simonson
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3610
    
  60

I'd advice to check your firewall settings. I've found the firewall to be much less permissive when I migrated from Windows XP to Windows 7.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Your main problem so far is that you have crippled your error-handling code in such a way that you have no idea what went wrong. All you know is that something bad happened.

And that's because you specifically programmed it that way. Here's a better way of doing it:


That code will print a stack trace when any IOException is thrown. The stack trace provides useful information about the problem. Try it.
Kevin Simonson
Ranch Hand

Joined: Oct 22, 2011
Posts: 113
Paul Clapham wrote:Your main problem so far is that you have crippled your error-handling code in such a way that you have no idea what went wrong. All you know is that something bad happened.

And that's because you specifically programmed it that way. Here's a better way of doing it:


That code will print a stack trace when any IOException is thrown. The stack trace provides useful information about the problem. Try it.

Okay, I changed my code accordingly. The exception handler in "SmRc.java" now is:

I tried again executing "java SmSe 13267 68" on my laptop and "java SmRc 13267 10.88.36.152" on the Linux machine. After a little bit I got the following output on the Linux machine:

I/O exception!
readExc.getMessage() == "Connection timed out".
java.net.ConnectException: Connection timed out
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:351)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:213)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:200)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:529)
at java.net.Socket.connect(Socket.java:478)
at java.net.Socket.<init>(Socket.java:375)
at java.net.Socket.<init>(Socket.java:189)
at SmRc.main(SmRc.java:12)

I'm not entirely sure how this helps me though. All it seems to do is say that the O/S timed out while trying to make the connection.

Kevin Simonson
Kevin Simonson
Ranch Hand

Joined: Oct 22, 2011
Posts: 113
Martin Vajsar wrote:I'd advice to check your firewall settings. I've found the firewall to be much less permissive when I migrated from Windows XP to Windows 7.

Do I need to check the firewall settings for my (Windows 7) laptop or the Linux machine? Or both? And is there a standard way to check firewall settings on a Windows 7 machine and on a Linux machine, or do I pretty much have to call my company's help desk and get them to help me figure out what the firewall settings are on these two machines?

Kevin Simonson
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Kevin Simonson wrote:I'm not entirely sure how this helps me though. All it seems to do is say that the O/S timed out while trying to make the connection.


And you didn't know that before, right?

It helps you because it tells you what's going on in the network. The usual problem with connecting to a server is when there is no server listening at the port you tried to connect to. But you didn't get the message you would have got if that were the case, which says "Connection refused". You got a different error message, so you don't have to confirm (yet) that your server is indeed listening at the port you tried to connect to. Instead, you have to figure out what that message means.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

As was said before, double check your firewall. The other thing to be concerned with in Win 7 is extra user-restrictions. Applications run from a user account are further 'sandboxed' to prevent bad code from executing, and so a lot of things that could run in XP get blocked in Win7. When you run the application on Windows 7, try to run it 'As Administrator' (even if you are logged in as an Administrator user).

If you run from the command line, make sure you open the command line 'As Administrator' by right-clicking on the Command Prompt icon and choosing 'Run as Administrator.'

If you run from an executable JAR then try to right-click directly on the JAR and do 'Run as Administrator.'


Steve
Brian Burress
Ranch Hand

Joined: Jun 30, 2003
Posts: 122
My bet is that the issue is on the Windows 7 box. As someone else suggested, Windows 7 does seem to default as less permissive as far as firewall.

A simple Google should assist you in finding out how to work with windows 7 firewall settings. Make sure you are allowing the server side to accept inbound requests over your port.

It won't hurt to do a netstat on the server side to confirm you are listening to the port.
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3610
    
  60

Kevin Simonson wrote:
Martin Vajsar wrote:I'd advice to check your firewall settings. I've found the firewall to be much less permissive when I migrated from Windows XP to Windows 7.

Do I need to check the firewall settings for my (Windows 7) laptop or the Linux machine? Or both? And is there a standard way to check firewall settings on a Windows 7 machine and on a Linux machine, or do I pretty much have to call my company's help desk and get them to help me figure out what the firewall settings are on these two machines?

Kevin Simonson

I meant the firewall on Windows side. It might also help marking your network as Home or Work instead of Public (assuming the Linux server actually isn't on a Public network). Both of this requires privileges you might be lacking. The detailed instructions are too extensive to list here, besides I'm still not that experienced with Windows 7. However, you should be able to google these out on the web.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7892
    
  21

Kevin Simonson wrote:I tried again executing "java SmSe 13267 68" on my laptop and "java SmRc 13267 10.88.36.152" on the Linux machine. After a little bit I got the following output...
I/O exception!
readExc.getMessage() == "Connection timed out".
java.net.ConnectException: Connection timed out
at java.net.PlainSocketImpl.socketConnect(Native Method)...
I'm not entirely sure how this helps me though. All it seems to do is say that the O/S timed out while trying to make the connection.

Of course it does. You now know the primary reason why the program failed.

Unfortunately, a bit like those CSI shows, "head splattered because he fell from a great height" isn't always what it seems.
Programs don't commit suicide, gumshoe:Dey wuz muydered. Muydered I tells ya. By youz, or by "de man" (the JVM), or by something we'z gots no control over...

Your task, Mr. Phelps - should you choose to accept it - is to work out why; and our analysts suggest that you look outside your program for the answer.

This tape will self-destruct in five seconds....

Winston

 
GeeCON Prague 2014
 
subject: Why Won't a Simple Socket Program Work when a Windows 7 Machine Acts as Server?