Ironic
Posts: 5
Joined: Fri Nov 29, 2013 11:14 pm

Cannot connect to socket server.

Fri Nov 29, 2013 11:21 pm

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

User avatar
rpdom
Posts: 17275
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Cannot connect to socket server.

Sat Nov 30, 2013 5:50 am

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

User avatar
DeeJay
Posts: 2027
Joined: Tue Jan 01, 2013 9:33 pm
Location: East Midlands, UK

Re: Cannot connect to socket server.

Sat Nov 30, 2013 8:36 am

Are you sure that

Code: Select all

host = socket.gethostname()
is correct if you want to accept an inbound connection from another networked device?
How To Ask Questions The Smart Way: http://www.catb.org/~esr/faqs/smart-questions.html
How to Report Bugs Effectively: http://www.chiark.greenend.org.uk/~sgtatham/bugs.html

User avatar
DougieLawson
Posts: 39304
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Cannot connect to socket server.

Sat Nov 30, 2013 12:21 pm

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/
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

User avatar
DougieLawson
Posts: 39304
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Cannot connect to socket server.

Sat Nov 30, 2013 12:36 pm

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()
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

Ironic
Posts: 5
Joined: Fri Nov 29, 2013 11:14 pm

Re: Cannot connect to socket server.

Sat Nov 30, 2013 7:20 pm

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

User avatar
DeeJay
Posts: 2027
Joined: Tue Jan 01, 2013 9:33 pm
Location: East Midlands, UK

Re: Cannot connect to socket server.

Sat Nov 30, 2013 9:32 pm

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.
How To Ask Questions The Smart Way: http://www.catb.org/~esr/faqs/smart-questions.html
How to Report Bugs Effectively: http://www.chiark.greenend.org.uk/~sgtatham/bugs.html

Ironic
Posts: 5
Joined: Fri Nov 29, 2013 11:14 pm

Re: Cannot connect to socket server.

Sat Nov 30, 2013 11:19 pm

That seems to have solved it.

I really was convinced this was the way to go.

Tks DeeJay

Return to “Networking and servers”