1

I am trying a simple client/server Python socket program between my MacBook Pro and Raspberry Pi 4. This code is from Computer Networking: A Top Down Approach [Kurose, Ross].

Client:

#!/usr/bin/python3

from socket import *

server_name = '192.168.0.17'
server_port = 12345

client_socket = socket(AF_INET, SOCK_STREAM)
client_socket.connect((server_name, server_port))

message = input('string to capitalise: ')

client_socket.send(message.encode())

modified_message = client_socket.recv(1024)

print('from server:', modified_message.decode())

client_socket.close()

Server:

#!/usr/bin/python3

from socket import *

server_port = 12345
server_socket = socket(AF_INET, SOCK_STREAM)         
server_socket.bind(('', server_port))               
server_socket.listen(1)                             

print('the server is ready to receive')

while True :
    connection_socket, addr = server_socket.accept()     
    message = connection_socket.recv(1024).decode()
    modified_message = message.upper()
    connection_socket.send(modified_message.encode())
    connection_socket.close()

I have no issue when the the Pi is the server and Mac is the client, however vice versa (Pi = client, Mac = server) the client program will run but not display the prompt ('string to capialise:') or progress the program further.

I tried changing the socket type to UDP (SOCK_DGRAM) and this had no issues, so I am led to believe this might be a TCP matter. Has anyone else had this issue?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.