Java服务器
package Socket;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class SS {
public static void main(String[] args) throws Exception{
ServerSocket ss = new ServerSocket(5612);
System.out.println("服务端已启动,正在监听端口...");
byte[] buf = new byte[1024];
byte[] img = new byte[1024];
while(true)
{
Socket s = ss.accept();
System.out.println("检测到客户端,准备数据接收...");
InputStream in = s.getInputStream();
in.read(buf);
String str = new String(buf).trim();
System.out.println(str);
File file=new File("E:\\2.jpg");
FileOutputStream fout=new FileOutputStream(file);
int len=0;
while((len=in.read(img))!=-1)
{
fout.write(img,0,len);
}
fout.flush();
fout.close();
in.close();
s.close();
}
}
}
Python客户端:
import base64
import socket
import os
import sys
import struct
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('127.0.0.1', 5612))
content=str('person:'+str(2)+',head:'+str(2)+',helmet:'+str(0)).encode("utf-8")
print(type(content))
s.send(content)
filepath = r'F:\Insects\sdfe\sdfe11.jpg'
with open(filepath, 'rb') as fp:
data = fp.read()
msg=content+data
s.send(data)
print((msg))
|