Skip to content

Instantly share code, notes, and snippets.

@jialinhuang00
Last active September 13, 2020 03:09
Show Gist options
  • Save jialinhuang00/25c027d46828a35df3ad8465331a8099 to your computer and use it in GitHub Desktop.
Save jialinhuang00/25c027d46828a35df3ad8465331a8099 to your computer and use it in GitHub Desktop.
python TCP
import socket
serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = socket.gethostbyname('')
port = 5001
serversocket.bind((host, port))
serversocket.listen()
while True:
clientsocket, address = serversocket.accept()
print('someone is reaching from ' + str(address))
# send
server_say_hello = "Greeting from server ------------------ Start"
clientsocket.send(server_say_hello.encode())
# get
print(clientsocket.recv(1024).decode())
# keep swallowing
while True:
input = clientsocket.recv(1024).decode()
if input == '':
break
print(input)
break
print('Going to shut down...')
clientsocket.close()
import socket
clientsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = socket.gethostbyname('')
port = 5001
clientsocket.connect((host, port))
# get
print(clientsocket.recv(1024).decode())
# send
client_say_hello = 'Greeting from client ------------------ Start'
clientsocket.send(client_say_hello.encode())
message = input()
# keep typing for server if user's input is not 'bye'
while message != 'bye':
clientsocket.send(message.encode())
message = input()
import socket
clientsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = socket.gethostbyname('')
port = 5001
clientsocket.connect((host, port))
# get
print(clientsocket.recv(1024).decode('ascii'))
# send
client_say_hello = 'Greeting from client ------------------1'
clientsocket.send(client_say_hello.encode('ascii'))
# get
print(clientsocket.recv(1024).decode('ascii'))
# send
client_say_hello_2 = 'Greeting from client ------------------2'
clientsocket.send(client_say_hello_2.encode('ascii'))
import socket
serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = socket.gethostbyname('')
port = 5001
serversocket.bind((host, port))
serversocket.listen()
while True:
clientsocket, address = serversocket.accept()
print('someone is reaching from ' + str(address))
# send
server_say_hello = "Greeting from server ------------------1"
clientsocket.send(server_say_hello.encode('ascii'))
# get
print(clientsocket.recv(1024).decode('ascii'))
# send
server_say_hello_2 = "Greeting from server ------------------2"
clientsocket.send(server_say_hello_2.encode('ascii'))
# get
print(clientsocket.recv(1024).decode('ascii'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment