Java代码问题
求大佬看看~~
本机运行代码Client第一次可以正常运行,但是第二次就会报错:
Exception in thread "main" java.net.SocketException: Connection reset
at java.base/sun.nio.ch.NioSocketImpl.implRead(NioSocketImpl.java:323)
at java.base/sun.nio.ch.NioSocketImpl.read(NioSocketImpl.java:350)
at java.base/sun.nio.ch.NioSocketImpl$1.read(NioSocketImpl.java:803)
at java.base/java.net.Socket$SocketInputStream.read(Socket.java:966)
at java.base/sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:270)
at java.base/sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:313)
at java.base/sun.nio.cs.StreamDecoder.read(StreamDecoder.java:188)
at java.base/java.io.InputStreamReader.read(InputStreamReader.java:177)
at java.base/java.io.BufferedReader.fill(BufferedReader.java:162)
at java.base/java.io.BufferedReader.readLine(BufferedReader.java:329)
at java.base/java.io.BufferedReader.readLine(BufferedReader.java:396)
at red.yhl.test7.TcpClient.main(TcpClient.java:23)
进程已结束,退出代码1
代码异常出现在Client代码中的log = br.readLine()中
该异常只会在本机出现,我尝试过虚拟机运行以及使用其他人的电脑运行就不会出现问题
代码示例:
TcpServer
package red.yhl.test7;
import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class TcpServer {
public static void main(String[] args) throws IOException {
ServerSocket ss = new ServerSocket(10100, 50, InetAddress.getByName("127.0.0.1"));
ss.setReuseAddress(true);
ThreadPoolExecutor pool = new ThreadPoolExecutor(
3,
6,
60,
TimeUnit.SECONDS,
new ArrayBlockingQueue<>(3),
Executors.defaultThreadFactory(),
new ThreadPoolExecutor.AbortPolicy()
);
while (true) {
// new Thread(new MyRunnable(ss.accept())).start();
pool.submit(new MyRunnable(ss.accept()));
}
// ss.close();
}
}
MyRunnable
package red.yhl.test7;
import java.io.*;
import java.net.Socket;
import java.util.UUID;
public class MyRunnable implements Runnable {
private final Socket socket;
private final InputStream in;
private final OutputStream out;
public MyRunnable(Socket socket) throws IOException {
this.socket = socket;
this.in = socket.getInputStream();
this.out = socket.getOutputStream();
}
@Override
public void run() {
try {
String name = UUID.randomUUID().toString().replace("-", "");
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("D:\\myapps\\Java\\mysocketnet\\copyImages\\" + name + ".png"));
BufferedInputStream bis = new BufferedInputStream(in);
int len;
byte[] bytes = new byte[1024];
while ((len = bis.read(bytes)) != -1) {
bos.write(bytes, 0, len);
}
bos.close();
System.out.println(name + "上传成功");
out.write(name.getBytes());
socket.shutdownOutput();
System.out.println(socket.toString());
System.out.println(socket.isClosed());
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
if (socket != null) {
try {
socket.close();// 报错 Connection reset 可尝试注释该行
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
}
System.out.println(socket.isClosed());
}
}
TcpClient
package red.yhl.test7;
import java.io.*;
import java.net.Socket;
public class TcpClient {
public static void main(String[] args) throws IOException {
Socket socket = new Socket("127.0.0.1", 10100);
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\myapps\\Java\\mysocketnet\\src\\red\\yhl\\test3\\userDemo.png"));
OutputStream os = socket.getOutputStream();
int len;
byte[] bytes = new byte[1024];
while ((len = bis.read(bytes)) != -1) {
os.write(bytes, 0, len);
}
os.flush();
socket.shutdownOutput();
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String log;
while ((log = br.readLine()) != null) {
System.out.println(log);
}
socket.close();
}
}
|