Page 1 of 1

Cannot connect to socket server.

Posted: Fri Nov 29, 2013 11:21 pm
by Ironic
Hi all

Trying to create a simple python socket server in Rpi

Code: Select all

import socket

s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
print 'Socket Created'
host = socket.gethostname()
port=12345
s.bind((host,port))

s.listen(5)
while True:
 c, addr = s.accept()
 print 'Got', addr
 c.send('Hello')
 c.close
This seems to run ok. Prints the Socket created line and sits there.

However i am unable to connect to it.
From both Win7(with firewall off) and from Android i can ping the RPi local network ip.
However i get a connection refused if i try to open socket to the RPi.
Connection from own Rpi works.

Any ideas?

Thanks

Re: Cannot connect to socket server.

Posted: Sat Nov 30, 2013 5:50 am
by rpdom
First thing to do is check that your Pi is listening on that port and IP address. Use the netstat command to check this.

Code: Select all

pi@raspi2 ~ $ netstat -t -l -n
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0      0 0.0.0.0:41222           0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:33190           0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN
You should see a line with something like

Code: Select all

tcp        0      0 0.0.0.0:12345           0.0.0.0:*               LISTEN
If your Pi is waiting for input on port 12345

Re: Cannot connect to socket server.

Posted: Sat Nov 30, 2013 8:36 am
by DeeJay
Are you sure that

Code: Select all

host = socket.gethostname()
is correct if you want to accept an inbound connection from another networked device?

Re: Cannot connect to socket server.

Posted: Sat Nov 30, 2013 12:21 pm
by DougieLawson
gethostbyname() does a DNS lookup to find the IP addr for the local or foreign system
gethostbyaddr() does a reserve DNS lookup to get a name from an IP addr

http://www.pythonforbeginners.com/code- ... -examples/

Re: Cannot connect to socket server.

Posted: Sat Nov 30, 2013 12:36 pm
by DougieLawson
Here's a simple working server program

Code: Select all

import socket
import threading
class SimpleServer:
  def __init__(self, host, port):
    self.host = host
    self.port = port
    self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    self.sock.settimeout(None)
    self.client_sockets = []

  def initialise(self):
    try:
      self.sock.bind((self.host, self.port))
    except socket.error:
      return
    self.sock.listen(5)
    self.server_thread = threading. Thread(target=self. serve_forever)
    self.server_thread.setDaemon(True)
    self.server_thread.start()
    print "Server running on %s and listening on %d" % (self.host, self.port)

  def serve_forever(self):
    try:
      request, client_address = self.sock.accept()
    except socket.error:
      return
    self.client_sockets.append(request)
    print "Received connection from " , client_address
And the python program that uses that class

Code: Select all

#!/usr/bin/python

from SimpleServer import SimpleServer
import socket
host = gethostbyname('pi')
server = SimpleServer(host , 20000)
server.initialise()
server.serve_forever()

Re: Cannot connect to socket server.

Posted: Sat Nov 30, 2013 7:20 pm
by Ironic
Hi all

Thanks for the replies:

rpdom, your code yields:
tcp 0 0 127.0.1.1:12345
Not the 0.0.0.0 you indicated. However if from the own Rpi i do: telnet 127.0.1.1 12345 i can connect to the server.

DeeJay AFAIK yes. This means i am openin the socket in the same network address i am. For connection to another computer is here i use an ip.

Douguie, i switched to s.bind(('localhost',port)) justo be on the safe side. Same results

Tried you code. Same results.

This seems to be a networking issue.
Firewall in router is disabled.
I can ping the Rpi (and connect over ssh) from android and windows, so at least some ports are open.
Can ping the android from windows and Rpi.
Any suggestions on how to diagnose this?

Regards

Re: Cannot connect to socket server.

Posted: Sat Nov 30, 2013 9:32 pm
by DeeJay
Ironic wrote: DeeJay AFAIK yes. This means i am openin the socket in the same network address i am. For connection to another computer is here i use an ip.
So now I'm not clear if the code you have posted is what is causing problems or not...

As an experiment, could you try changing that line to

Code: Select all

host = ''
Then re-run the server code on the RPi and try again to make a connection to port 12345 on the RPi from one of your other systems.

Re: Cannot connect to socket server.

Posted: Sat Nov 30, 2013 11:19 pm
by Ironic
That seems to have solved it.

I really was convinced this was the way to go.

Tks DeeJay