Tcp two computers

Lesson 27: TCP from two computers

Alternate Video:

Server side:

#server-receive  tcp
import socket, sys,os

print('server pid:',os.getpid())
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
host = sys.argv[1]
port= int(sys.argv[2])
s.bind( (host, port) )
s.listen(1)
print(s)
print()
conn, addr = s.accept()
print(conn)
while True:
    reply = input("Send: ")
    conn.sendall(reply.encode())
    if reply == 'bye':
        break
    data = conn.recv(1024)
    print('Client says: ',data.decode())

conn.close()

Client side:

#client-send tcp
import socket, sys,os
print('client pid:',os.getpid())

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
host = sys.argv[1]
port = int(sys.argv[2])
s.connect( (host, port) )

while True:
    data = s.recv(1024) 
    print('Server replies: ',data.decode())
    if data.decode() == 'bye':
        print('Server ended call')
        break
    line = input('Send :') 
    s.sendall(line.encode())

   
s.close()