supersocket缓冲区_使用Socket从Client传输文件到Server对文件缓冲区大小有限制么?...
Client端代碼:
public class Client implements Runnable {
private String ip;// 連接IP
private int port;// 連接端口
private String filePath;// 源文件路徑
public Client(String ip, int port, String filePath) {
super();
this.ip = ip;
this.port = port;
this.filePath = filePath;
}
@Override
public void run() {
try {
long id = Thread.currentThread().getId();
System.out.println("進(jìn)程" + id + "上傳開始!");
Socket s = new Socket(InetAddress.getByName(ip), port);
FileInputStream fis = new FileInputStream(filePath);
BufferedInputStream bis = new BufferedInputStream(fis);
DataInputStream dis = new DataInputStream(s.getInputStream());
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
long size = new File(filePath).length();
dos.writeUTF(MD5.getMD5(filePath));
dos.writeLong(size);
dos.writeUTF(new File(filePath).getName());
int i = 0;
int j=0;
while ((i = fis.read(buffer)) != -1) {
dos.write(buffer, 0, i);
dos.flush();
System.out.println(j++);
// Thread.sleep(2000);
}
System.out.println("進(jìn)程" + id + dis.readUTF());
dis.close();
dos.close();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Server端代碼:
public class Server implements Runnable {
private ServerSocket ss;
private String path;
public Server(ServerSocket ss, String path) {
super();
this.ss = ss;
this.path = path;
}
@Override
public void run() {
while (true) {
try {
Socket s = ss.accept();
DataInputStream dis = new DataInputStream(s.getInputStream());
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
String md5 = dis.readUTF();
System.out.println("MD5:" + md5);
long size = dis.readLong();
System.out.println("文件總大小:" + size);
String fileName = new SimpleDateFormat("yyyyMMddHmsS")
.format(new Date())
+ (int) (1000 * Math.random())
+ dis.readUTF();
System.out.println("文件名:" + fileName);
FileOutputStream fos = new FileOutputStream(path + "/"
+ fileName);
BufferedOutputStream bos = new BufferedOutputStream(fos);
while (size > bufferSize) {
dis.read(buffer);
fos.write(buffer);
size -= bufferSize;
// Thread.sleep(2000);
}
fos.write(buffer, 0, dis.read(buffer));
if (MD5.getMD5(path + "/" + fileName).equals(md5)) {
dos.writeUTF("MD5驗(yàn)證正確,上傳成功!");
} else {
dos.writeUTF("MD5驗(yàn)證錯(cuò)誤,上傳失敗!");
}
dis.close();
dos.close();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
try {
ServerSocket ss = new ServerSocket(2011);
String path = "upload";
new Thread(new Server(ss, path)).start();
} catch (IOException e) {
e.printStackTrace();
}
}
}
疑點(diǎn)如下:
1.當(dāng)Client的bufferSize小于Server的bufferSize時(shí)拋出java.net.SocketException: Software caused connection abort: socket write error,是不是要考慮Client和Server的處理速度問題?就是說Server讀取速度快于Client的寫入速度時(shí)候會(huì)出問題?可是這個(gè)異常是Client報(bào)的啊。
2.當(dāng)Client端sleep(2000)時(shí)候Server端會(huì)等待這個(gè)可以理解,而當(dāng)Server端sleep(2000)時(shí)候Client端也會(huì)等待,為何呢?這個(gè)不能理解。另外既然Server端會(huì)等待Client端的write,是不是可以理解為兩端的bufferSize隨便設(shè)置都可以?可運(yùn)行結(jié)果表明我的理解是錯(cuò)誤的。當(dāng)Client端的bufferSize是Server端的整數(shù)倍時(shí)候才能傳輸成功。
求解惑,或許是我的程序本身有問題?
總結(jié)
以上是生活随笔為你收集整理的supersocket缓冲区_使用Socket从Client传输文件到Server对文件缓冲区大小有限制么?...的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android wear 2 手表,An
- 下一篇: 解决错误:Main applicatio