Solannis
Posts: 7
Joined: Tue Nov 06, 2012 4:21 pm

Two Pi's, One Socket, Zero Joy

Sun Nov 20, 2016 4:40 am

Hi,

I need some help figuring out a connectivity (firewall?) issue I am having on my Raspberry Pi 3's. First, let me explain the computing environment I have:

A) My iMac desktop (connected wired and wireless)
B) Raspberry Pi 3 (raspberrypi_a, connected wireless only)
C) Raspberry Pi 3 (raspberrypi_b, connected wireless only)

I have written some basic Python socket code -- specifically a simple client and a simple server -- and it works in some cases but not others. Here are the tests I've run:

1) When I use the client code on my iMac to connect to the server code also running on my iMac (on port 12345 and also 49152), both programs work fine.
2) When I use the client code on my Raspberry Pi 3 (on both *_a and *_b) to connect to the server code running on my iMac on the same two ports, both programs work fine.
3) When I use the client code on my Raspberry Pi 3 (on _a) to connect to the server code running on my Raspberry Pi 3 (on _a) on the same two ports, both programs work fine. Same is true when I run on _b for both client and server.

The problem occurs when I try to go from client running on _b to server running on _a. When I run in this configuration, I receive a "socket error: [Errno 111] Connection refused". I receive the same error when the client is running on my iMac and I try to connect to either of the Raspberry Pi 3's.

These are default-build RPi's with the standard configuration. I have made no networking changes of any sort, nor have I turned on (or off) any default firewall software. Clearly the PI's can talk out the ports to the iMac, but they cannot talk to each other on the same port, nor can they take connections from the iMac.

I have tried to look around for a firewall or port configuration or other kind of networking rules setting/applet in the GUI, but I have not found anything that would seem to address my issue. I have done a lot of searching on this forum and through Google, and a lot of the chatter focuses on actual programming mistakes (which I know is not the case, since I do get the code to work when using the iMac as a server, or connecting to localhost), or talk about an iptables file in /etc/network which I do not seem to have.

So what am I missing? Why won't the Pi's talk to each other or let the iMac talk to them?

User avatar
paulwratt
Posts: 83
Joined: Fri Jun 12, 2015 12:15 am

Re: Two Pi's, One Socket, Zero Joy

Sun Nov 20, 2016 5:13 am

How did you connect to ur Pi's the first time, did u use ssh at anypoint, if so, then it maybe related to "trusted" connection

Are ur 2x Pi copy of same sdcard. if IP tables is not setup (default install) that should not effect it.

I am only guessing here, but I believe its something to do with _a & _b being too similar in someway. The connection_refused sound like key/hash not been set for "that machine", but u not mention ssl or ssh, so my only memory is from windows regarding sockets.

Do you know the process (step-by-step) what happens when you do socket connection, if so, then it will lead you to culpret, and it may have generic "unhelpful" result/error code...


Paul

User avatar
DougieLawson
Posts: 35836
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Two Pi's, One Socket, Zero Joy

Sun Nov 20, 2016 9:12 am

You need to have a server that runs a listener that spawns a new thread with accept() when a connection is made.
http://code.activestate.com/recipes/578 ... server/#c2

Or chuck everything you've done and use a ready built server like sshd or mosquitto or Apache2 or lighttpd or NGinx.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

Solannis
Posts: 7
Joined: Tue Nov 06, 2012 4:21 pm

Re: Two Pi's, One Socket, Zero Joy

Sun Nov 20, 2016 3:55 pm

Thank you for the feedback. Since the client and server code I have written already works (between my desktop and my Pi's, and when the Pi's talk to themselves) I have to assume that the problem is a configuration issue with the Pi's, not the fault of the code. The whole point of using Python was that it is the same language across the two platforms: if the code works on my Mac, then it should work on my Pi. And it does as long as the Pi is talking to itself. It is only when I try to have the two Pi's talk to each other or have the Mac talk to the Pi that I receive the error. Logically, this points to a configuration issue, not a code issue.

I will investigate and see if I can integrate sshd or one of your other recommended platforms into the application I am trying to create and get better results. I am not building a web server, and I really don't want or need remote login capability. What I need is two hosts that can talk to each other over a TCP connection. Like a chat server I suppose, but this is not a chat system I am building...not in the traditional sense, anyway.
Last edited by Solannis on Sun Nov 20, 2016 4:07 pm, edited 2 times in total.

Solannis
Posts: 7
Joined: Tue Nov 06, 2012 4:21 pm

Re: Two Pi's, One Socket, Zero Joy

Sun Nov 20, 2016 4:02 pm

paulwratt wrote:How did you connect to ur Pi's the first time, did u use ssh at anypoint, if so, then it maybe related to "trusted" connection

Are ur 2x Pi copy of same sdcard. if IP tables is not setup (default install) that should not effect it.

I am only guessing here, but I believe its something to do with _a & _b being too similar in someway. The connection_refused sound like key/hash not been set for "that machine", but u not mention ssl or ssh, so my only memory is from windows regarding sockets.

Do you know the process (step-by-step) what happens when you do socket connection, if so, then it will lead you to culpret, and it may have generic "unhelpful" result/error code...


Paul
I connected to my Pi's with a USB keyboard and mouse and a small HDMI monitor. Then I configured them locally (using the guided setup and then later the GUI) from there to connect to my Wi-Fi network and that's about it. I have ssh'ed into both of them from my Mac desktop, but I have not ssh'ed to either of them from the other. I will try that to see if that works/helps.

The two Pi's are set up using the same image I downloaded from the Raspberry Pi website.

I am familiar with the step-by-step process of making a socket connection, as I have been writing this kind of code for 20+ years (first in C, then in C++, then in Java, and now in Python). I know the code works because I can run the code locally on my Mac and my Pi's when connecting to localhost, and it works when the Pi's talk to my Mac. I receive the error ONLY when the two Pi's try to talk to each other (instead of themselves) or the Mac tries to talk to either of them. So I am quite certain that the code works. It seems to be a configuration issue of some kind that is preventing the two Pi's from talking to each other.

I will try your suggestion of ssh'ing from each to the other to see if that creates a "trusted" connection that will allow them to communicate.

User avatar
pi-anazazi
Posts: 526
Joined: Fri Feb 13, 2015 9:22 pm
Location: EU

Re: Two Pi's, One Socket, Zero Joy

Sun Nov 20, 2016 6:32 pm

Wireless isolation enabled on the wifi? Would prevent contact from one wireless client to another. Has to be an option in the wifi setup of your router.
Kind regards

anazazi

User avatar
paulwratt
Posts: 83
Joined: Fri Jun 12, 2015 12:15 am

Re: Two Pi's, One Socket, Zero Joy

Wed Nov 23, 2016 1:16 am

good suggestion @pi-anazazi but I think this is not the issue, however it maybe worth checking anyway.

you are right @Solannis I dont know why you would need to ssh to Mac, when you are not using ssl connections, but it simplifies things if you are.

The reason I mention "step-by-step" is that one of those steps will pull an OS config option that has not been set, or set the same on the Pi's.

My final suggestion is to do a generic search, not Pi or Raspbian specific, as I can well imagine there has been exactly this sort of issue, especially with Java (and possibly Python) on various OS's, they will probably help.

I am sorry to not be more helpful with specifics, but please post results, especially when "sovled" (and change post heading too), as this will probably help me in the future too

EDIT: one last thought, in your code can you connect via IP address instead of server name, this may prove useful in finding an answer.

Paul

Solannis
Posts: 7
Joined: Tue Nov 06, 2012 4:21 pm

Re: Two Pi's, One Socket, Zero Joy

Wed Nov 23, 2016 5:59 am

paulwratt wrote:good suggestion @pi-anazazi but I think this is not the issue, however it maybe worth checking anyway.

you are right @Solannis I dont know why you would need to ssh to Mac, when you are not using ssl connections, but it simplifies things if you are.

The reason I mention "step-by-step" is that one of those steps will pull an OS config option that has not been set, or set the same on the Pi's.

My final suggestion is to do a generic search, not Pi or Raspbian specific, as I can well imagine there has been exactly this sort of issue, especially with Java (and possibly Python) on various OS's, they will probably help.

I am sorry to not be more helpful with specifics, but please post results, especially when "sovled" (and change post heading too), as this will probably help me in the future too

EDIT: one last thought, in your code can you connect via IP address instead of server name, this may prove useful in finding an answer.

Paul
Hi Paul,

When you say step-by-step, can you be more specific? Are you referring to specific lines of code, or steps in the establishing of a socketed connection? Either way, since the Pi's can connect to the Mac, I know the code is right, so it is some specific configuration issue on the Pi's themselves that is the culprit, and that is what I am looking for help in ideas as to what to look at and where to look.

I have done many general Google searches, but most of them point at specific products/platforms (like NGinx and ssd and whatnot) and since I am writing this code myself (and, again, since it does work locally and from Pi to Mac), none of those posts that discuss those software platforms' settings really applies. I have yet to find something that says "Go to /etc/network and look at X and see if option Y or Z are set to n" or something that would be more OS-specific.

As far as connecting via IP address versus hostname, I have tried that and the results are no different in any test case. This is clearly a configuration issue on the Pi somewhere. I am not beyond wiping out the SD cards and trying another setup/reload. There is nothing I have done to configure them (other than the default setup) and no custom software on them (other than the code I have written), so I may just wipe them both out and try again and see if that makes a difference.

I am not usually the first person to try anything or have any problem, so I am somewhat surprised that no one else seems to have experienced this problem before. I will keep looking. I am just baffled that this hasn't already been experienced and resolved.

User avatar
pi-anazazi
Posts: 526
Joined: Fri Feb 13, 2015 9:22 pm
Location: EU

Re: Two Pi's, One Socket, Zero Joy

Wed Nov 23, 2016 9:39 am

...keep in mind that debugging network issues on raspian can be a bit of a pain.

viewtopic.php?t=156650
Kind regards

anazazi

User avatar
paulwratt
Posts: 83
Joined: Fri Jun 12, 2015 12:15 am

Re: Two Pi's, One Socket, Zero Joy

Wed Dec 07, 2016 9:29 am

there is one difference, but I am not sure it this affect ur problem, endian-ness.. (just putting it out there)

step-by-step I meant, at one point I could have quoted verbatim the DLL's/Java objects that were used to create a protocol link, from start to finish, including hardware level, and hence which config files/settings they would use.

I must be getting old, as I can do this no longer, which is why my replys so far have vague-ish, and not "on the money".

So my next step, in your situation, would be to forget that I know this should work, go back to basics, write some (different, from scratch) code to double check the situation, possibly in another language, and from a tutorial (maybe even original place you learned).

there maybe something in a tut that helps you pin down your issue. And if you use another language and get the same result you can say it is a fundamental issue on the OS's you use. If its an OS issue, then change ONE sdcard for another non-Raspbian OS, like VoidLinux, or Devuan, or Slackware Arm Pi (SARPi), or Arch, or Puppy, and see how you code goes there (then try TWO non-Raspbian).

Also, if you are willing to try another OS, you can get an older sdcard image and give thart a whurl, but that will only really work if you know how to get the "old packages" from the repo as well (basically dont do "sudo apt-get update")(2015 or earlier). I would consider this option as assisting Raspberry Pi Foundation iscolating possible issue, which you may find too time consuming.

Cheers
Paul

Solannis
Posts: 7
Joined: Tue Nov 06, 2012 4:21 pm

Re: Two Pi's, One Socket, Zero Joy

Sun Dec 11, 2016 1:44 am

Hi Paul,

Perhaps I was not clear in my first (and subsequent) post. The code works. It works correctly on Mac to Mac and it works Pi to Mac. It does not work on Mac to Pi and it does not work on Pi to Pi. So the code is not the question here. The versions of Python are similar (2.7.9 on the Pi's, 2.7.10 on the Mac). If it was the code that was in error somewhere, I would suspect that it would fail to work in all configurations, but that is NOT the case. The code works. I will try the same code in Java just to make sure, but I am pretty sure that this is a network/configuration issue, not a code issue. I have also wiped out the SD cards and reloaded NOOBS and Raspbian with Pixel, but that did not make any difference.

I will try other OSes to see if those possibly work better.

Solannis
Posts: 7
Joined: Tue Nov 06, 2012 4:21 pm

Re: Two Pi's, One Socket, Zero Joy

Wed Mar 15, 2017 2:37 am

Hey everyone. I know it's been a while, but that's a function of having the time to come back to this project and revisit my issue. So I took the advice of the suggestions made here and finally got around to writing a simple Java-based chat client. Sure enough, it works just fine. Mac to Pi, Pi to Mac, and Pi to Pi. So the problem does indeed appear to be with the Python Sockets implementation ON the Pi itself; specifically only when the Pi is acting as the "server." When the Pi is a client and the host is anything other than a Pi (e.g. my iMac), the Python Sockets implementation works fine. Under Java, however, the Pi works as both client and server, running both on the same Pi as well as two different Pi's (one as client, another as server). I have tested this both on all wired connections (iMac and both Pi's) as well as a mix of wired (iMac) and Wi-Fi (both Pi's). This successful eliminates Wi-Fi and possible firewall/interface configuration issues as possible sources of the problem.

So for whatever it's worth, using Python on the Pi to be a generic Socket server is a non-starter. I have not had the opportunity to try any of the other ssh/secure sockets application platforms that are available to the Pi, nor am I likely to at this point now that I know I can get this to work perfectly well in Java.

Just wanted to update this thread and bring it to a close for anyone else that might experience/wonder about this issue.

Return to “Troubleshooting”