tcp套接字通信

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

#coding:utf-8
import socket

host = '10.200.83.175'
port = 1329
s =socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.connect((host,port))

while True:
mes = bytes(input("Client input data:"),encoding='utf-8')
if mes == 'q':
print('it is break')
break
s.send(mes)
data,address = s.recv(1024)
print("This is data of Service:>>>>>>",data)

s.close()
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
#服务器端
import socket


host = '10.200.83.175'
port = 1329

s =socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.bind((host,port))
s.listen(5)

ss, addr = s.accept()

while True:
data = ss.recv(1024)
print(data)
if (data == '1'):
mes = 'welcome!'
ss.send(mes)
else:
mes = bytes('a',encoding='utf-8')

ss.send(mes)

s.close()