0

Just started learning python socket programming. I'm trying to send and receive data from a Raspberry Pi 3 using python sockets. I'm running the server program on the Raspberry Pi and the client program on my laptop running Ubuntu. Both are on the same WiFi network. The program runs and the client connects to the server, but there is no data transfer. Both the client and server do nothing. The two programs run properly if I try to run them both on the same device. Here's my code :-

Client

import socket

HOST = '172.16.31.51'
PORT = 5000
BUFSIZ = 1024

if __name__ == '__main__':
    client_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    host = input("Enter hostname [%s]: " %HOST) or HOST
    port = input("Enter port [%s]: " %PORT) or PORT

    sock_addr = (host, int(port))
    client_sock.connect(sock_addr)

    payload = 'GET TIME'

    try:
        while True:
            client_sock.send(payload.encode('utf-8'))
            data = client_sock.recv(BUFSIZ)
            print(repr(data))
            more = input("Want to send more data to server[y/n]:")
            if more.lower() == 'y':
                payload = input("Enter payload: ")
            else:
                break
    except KeyboardInterrupt:
        print("Exited by user")
    client_sock.close()

Server

import socket
from time import ctime

PORT = 5000
BUFSIZ = 1024
ADDR = ('', PORT)

if __name__ == '__main__':
    server_socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    server_socket.bind(ADDR)
    server_socket.listen(5)
    server_socket.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR, 1)

    while True:
        print('Server waiting for connection...')
        client_sock, addr = server_socket.accept()
        print('Client connected from: ', addr)

        while True:
            data = client_sock.recv(BUFSIZ)
            if not data or data.decode('utf-8') == 'END':
                break
            print("Received from client: %s" % data.decode('utf-8'))
            print("Sending the server time to client: %s" %ctime())
            try:
                client_sock.send(bytes(ctime(), 'utf-8'))
            except KeyboardInterrupt:
                print("Exited by user")
        client_sock.close()
    server_socket.close()

EDIT:

The screenshots - https://i.stack.imgur.com/uFEMR.jpg

As soon as I hit Ctrl + C, on the client side, the server seems to send some data before it disconnects from the client. Here's a screenshot of that - https://i.stack.imgur.com/8eXjU.jpg

  • What is your output on both ends? What have you diagnosed so far? – Klaus D. Jan 06 '18 at 08:01
  • Here are the output screens on both devices - https://imgur.com/a/NgzsC – Sparsh Kumar Jan 06 '18 at 15:23
  • And as soon as I click on Ctrl + C, on the client side (my laptop), the server seems to send some data and then 'connection reset by peer' appears. I have no idea why it sends data only when I hit Ctrl + C. Here's a screenshot of that - https://imgur.com/a/hoLwN – Sparsh Kumar Jan 06 '18 at 15:31

1 Answers1

0

Your sockets work OK, but you made some other programming mistakes. Did you ever run it? This is the modified working code. (Hint: you can start testing just the client when you use nc -l 5000 as a simple manual server.

Client

import socket

HOST = '127.0.0.1'
PORT = 5000
BUFSIZ = 1024

if __name__ == '__main__':
    client_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    host = str(raw_input("Enter hostname [%s]: " %HOST)) or HOST
    port = str(raw_input("Enter port [%s]: " %PORT)) or PORT

    sock_addr = (host, int(port))
    client_sock.connect(sock_addr)

    payload = 'GET TIME'

    try:
        while True:
            client_sock.send(payload.encode('utf-8'))
            data = client_sock.recv(BUFSIZ)
            print(repr(data))
            more = raw_input("Want to send more data to server[y/n]:")
            if more.lower() == 'y':
                payload = str(raw_input("Enter payload: "))
            else:
                break
    except KeyboardInterrupt:
        print("Exited by user")
    client_sock.close()

Server

import socket
from time import ctime

PORT = 5004
BUFSIZ = 1024
ADDR = ('', PORT)

if __name__ == '__main__':
    server_socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    server_socket.bind(ADDR)
    server_socket.listen(5)
    server_socket.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR, 1)

    while True:
        print('Server waiting for connection...')
        client_sock, addr = server_socket.accept()
        print('Client connected from: ', addr)

        while True:
            data = client_sock.recv(BUFSIZ)
            if not data or data.decode('utf-8') == 'END':
                break
            print("Received from client: %s" % data.decode('utf-8'))
            print("Sending the server time to client: %s" %ctime())
            try:
                client_sock.send( ctime().encode('utf-8') )
            except KeyboardInterrupt:
                print("Exited by user")
                break;
        client_sock.close()
    server_socket.close()
transistor
  • 649
  • 6
  • 11
  • I did run my code before posting. It ran well locally but didn't seem to work when I ran it on two different devices (That's why I had the different HOST IP in the client program). Anyways, I ran the code you posted. Same problem. Runs well locally. Client connects when the server is on a different device but doesn't do anything after that. – Sparsh Kumar Jan 06 '18 at 15:10
  • Ok, I see. Propably the network connection works a bit different when connected tomanother machine. This may be the ‚TCP flush‘ problem. Try setsockopt TCP_NODELAY or using UDP instead of TCP. More info: https://stackoverflow.com/questions/855544/is-there-a-way-to-flush-a-posix-socket and https://stackoverflow.com/questions/4407835/python-socket-flush – transistor Jan 07 '18 at 10:32