Modified simple_client

Now it's more descriptive
This commit is contained in:
gerroo 2018-10-28 13:24:41 -08:00
parent 991abb2402
commit 9ded2c3d22
5 changed files with 50 additions and 35 deletions

29
simple_client/client.py Normal file
View File

@ -0,0 +1,29 @@
# client.py
import socket
HOST, PORT = '127.0.0.1', 1400
s = socket.socket(
socket.AF_INET # ADDRESS FAMILIES
#Name Purpose
#AF_UNIX, AF_LOCAL Local communication
#AF_INET IPv4 Internet protocols
#AF_INET6 IPv6 Internet protocols
#AF_APPLETALK Appletalk
#AF_BLUETOOTH Bluetooth
socket.SOCK_STREAM # SOCKET TYPES
#Name Way of Interaction
#SOCK_STREAM TCP
#SOCK_DGRAM UDP
)
s.connect((HOST, PORT))
s.send('Hello World'.encode('ascii'))#in UDP use sendto()
data = s.recv(1024)#in UDP use recvfrom()
s.close()#end the connection
print(repr(data.decode('ascii')))

21
simple_client/server.py Normal file
View File

@ -0,0 +1,21 @@
# server.py
import socket
HOST, PORT = '127.0.0.1', 1400
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)#refer to client.py
s.bind((HOST, PORT))
s.listen(1)#listen for 1 connection
conn, addr = s.accept()#start the actual data flow
print('connected to:', addr)
while 1:
data = conn.recv(1024).decode('ascii')#receive 1024 bytes and decode using ascii
if not data:
break
conn.send((data + ' [ addition by server ]').encode('ascii'))
conn.close()

View File

@ -1,14 +0,0 @@
# client.py
import socket
HOST, PORT = '127.0.0.1', 1400
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
s.send(b'Hello World')
data = s.recv(1024)
s.close()
print(repr(data.decode('ascii')))

View File

@ -1,21 +0,0 @@
# server.py
import socket
HOST, PORT = '127.0.0.1', 1400
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(1)
conn, addr = s.accept()
print('connected to:', addr)
while 1:
data = conn.recv(1024)
if not data:
break
conn.send(data + b' [ addition by server ]')
conn.close()