D42 #100daysOfCode

Day42 #100daysofcode: I finally have Python sockets working! I am now able to send a message from my server to my client without anything breaking!

Read Time2 Minutes, 22 Seconds

We have communication!

So yesterday I was getting frustrated over not being able to understand how sockets would work, and also not being able to understand Youtube videos explaining it and decided to ditch the videos and read the documentation.

Here’s a link to the documentation I was using.

After a few hours of reading about sockets and how they worked, I followed the example that the documentation used to set up a basic connection between a server and a client, then I got to tweaking.

By default, there is an automatic message that is sent to the server, the server replies then the connection is closed.

What I wanted to do is have a constant connection where I could send messages to and from the client and server, somewhat simulating an online chat, but this also required me to understand the code so I could tweak it.

Well, after all my reading let me present to you my application!

So above I am sending messages to and from the client and server, yay!

I am facing an issue where after a few messages both client and server lock up, so I need to figure out what’s going on there, but I’m planning on making this from the ground up on my own.

Here’s to Day43!

Also if you want to see my code, here it is.

Server Code.

import socket
import sys

# Create a TCP/IP Socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Bind the socket to the port
server_address = ('localhost', 10000)
print('starting up on %s port %s' % server_address)
sock.bind(server_address)
# Listen for incoming connections
sock.listen(1)

while True:
    # Wait for a connection
    print('waiting for a connection')
    connection, client_address = sock.accept()

    try:
        print(sys.stderr, 'connection from', client_address)

        # Receive the data in small chunks and retransmit it
        while True:
            data = connection.recv(100)
            print(sys.stderr, 'received "%s"' % data)
            if data:
                print(sys.stderr, 'sending data back to client')
                # Message needs to be encoded to be sent.
                data = input("msg: ").encode()
                # msg = data.encode('utf-8')
                connection.sendall(data)
            else:
                print(sys.stderr, 'no more data from', client_address)
                break
    finally:
        # Clean up the connection
        connection.close()

Client Code.

import socket
import sys

# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# Connect the socket to the port where the server is listening
server_address = ('localhost', 10000)
print('connection to %s port %s' % server_address)
sock.connect(server_address)

try:
    while True:
        # Send data
        message = input("What is your message?:" ).encode()
        print(sys.stderr, 'sending "%s"' % message)
        sock.sendall(message)

        # Look for the response
        amount_received = 0
        amount_expected = len(message)
        
        while amount_received < amount_expected:
            data = sock.recv(100)
            amount_received += len(data)
            print(sys.stderr, 'received "%s"' % data)

finally:
    print(sys.stderr, 'closing socket')
    sock.close()
0 0
0 %
Happy
0 %
Sad
0 %
Excited
0 %
Angry
0 %
Surprise

One thought on “D42 #100daysOfCode

  1. Looks like an interesting problem. I haven’t done socket programming in about 25 years! I’ll take a look at it. I have cloned the 100 days repo and I hope to make Julia translations of each of the python scripts.

Leave a Reply

Your email address will not be published. Required fields are marked *

Close