diff --git a/simple_client_server/README.md b/simple_client/README.md similarity index 100% rename from simple_client_server/README.md rename to simple_client/README.md diff --git a/simple_client/client.py b/simple_client/client.py new file mode 100644 index 000000000..e8319a072 --- /dev/null +++ b/simple_client/client.py @@ -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'))) diff --git a/simple_client/server.py b/simple_client/server.py new file mode 100644 index 000000000..c23075608 --- /dev/null +++ b/simple_client/server.py @@ -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() diff --git a/simple_client_server/client.py b/simple_client_server/client.py deleted file mode 100644 index c91e6233a..000000000 --- a/simple_client_server/client.py +++ /dev/null @@ -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'))) diff --git a/simple_client_server/server.py b/simple_client_server/server.py deleted file mode 100644 index c6b661d99..000000000 --- a/simple_client_server/server.py +++ /dev/null @@ -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()