• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 246
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 246
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 246
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.'

 
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 Vashko
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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

 
reply
    Bookmark Topic Watch Topic
  • New Topic