remote socket communication

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • neoy
    New Member
    • Jul 2008
    • 4

    #1

    remote socket communication

    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.ht ml

    I was able to get the KnockKnockServe r 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.Serve rSocketFactory and the javax.net.Socke tFactory 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.
  • Nepomuk
    Recognized Expert Specialist
    • Aug 2007
    • 3111

    #2
    Originally posted by neoy
    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

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      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

      Comment

      • Nepomuk
        Recognized Expert Specialist
        • Aug 2007
        • 3111

        #4
        Originally posted by JosAH
        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

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by nepomuk
          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)

          Comment

          • Nepomuk
            Recognized Expert Specialist
            • Aug 2007
            • 3111

            #6
            Originally posted by JosAH
            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)

            Comment

            • neoy
              New Member
              • Jul 2008
              • 4

              #7
              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:
              Code:
              port = 4321;
              Socket socket = new ServerSocket(port).accept();
              //get streams and do io
              client side:
              Code:
              port = 4321;
              //replace "0.0.0.0" with ip address of host computer.
              Socket socket = new ServerSocket("0.0.0.0", port);
              //get streams and do io
              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.Connec tException: 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.

              Comment

              • Nepomuk
                Recognized Expert Specialist
                • Aug 2007
                • 3111

                #8
                Hi again!
                Originally posted by neoy
                Thanks for your responses.
                You're welcome! :-)
                Originally posted by neoy
                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. ^^)
                Originally posted by neoy
                My code basically looks like this:
                server side:
                Code:
                port = 4321;
                Socket socket = new ServerSocket(port).accept();
                //get streams and do io
                client side:
                Code:
                port = 4321;
                //replace "0.0.0.0" with ip address of host computer.
                Socket socket = new ServerSocket("0.0.0.0", port);
                //get streams and do io
                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.
                Originally posted by neoy
                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.Connec tException: 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.Conne ctException: 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.
                Originally posted by neoy
                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.
                Originally posted by neoy
                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.
                Originally posted by neoy
                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

                Comment

                • JosAH
                  Recognized Expert MVP
                  • Mar 2007
                  • 11453

                  #9
                  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

                  Comment

                  • neoy
                    New Member
                    • Jul 2008
                    • 4

                    #10
                    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.

                    Comment

                    • neoy
                      New Member
                      • Jul 2008
                      • 4

                      #11
                      I got the client side to work for sure.

                      Code:
                      Socket socket = new Socket("google.com", 80);
                      //print out raw html
                      still working on the server

                      Comment

                      • Nepomuk
                        Recognized Expert Specialist
                        • Aug 2007
                        • 3111

                        #12
                        Originally posted by neoy
                        I got the client side to work for sure.

                        Code:
                        Socket socket = new Socket("google.com", 80);
                        //print out raw html
                        still working on the server
                        Glad to hear that your making progress! :-)

                        Greetings,
                        Nepomuk

                        Comment

                        Working...