Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 252ea77

Browse files
committed
socket server
1 parent e50ccc6 commit 252ea77

File tree

1 file changed

+61
-30
lines changed

1 file changed

+61
-30
lines changed

pytcp/TcpServer.py

Lines changed: 61 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,74 @@
1-
import time
2-
from socket import *
1+
import os
2+
import socket
3+
import threading
34

4-
import lighton
55

6-
lighton.setUP()
6+
def sending_file(connection):
7+
try:
8+
# 接收消息头
9+
# file_info_size = struct.calcsize('128sl')
710

8-
ctrCmd = ['UP', 'DOWN', 'VIEW']
9-
message = "hello world"
10-
HOST = '192.168.43.96'
11-
PORT = 5555
11+
head = connection.recv(1024)
1212

13-
BUFSIZE = 1024
14-
ADDR = (HOST, PORT)
13+
if head:
14+
data = head.decode("utf-8").split(";")
15+
file_name = data[1][9:]
16+
file_size = data[0][15:]
1517

16-
tcpSerSock = socket(AF_INET, SOCK_STREAM)
17-
tcpSerSock.bind(ADDR)
18-
tcpSerSock.listen(5)
18+
file_new_dir = os.path.join('receive')
19+
if not os.path.exists(file_new_dir):
20+
os.makedirs(file_new_dir)
1921

20-
while True:
21-
tcpCliSock, addr = tcpSerSock.accept()
22+
file_new_name = os.path.join(file_new_dir, file_name)
23+
received_size = 0
24+
# 如果文件已存在
25+
if os.path.exists(file_new_name):
26+
received_size = os.path.getsize(file_new_name)
2227

23-
time.sleep(.05)
24-
data = ''
25-
data = tcpCliSock.recv(BUFSIZE).decode()
28+
# 发送文件大小
29+
connection.send((str(received_size) + "\n").encode("utf-8"))
30+
print((str(received_size) + "\n").encode("utf-8"))
31+
print("start receiving file:", file_name)
2632

27-
print(data)
33+
write_file = open(file_new_name, 'ab')
34+
while not str(received_size) == file_size:
2835

29-
if not data:
30-
print("there is no data")
31-
break
36+
receive_origin = connection.recv(40960)
37+
if receive_origin == b"break...":
38+
break
39+
receive_data = receive_origin
40+
received_size += len(receive_data)
41+
print(received_size)
42+
write_file.write(receive_data)
3243

33-
if data == ctrCmd[0]:
34-
lighton.ledOn()
44+
if str(received_size) == file_size:
45+
print("接收完成!\n")
46+
else:
47+
print("接收暂停!")
48+
write_file.close()
3549

36-
if data == ctrCmd[1]:
37-
lighton.ledOff()
50+
connection.close()
3851

39-
if data == ctrCmd[2]:
40-
lighton.ledOn()
41-
# here I want to send String back to Android
52+
except Exception as e:
53+
print(e)
4254

43-
tcpCliSock.close();
55+
56+
if __name__ == '__main__':
57+
host = socket.gethostname()
58+
59+
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
60+
sock.bind(("192.168.42.166", 8888))
61+
print("服务已启动---------------")
62+
sock.listen(2)
63+
address = sock.accept()[1]
64+
print("客户端已连接:", address)
65+
66+
fileSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
67+
fileSocket.bind(("192.168.42.166", 9999))
68+
fileSocket.listen(5)
69+
70+
while True:
71+
connection, address = fileSocket.accept()
72+
print("发送文件的客户端地址:", address)
73+
thread = threading.Thread(target=sending_file, args=(connection,))
74+
thread.start()

0 commit comments

Comments
 (0)