-
Notifications
You must be signed in to change notification settings - Fork 0
/
sk2.py
53 lines (45 loc) · 1.17 KB
/
sk2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# # Import socket module
# import socket
# # Create a socket object
# s = socket.socket()
# # Define the port on which you want to connect
# port = 50000
# # connect to the server on local computer
# s.connect(('127.0.0.1', port))
# s.send(bytes("hello ", 'utf-8'))
# # receive data from the server and decoding to get the string.
# print (s.recv(1024).decode())
# # close the connection
# s.close()
# # now for udp client
# import socket
# import sys
# s= socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# message = "Hello, world I came via UDP".encode('utf-8')
# s.sendto(message, ('127.0.0.1', 50000))
# data, addr = s.recvfrom(4096)
# print(str(data))
# s.close()
# Now multi threaded client
import socket
import sys
cs= socket.socket()
host = '127.0.0.1'
port = 50000
try:
cs.connect((host, port))
print("Connected to server")
except:
print("Connection to server failed")
sys.exit()
response = cs.recv(1024).decode()
print(response)
while True:
print("Enter message to send to server")
message = input()
cs.send(message.encode())
data = cs.recv(1024).decode()
print("Server says: ", data)
if data == "bye":
break
cs.close()