java socket 编程connection reset

发布于 2021-11-12 07:04:40 字数 3939 浏览 766 评论 4

在java socket 编程的练习中,我建了两个类,服务端和客户端;
我遇到这样一个问题。

下面是服务器端输入的内容。

println class

just do it

please study hard, day day up.

Hope happinesses follow you wherever you go!

java.net.SocketException: Connection reset

at java.net.SocketInputStream.read(Unknown Source)

at java.net.SocketInputStream.read(Unknown Source)

at mars.client.Server$Println.run(Server.java:55)

at java.lang.Thread.run(Unknown Source)

第一个类(服务端类Server.java)

 public class Server {

 

public static void main(String[] args) {

 

ServerSocket serverSocket = null;

Socket socket = null;

InputStream inputStream = null;

 

try {

serverSocket = new ServerSocket(5999);

while (true) {

socket = serverSocket.accept();

inputStream = socket.getInputStream();

// if a client connects, start a thread with a parameter to deal with the inputStream 

new Server().start(inputStream);

}

 

} catch (IOException e) {

e.printStackTrace();

} finally {

try {

serverSocket.close();

} catch (IOException e) {

e.printStackTrace();

 

}

}

}

 

//the function to start a thread with a parameter

public void start(InputStream input) {

new Thread(new Println(input)).start();

}

 

//Inner Class implements Runnable interface to deal with inputstream data

class Println implements Runnable {

 

private InputStream inputStream;

 

public Println(InputStream input) {

this.inputStream = input;

System.out.println("println class");

 

}

 

@Override

public void run() {

byte[] buffer = new byte[1024];

int temp = 0;

try {

//exception throwed in this code:inputStream.read(buffer)

while ((temp = inputStream.read(buffer)) != -1) {

System.out.println(new String(buffer, 0, temp));

}

} catch (IOException e) {

e.printStackTrace();

 

}

}

}

}

客服端的类TCPClient.java

public class TCPClient {

 

public static void main(String[] args) {

try {

Socket socket = new Socket("127.0.0.1", 5999);

InputStream inputStream = new FileInputStream("G://androidProject//testfile//test1.txt");

OutputStream outputStream = socket.getOutputStream();

new TCPClient().start(outputStream, inputStream);

} catch (UnknownHostException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

public void start(OutputStream output, InputStream inputStream) {

new Thread(new OutPut(output, inputStream)).start();

}

class OutPut implements Runnable {

 

OutputStream outputStream = null;

InputStream inputStream = null;

public OutPut(OutputStream output, InputStream inputStream) {

this.outputStream = output;

this.inputStream = inputStream;

}

@Override

public void run() {

byte[] buffer = new byte[1024];

int temp = 0;

try {

while((temp = inputStream.read(buffer)) != -1) {

System.out.println(new String(buffer, 0, temp));

outputStream.write(buffer, 0, temp);

}

} catch (IOException e) {

e.printStackTrace();

}

}

}

 

}

 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

归途 2021-11-17 10:00:50

懂了,非常感谢您的解答

坚持沉默 2021-11-17 01:50:30

谢谢您

眼泪淡了忧伤 2021-11-17 00:09:19

您说的这种情况如果是刚开始第一次读的时候会有可能读不到数据吧,而且在网络实际传输的时候很有可能会在文件结束前出现inputStream。avaliable=0

成熟的代价 2021-11-12 17:18:16

客户端输入的内容已经结束.服务器还在进行读取内容所以会抛出异常.

在服务器读取数据库加判断读取数据流是否有效

修改如下:

while (inputStream.available()>0&&(temp = inputStream.read(buffer)) != -1) {

					System.out.println(new String(buffer, 0, temp));

				}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文