Skip to content
Snippets Groups Projects
Commit b5101e65 authored by Doug Harms's avatar Doug Harms
Browse files

Initial Commit

parents
No related merge requests found
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 (f'connecting to {server_address[0]} port {server_address[1]}')
sock.connect(server_address)
# Send data
message = 'This is the message. It will be repeated.'
print (f'sending "{message}"')
sock.sendall(message.encode())
# Look for the response
amount_received = 0
amount_expected = len(message)
while amount_received < amount_expected:
data = sock.recv(16)
amount_received += len(data)
print (f'received "{data.decode()}"')
dummy = input("Press enter to terminate ")
print ('closing socket')
sock.close()
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 (f'starting up on {server_address[0]} port {server_address[1]}')
sock.bind(server_address)
# Listen for incoming connections
sock.listen()
while True:
# Wait for a connection
print ('waiting for a connection')
connection, client_address = sock.accept()
print (f'connection from {client_address}')
# Receive the data in small chunks and retransmit it. Continue doing this
# until the client closes the tcp connection
while True:
data = connection.recv(16)
print (f'received "{data.decode()}"')
print (f'length of data={len(data)}')
if len(data) > 0:
print ('sending data back to the client')
connection.sendall(data)
else:
print (f'{client_address} has closed the connection, so we are finished.')
break
# Clean up the connection
connection.close()
import socket
import sys
import threading
# define a class to handle one connected client.
class serverThread( threading.Thread ):
#initialize the instance. The socket and clent address are passed
def __init__(self, connection, clientAddress ):
threading.Thread.__init__(self)
self.connection = connection
self.clientAddress = clientAddress
# this method (which is automatically invoked by start) does all the processing for one client
def run(self):
print (f'connection from {self.clientAddress}')
# Receive the data in small chunks and retransmit it. Continue doing this
# until the client closes the tcp connection
while True:
data = self.connection.recv(16)
#print (f'received "{data.decode()}"')
#print (f'length of data={len(data)}')
if len(data) > 0:
#print ('sending data back to the client')
connection.sendall(data)
else:
print (f'{self.clientAddress} has closed the connection, so we are finished.')
break
# Clean up the connection
self.connection.close()
# the main function
# 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 (f'starting up on {server_address[0]} port {server_address[1]}')
sock.bind(server_address)
# Listen for incoming connections
sock.listen()
while True:
# Wait for a connection
print ('waiting for a connection')
connection, client_address = sock.accept()
# create a thread to handle this client
thread = serverThread( connection, client_address )
thread.start()
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment