Home > database >  Python TCP sockets won't connect
Python TCP sockets won't connect

Time:09-25

As part of a uni course, I have to write a simple python TCP client-server chat but I'm having issues getting the sockets in the server.py and client.py programs to connect.

After many attempts I decided to really strip back the program to just try to connect the sockets and have the client send one message to the server after they are connected.

I am running the two programs (server.py and client.py) on the same computer (macOS) in separate terminal windows. I have tried turning off the firewall and running the programs as well just in case that was somehow causing an issue.

My code is as below:

server.py - This is run first

from socket import *
serverPort = 11500
serverSocket = socket(AF_INET, SOCK_STREAM)
serverSocket.bind(('', serverPort))

print("The server is listening")
serverSocket.listen(1)

clientSocket, clientAddress = serverSocket.accept()

message, clientAddress = serverSocket.recvfrom(2048)

client.py - Once server.py is running, this is then started

from socket import *

serverName = "127.0.0.1"
serverPort = 11500

clientSocket = socket(AF_INET, SOCK_STREAM)
clientSocket.connect((serverName, serverPort))

message = "Hello"
clientSocket.send(message.encode())

This is the error I have been receiving:

(base) me@ME python_socket % python3 tcp_server.py
The server is listening
Traceback (most recent call last):
  File "tcp_server.py", line 11, in <module>
    message, clientAddress = serverSocket.recvfrom(2048)
OSError: [Errno 57] Socket is not connected

Thanks in advance for any help. It seems like something that should be really simple but even after watching tutorials online I still seem to have the issue of the sockets not connecting.

CodePudding user response:

The issue is that, in the server.py code, you should replace serverSocket in the recvfrom line with clientSocket, since when you call accept right before that line, clientSocket (returned by accept) is a new socket specific to that connection.

clientSocket, clientAddress = serverSocket.accept()

message, clientAddress = clientSocket.recvfrom(2048)

After that change, it is going to work just fine.

CodePudding user response:

Problems

  1. You should use recv() instead of recvfrom() when you connect using TCP. recvfrom() is used for UDP socket.
  2. Instead of receiving data from serverSocket, you should use newly created TCP clientSocket.

Correct Solution

Server

from socket import *

serverPort = 11500
serverSocket = socket(AF_INET, SOCK_STREAM)
serverSocket.bind(("localhost", serverPort))

print("The server is listening")
serverSocket.listen(1)

clientSocket, clientAddress = serverSocket.accept()
message = clientSocket.recv(2048).decode()
print(message)

Client

from socket import *

serverName = "127.0.0.1"
serverPort = 11500

clientSocket = socket(AF_INET, SOCK_STREAM)
clientSocket.connect((serverName, serverPort))

message = "Hello"
clientSocket.send(message.encode())

Server Output:

The server is listening
Hello
  • Related