Noah Linsley
Noah Linsley

Reputation: 21

Why is my socket not working?

I've gotten [Errno 10048], then if I fix that, I get [Errno 10013], and if I fix that, I get [Errno 10053], and if I fix that, I get [Errno 10048] again. What's wrong with my code?

Here's the server:

#!/usr/bin/env python

import socket

host = ''
port = 65535
backlog = 5
size = 1024
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((host,port))
s.listen(backlog)
while 1:
    client, address = s.accept()
    data = client.recv(size)
    if data:
        client.send(data)
    client.close() 

And here's my client:

#!/usr/bin/env python

import socket

host = 'localhost'
port = 65535
size = 1024
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
while True:
    txt = raw_input('')
    s.send(txt)

What is wrong with my code?

Upvotes: 2

Views: 3132

Answers (1)

Jasper
Jasper

Reputation: 3947

Your client never connects to the server (note that the host and port variable are unused). Since the server closes the connection after each echo, I fixed the client by recreating the connection after each user input:

import socket

host = 'localhost'
port = 65535
size = 1024
while True:
    txt = raw_input('')
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect((host, port))
    s.send(txt)
    r = s.recv(size)
    print r
    s.close()

Upvotes: 1

Related Questions