java socket 编程connection reset
在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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
懂了,非常感谢您的解答
谢谢您
您说的这种情况如果是刚开始第一次读的时候会有可能读不到数据吧,而且在网络实际传输的时候很有可能会在文件结束前出现inputStream。avaliable=0
客户端输入的内容已经结束.服务器还在进行读取内容所以会抛出异常.
在服务器读取数据库加判断读取数据流是否有效
修改如下: