1

I am new to python coding and tried to create a simple python socket server. I coded a client.py and a server.py on my laptop and it seems to work (It does what it is supposed to do....), but if i try to run the server on my laptop and the client on my other computer, it sometimes times out.

client.py

import socket

s = socket.socket()

host = '192.168.178.87'
port = 12345

s.connect((host, port))
print s.recv(1024)
s.sendall("greetings")

server.py

import socket

s = socket.socket()

host = ''
port = 12345
s.bind((host, port))

s.listen(5)

i = 0

while i < 5:
    c, addr = s.accept()
    print 'Got connection from', addr
    c.send('Thank you for connecting')
    data = c.recv(1024)
    print data
    c.close()
    i += 1
    print i
s.close()

I am using a FritzBox 7390, Both computers are in the same local network, both firewalls are turned off and no antivirus is installed.

I am using windows 7 and python 2.7 on both computers.

My problem summed up:

If I run the server on my laptop(192.168.178.87) and open the client.py via
cmd on the same computer , it works (I tried to use 127.0.0.1, 192.168.178.87 and my official ipv4 address(portforwarding in the router)) and everything works.

But if I try to use the client.py on my other computer (192.168.178.131) it
only works if I am the fifth to try to connect and I have no idea why.
I can connect to the server via browser, and it sometimes works, but mostly
there is an timeout error (Errno 10060).

What is the problem with my code?

Wolfgang Fahl
  • 15,016
  • 11
  • 93
  • 186
Hans Wuast
  • 55
  • 6
  • I'm guessing you're swapping `host = '192.168.178.87'` for `host = '192.168.178.131'` when you're switching which script rruns on which machine? – Torxed Dec 20 '16 at 21:10
  • server.py runs on 192.168.178.87 and client.py runs on 192.168.178.131 – Hans Wuast Dec 20 '16 at 21:11
  • You mentioned you swapped the two, client.py moved to the "server" machine.. Did you swap the host definition upon doing so? – Torxed Dec 20 '16 at 21:15
  • This maybe worth taking a look at. https://www.youtube.com/watch?v=MCs5OvhV9S4 – SudoKid Dec 20 '16 at 21:17
  • What I did : I ran the server.py on 192.168.178.87 with `host=192.168.178.87` and client with `host=192.168.178.87` on 192.168.178.87 That worked everytime I tried , but when i started the client.py script on 192.168.178.131 with `host=192.168.178.87` it works sometimes – Hans Wuast Dec 20 '16 at 21:18
  • @Emmet Speer ,thanks , bookmarked it for later , but are you sure that this video has something to do with python sockets ? – Hans Wuast Dec 20 '16 at 21:20
  • Wierd stuff is happening right now :D I started server.py and client.py on both computers (modified host variable) and the client.py on 192.168.178.131 returns "thank you for connecting"(always) and the client.py on 192.168.178.87 always returns an 10060 error. So i basically did the same thing on different computers , but two different things happened xD – Hans Wuast Dec 20 '16 at 21:24
  • Thanks for the support , but I don't think anybody is able to help me with this problem, because my laptop seems to be kond of retarded :3 For those who care : I have no idea what the problem was , the code is correct, but my Notebook is a little bit wierd, so the only thing I can do at the moment is using my other computer as Server ^^ – Hans Wuast Dec 20 '16 at 21:47
  • My shot in the dark is: the Windows Firewall configuration prevents you from connecting. Perhaps briefly disabling the firewall on both computers could confirm that. – Robᵩ Dec 21 '16 at 02:10
  • 1
    Easiest way to chekc: use `wireshark` to see whether packets are present. – Fejs Dec 21 '16 at 14:22
  • @Robᵩ already did that , still the same problem ^^ – Hans Wuast Dec 21 '16 at 20:40

0 Answers0