0

I have two files. server.py and client.py. Client's code:

import socket
s = socket.socket()
try:
    s.connect(("localhost", 17894))
except socket.error:
    print "Couldn't connect."
    raise  # Useless, I know, but once I fix it I will change to exit()

data = ' '
BUFFER_SIZE = 1024

WELCOME_MSG = """Connecting to server.
The server should send you a list of commands when connected.
"""

print WELCOME_MSG
print s.recv(BUFFER_SIZE)  # Receive server's welcome message
while data:
    cmd = raw_input(">>> ")
    s.send(cmd)
    stuff = 1
    data = s.recv(BUFFER_SIZE)
    print data

    print "Server closed connection."
s.close()

Server's code:

import socket

s = socket.socket()
s.bind(("localhost", 17894))
s.listen(1)

BUFFER_SIZE = 1024

WELCOME_MSG = """...stuff..."""

while True:
    con, addr = s.accept()
    con.send(WELCOME_MSG)
    while True:
        data = s.recv(BUFFER_SIZE).split(" ")
        # some data processing, sets ans to a string
        con.send(str(ans))
    con.close()

I am running the server and then the client. Everything as expected, I receive the welcome message and it is displayed, the problem starts when I try and enter a command (or just try to send anything to the server)

Client's error:

Traceback (most recent call last):
  File "...path.../Client.py", line 22, in <module>
    data = s.recv(BUFFER_SIZE)
socket.error: [Errno 10053] An established connection was aborted by the software in your host machine

Server's error:

Traceback (most recent call last):
  File "...path.../Server.py", line 32, in <module>
    data = s.recv(BUFFER_SIZE).split(" ")
socket.error: [Errno 10057] A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using a sendto call) no address was supplied

The line number is different because I removed some imports

Socket programming is pretty new to me, so I don't know what is happening and how to fix it. Help would be appreciated!

2 Answers 2

2

I found my mistake.

import socket

s = socket.socket()
s.bind(("localhost", 17894))
s.listen(1)

BUFFER_SIZE = 1024

WELCOME_MSG = """...stuff..."""

while True:
    con, addr = s.accept()
    con.send(WELCOME_MSG)
    while True:
        data = s.recv(BUFFER_SIZE).split(" ") # HERE <----------------------------------
        # Some data processing, sets 'ans' to a string
        con.send(str(ans))
    con.close()

I am using the socket that is bound to receive data, instead of using the socket named con that is opened on s.accept().

Sign up to request clarification or add additional context in comments.

Comments

0

From a personal experience, try define a receive function like this:

def receive(sock):
    buf = sock.recv(BUFFER_SIZE)
    print (buf)

And the previous part should be like this:

while True:
    con, addr = s.accept()
    con.sendall(WELCOME_MSG)
    while 1:
        receive(con)
        con.sendall(str(ans))
        #con.close()

Notice

If you are using Python 3.x as you mentioned, you can not print data from the buffer, because in Python 3.x the buffer output type is bytes and you should decode it like this:

data = data.decode("UTF-8")

5 Comments

Sorry, it is python 2.7, I'm just programming something else in python 3 so I confused it. (Updating in the question too)
@AmitGold Ok , just notice part is for python 3 , but did you try answer?
but I don't wanna print it, I wanna run stuff on it, its supposed to be a command server...
@AmitGold I write an example for you here , you can change it to what you want
how will breaking it off into another function help?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.