实验图片和前期准备:
服务端发送和接受i: 客户端接收服务端的时间: 设备: 自己的笔记本电脑和jetsonnano一台+显示屏 同一个wifi 查看ubuntu和windows的ip地址
注意:windows和ubuntu关闭防火墙!
ubuntu作为服务端,windows客户端传输数据和文件:
服务端用python(jetsonnano) 客户端用java写:(自己笔记本) 服务端接收到文件并输出: 代码
from socket import *
import time
from time import ctime
host='192.168.31.177'
port=8189
bufsiz=1024
addr=(host,port)
tcpServer=socket(AF_INET,SOCK_STREAM)
tcpServer.bind(addr)
tcpServer.listen(5)
while True:
print('wait for connection..')
tcpCliServer,addr= tcpServer.accept()
print('..connect from',addr)
while True:
data = tcpCliServer.recv(bufsiz).decode()
print(data)
if data == "yes":
print("yee!")
if not data:
break
tcpCliServer.send(bytes(ctime(), 'utf-8'))
tcpCliServer.send(bytes("nice", 'utf-8'))
tcpCliServer.send(bytes("nn",'utf-8'));
tcpCliServer.close()
tcpServer.close()
客户端接收到的:
public static void main(String[]args) throws IOException {
int i = 0;
Socket socket = new Socket("192.168.31.177",8189);
OutputStream outputStream = socket.getOutputStream();
File file = new File("D:\\Inventory.txt");
DataOutputStream dataInputStream= new DataOutputStream(outputStream);
FileInputStream fileInputStream = new FileInputStream(file);
byte[]bytes = new byte[1024];
try{
dataInputStream.writeUTF(file.getName());
dataInputStream.flush();
System.out.println("传输文件");
int len=0;
while ((len = fileInputStream.read(bytes,0,bytes.length)) != -1)
{
dataInputStream.write(bytes,0,bytes.length);
dataInputStream.flush();
}
}catch (Exception e){
System.out.println(e);
}
InputStream inputStream = socket.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
outputStream.write("我是中国人yaoo".getBytes(StandardCharsets.UTF_8));
char[] charArr = new char[1000];
int readLen = -1;
if((readLen = (inputStreamReader.read(charArr))) != -1) {
while (i < 5) {
i++;
outputStream.write(("我是"+i).getBytes(StandardCharsets.UTF_8));
String newString = new String(charArr, 0, readLen);
System.out.println(newString);
}
dataInputStream.close();
fileInputStream.close();
outputStream.close();
inputStream.close();
socket.close();
}
}
windows作为服务端,ubuntu作为客户端:
自己笔记本作为服务端:
public class BeginThread extends Thread{
private Socket socket;
public BeginThread(Socket socket)
{
super();
this.socket = socket;
}
@Override
public void run(){
try{
InputStream inputStream = socket.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
char[] charArr = new char[1000];
int readLen = -1;
while((readLen = (inputStreamReader.read(charArr))) != -1)
{
String newString = new String(charArr,0,readLen);
if(newString.matches("yes!"))
System.out.println("成功收到来自客户端的数据");
System.out.println(newString);
}
inputStreamReader.close();
inputStream.close();
socket.close();
}catch (IOException e){
e.printStackTrace();
}
}
}
public class Server{
public static void main(String[] args) throws IOException {
ServerSocket socket = new ServerSocket(8888);
int runTag = 1;
while (runTag == 1)
{
Socket socket1 = socket.accept();
BeginThread beginThread = new BeginThread(socket1);
beginThread.start();
}
socket.close();
}
服务端接收到数据:
客户端:
import socket
s = socket.socket()
host = "192.168.31.131"
port = 8888
s.connect((host, port))
s.send('yes!'.encode('utf-8'))
s.close()
|