java socket 编程connection reset

Jwindy 发布于 2012/03/12 15:17
阅读 9K+
收藏 0

【Gopher China万字分享】华为云的Go语言云原生实战经验!>>>

在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();

}

}

}

 

}

 

加载中
0
天马行空&
天马行空&

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

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

修改如下:

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

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

				}

找到组织
找到组织
您说的这种情况如果是刚开始第一次读的时候会有可能读不到数据吧,而且在网络实际传输的时候很有可能会在文件结束前出现inputStream。avaliable=0
Jwindy
Jwindy
谢谢您
0
Jwindy
Jwindy
懂了,非常感谢您的解答
返回顶部
顶部