用TCP协议写一个客户端和一个服务端,实现上传文件
程序员文章站
2022-06-06 16:50:59
...
客户端:
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
public class TCP_File_Send {
public static void main(String[] args) throws IOException {
Socket socket = new Socket("127.0.0.1", 7878);
OutputStream output = socket.getOutputStream();
FileInputStream input = new FileInputStream(new File("C:\\ESD\\abc.txt"));
byte[] buf = new byte[1024];
int len = -1;
while ((len = input.read(buf)) != -1) {
output.write(buf, 0, len);
}
input.close();
socket.close();
}
}
服务端:
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class TCP_File_Server {
public static void main(String[] args) throws IOException {
ServerSocket ss = new ServerSocket(7878);
Socket accept = ss.accept();
InputStream input = accept.getInputStream();
FileOutputStream output = new FileOutputStream("C:\\ESD\\def.txt");
byte[] buf = new byte[1024];
int len = -1;
while ((len = input.read(buf)) != -1) {
output.write(buf, 0, len);
}
output.close();
ss.close();
}
}
第一次写博客!还请多多指教。
上一篇: QPushButton设置qss式样
下一篇: QT获取节点的方法