Daniel Engel
Daniel Engel

Reputation: 158

I got a connection error in my socket program

I tried to make a program that connects between a server and client.

Here is the server code:

import socket

HOST="127.0.0.1"
PORT=5000

mySocket=socket.socket(socket.AF_INET, socket.SOCK_STREAM)

try:
mySocket.bind( (HOST, PORT) )

except socket.error:
print ("Call to bind failed")

while 1:
  print("Waiting for connection")
  mySocket.listen(1)

  connection, address=mySocket.accept()
  print("Connection recieved from:", address[0])

  connection.send("SERVER>>> Connection successful")
  clientMessage=connection.recv(1024)

while clientMessage != "Client: end":
    if not clientMessage:
        break

    print (clientMessage)
    serverMessage=input("Server: ")
    connection.send("Server: "+serverMessage)
    clientMessage=connection.recv(1024)

print("Connection ended.")
connection.close()

This is the client:

import socket
HOST="127.0.0.1"
PORT=5000

print ("Attempting connection")
mySocket=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
  mySocket.connect( ( HOST, PORT) )
except socket.error:
  print("Call to connect failed")
print ("Connected to server")

serverMessage=mySocket.recv(1024)

while serverMessage!="Server: end":
  if not serverMessage:
      break

  print (serverMessage)
  clientMessage=input("Client: ")
  mySocket.send("Client: "+clientMessage)
  serverMessage=mySocket.recv(1024)

print ("Connection ended.")
mySocket.close()

This is the error:

 File "C:/Python34/python 3.4/client.py", line 13, in <module>
 serverMessage=mySocket.recv(1024)
 OSError: [WinError 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 error says I did something wrong with the address(?)

I cant understand what the error wants from me. Can you help me?

Upvotes: 2

Views: 986

Answers (1)

pholtz
pholtz

Reputation: 474

Alright, I made some edits to both of your programs. You need to handle sending and receiving data over the socket as a byte array since you are using python 3. The client and server programs below worked for me using python 3.5.1. Hope this helps.

Also I got rid of your while 1 infinite loop in the server and removed the try/except statements as I didn't feel they added anything. If you are going to except the socket.error you should at least quit the program at that point since that is a fatal error here.

server.py

import socket

HOST="127.0.0.1"
PORT=5000

mySocket=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
mySocket.bind( (HOST, PORT) )
mySocket.listen(1)
print("Waiting for connection")

connection, address=mySocket.accept()
print("Connection recieved from:", address[0])

connection.send(bytearray("SERVER>>> Connection successful", "utf-8"))
clientMessage=connection.recv(1024)

while clientMessage != "Client: end":
    if not clientMessage:
        break

    print (clientMessage)
    serverMessage=input("Server: ")
    connection.send(bytearray("Server: "+serverMessage, "utf-8"))
    clientMessage=connection.recv(1024)

print("Connection ended.")
connection.close()

client.py

import socket
HOST="127.0.0.1"
PORT=5000

print ("Attempting connection")
mySocket=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
mySocket.connect( ( HOST, PORT) )
print ("Connected to server")

serverMessage=mySocket.recv(1024)

while serverMessage!="Server: end":
  if not serverMessage:
      break

  print (serverMessage)
  clientMessage=input("Client: ")
  mySocket.send(bytearray("Client: "+clientMessage, "utf-8"))
  serverMessage=mySocket.recv(1024)

print ("Connection ended.")
mySocket.close()

Upvotes: 1

Related Questions