尝试与服务器通信时连接重置。 但是客户端执行代码没有任何异常。成功执行客户端后,打印终端中的所有字节。但是,首先在服务器端创建了两个连接。实际上我想为服务器和客户端编写一个程序来实现客户端的并行连接。 为什么 ScocketException:在服务器端发生连接重置?我该如何解决这个问题?
服务器:
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
import java.util.zip.Adler32;
import java.util.zip.CRC32;
import java.util.zip.Checksum;
public class Server {
private static String dir = "E:\\DestinatioFolder\\";
public static void main(String[] args) throws IOException {
ServerSocket servsock = new ServerSocket(5050);
Socket socket = null;
while(true) {
socket = servsock.accept();
System.out.println("New Connection....");
BufferedInputStream bis = new BufferedInputStream(socket.getInputStream());
DataInputStream dis = new DataInputStream(bis);
Thread t = new ClientHandler(socket, bis, dis);
t.start();
}
}
}
class ClientHandler extends Thread {
final String dir = "E:\\DestinationFolder\\"; // you may change this
final BufferedInputStream bis;
final DataInputStream dis;
final Socket socket;
public ClientHandler(Socket socket, BufferedInputStream bis, DataInputStream dis ) {
this.socket = socket;
this.bis = bis;
this.dis = dis;
}
@Override
public void run() {
try{
long fileLength = dis.readLong();
String fileName = dis.readUTF();
System.out.println("FILE Name: "+fileName);
File file = new File(dir + fileName);
FileOutputStream fos = new FileOutputStream(file);
BufferedOutputStream bos = new BufferedOutputStream(fos);
for(int i=0;i<fileLength; i++) {
int b = bis.read();
bos.write(b);
System.out.println("Byte:"+b);
}
bos.close();
} catch(Exception e){
System.out.println("Server: "+e);
}
}
}
客户:
import java.io.*;
import java.net.Socket;
import java.util.concurrent.ExecutorCompletionService;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Client1 {
private static String dir = "E:\\SourceFolder\\";
public static void main(String[] args) throws IOException, InterruptedException {
Socket[] sock = new Socket[5];
for(int i=0;i<2;i++) {
sock[i] = new Socket("localhost",5050);
}
File[] files = new File(dir).listFiles();
sendFile(files,sock);
}
private static void sendFile(File[] files, Socket[] sock) throws InterruptedException, IOException {
for(int i=0;i<2;i++) {
BufferedOutputStream bos = new BufferedOutputStream(
sock[i].getOutputStream());
DataOutputStream dos = new DataOutputStream(bos);
Thread t = new ClientThread(files[i], bos, dos, i);
t.start();
}
}
}
class ClientThread extends Thread {
final BufferedOutputStream bos;
final DataOutputStream dos;
final File file;
final int t;
public ClientThread(File file, BufferedOutputStream bos,
DataOutputStream dos, int t) {
this.dos = dos;
this.bos = bos;
this.file = file;
this.t = t;
}
@Override
public void run() {
try {
long length = file.length();
System.out.println("Thread: "+this.t+ " FILE Length: "+length);
dos.writeLong(length);
String fileName = file.getName();
System.out.println("Thread: "+this.t+ " FILE: "+fileName);
dos.writeUTF(fileName);
FileInputStream fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis);
int b = 0;
while((b = bis.read()) > 0) {
bos.write(b);
System.out.println("Thread: " + this.t + " Byte:"+b);
}
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}