java socket 线程connection reset异常,创建线程不能读客户端的内容。急啊,详见代码。
客户端代码:importjava.net.*;importjava.io.*;publicclassClient{publicstaticvoidmain(String[...
客户端代码:import java.net.*;
import java.io.*;
public class Client {
public static void main(String []args){
String s="abc";
try{
Socket socket=new Socket("localhost",9528);
OutputStream out=socket.getOutputStream();
String str="1\n"+s;
System.out.print(str);
out.write(str.getBytes());
}catch(UnknownHostException e1){
e1.printStackTrace();
}catch(IOException e2){
e2.printStackTrace();
}} }
服务器代码:public class Server {
public static void main(String []args){
try{
ServerSocket server=new ServerSocket(9528);
Socket so=server.accept();
InputStream in=so.getInputStream();
InputStreamReader inR=new InputStreamReader(in);
BufferedReader inbf=new BufferedReader(inR);
String cmd=inbf.readLine();
System.out.print(cmd);
if(cmd.equals("1")){
NewThread thd=new NewThread(so);
thd.start();
}
}catch(IOException e1){
e1.printStackTrace();
} }
服务器端线程代码:public class NewThread extends Thread {
private Socket sct;
private String cmd;
private String a;
public NewThread(Socket s) {
sct = s;
try {
InputStream in = sct.getInputStream();
InputStreamReader inR = new InputStreamReader(in);
BufferedReader inbf = new BufferedReader(inR);
cmd = inbf.readLine();
a = inbf.readLine();
} catch (IOException e1) {
e1.printStackTrace();
} finally {
try {
sct.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void run() {
System.out.print(cmd);
System.out.print(a);
}
}
服务器异常:java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:168)
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:264)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:306)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:158)
at java.io.InputStreamReader.read(InputStreamReader.java:167)
at java.io.BufferedReader.fill(BufferedReader.java:136)
at java.io.BufferedReader.readLine(BufferedReader.java:299)
at java.io.BufferedReader.readLine(BufferedReader.java:362)
at st.NewThread.<init>(NewThread.java:27)
at st.Server.main(Server.java:23)
服务器端输出:1nullnull
求指点啊,最好详细点儿,多谢啦。 展开
import java.io.*;
public class Client {
public static void main(String []args){
String s="abc";
try{
Socket socket=new Socket("localhost",9528);
OutputStream out=socket.getOutputStream();
String str="1\n"+s;
System.out.print(str);
out.write(str.getBytes());
}catch(UnknownHostException e1){
e1.printStackTrace();
}catch(IOException e2){
e2.printStackTrace();
}} }
服务器代码:public class Server {
public static void main(String []args){
try{
ServerSocket server=new ServerSocket(9528);
Socket so=server.accept();
InputStream in=so.getInputStream();
InputStreamReader inR=new InputStreamReader(in);
BufferedReader inbf=new BufferedReader(inR);
String cmd=inbf.readLine();
System.out.print(cmd);
if(cmd.equals("1")){
NewThread thd=new NewThread(so);
thd.start();
}
}catch(IOException e1){
e1.printStackTrace();
} }
服务器端线程代码:public class NewThread extends Thread {
private Socket sct;
private String cmd;
private String a;
public NewThread(Socket s) {
sct = s;
try {
InputStream in = sct.getInputStream();
InputStreamReader inR = new InputStreamReader(in);
BufferedReader inbf = new BufferedReader(inR);
cmd = inbf.readLine();
a = inbf.readLine();
} catch (IOException e1) {
e1.printStackTrace();
} finally {
try {
sct.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void run() {
System.out.print(cmd);
System.out.print(a);
}
}
服务器异常:java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:168)
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:264)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:306)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:158)
at java.io.InputStreamReader.read(InputStreamReader.java:167)
at java.io.BufferedReader.fill(BufferedReader.java:136)
at java.io.BufferedReader.readLine(BufferedReader.java:299)
at java.io.BufferedReader.readLine(BufferedReader.java:362)
at st.NewThread.<init>(NewThread.java:27)
at st.Server.main(Server.java:23)
服务器端输出:1nullnull
求指点啊,最好详细点儿,多谢啦。 展开
4个回答
展开全部
/****
异常解释: 主要报的是一个 Connection reset :连接重置异常
导致此异常的原因是 客户断的连接断开了,也就是说
当服务端要从客户端读取数据的时候,客服端就断开连接了,当然就会报异常了。
*/
////////////////////////Server.java 代码:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args) {
try {
ServerSocket server = new ServerSocket(9528);
System.out.println("服务器启动成功...等待连接>>>>");
Socket so = server.accept();
System.out.println("获取一连接....");
/*
* 服务端在读取客户端数据的时候,
* 当客户端关闭连接,就会报Connection reset:连接重置异常
*
* 所以这时候不能让客户端的程序结束...
*
*/
InputStream in = so.getInputStream();
InputStreamReader inR = new InputStreamReader(in);
BufferedReader inbf = new BufferedReader(inR);
String cmd = inbf.readLine();
System.out.print(cmd);
if (cmd.equals("1")) {
NewThread thd = new NewThread(so);
thd.start();
}
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
////////////////NewThread.java 代码:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.Socket;
public class NewThread extends Thread {
private Socket sct;
private String cmd;
private String a;
public NewThread(Socket s) {
sct = s;
try {
while(true){////////////////////////
InputStream in = sct.getInputStream();
InputStreamReader inR = new InputStreamReader(in);
BufferedReader inbf = new BufferedReader(inR);
cmd = inbf.readLine();
a = inbf.readLine();
}////////////////////////////////////
} catch (IOException e1) {
e1.printStackTrace();
} finally {
try {
sct.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void run() {
System.out.print(cmd);
System.out.print(a);
}
}
// ///////////////////Client 代码:
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
public class Client {
public static void main(String []args){
String s="abc";
try{
Socket socket=new Socket("localhost",9528);
System.out.println("服务器连接成功》。。。。");
OutputStream out = socket.getOutputStream();
InputStream in = socket.getInputStream();/////////////
String str = "1\n" + s;
System.out.print(str);
out.write(str.getBytes());
////////////
while(true){
in.read();
}
////////////
} catch (UnknownHostException e1) {
e1.printStackTrace();
} catch (IOException e2) {
e2.printStackTrace();
}
}
}
异常解释: 主要报的是一个 Connection reset :连接重置异常
导致此异常的原因是 客户断的连接断开了,也就是说
当服务端要从客户端读取数据的时候,客服端就断开连接了,当然就会报异常了。
*/
////////////////////////Server.java 代码:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args) {
try {
ServerSocket server = new ServerSocket(9528);
System.out.println("服务器启动成功...等待连接>>>>");
Socket so = server.accept();
System.out.println("获取一连接....");
/*
* 服务端在读取客户端数据的时候,
* 当客户端关闭连接,就会报Connection reset:连接重置异常
*
* 所以这时候不能让客户端的程序结束...
*
*/
InputStream in = so.getInputStream();
InputStreamReader inR = new InputStreamReader(in);
BufferedReader inbf = new BufferedReader(inR);
String cmd = inbf.readLine();
System.out.print(cmd);
if (cmd.equals("1")) {
NewThread thd = new NewThread(so);
thd.start();
}
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
////////////////NewThread.java 代码:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.Socket;
public class NewThread extends Thread {
private Socket sct;
private String cmd;
private String a;
public NewThread(Socket s) {
sct = s;
try {
while(true){////////////////////////
InputStream in = sct.getInputStream();
InputStreamReader inR = new InputStreamReader(in);
BufferedReader inbf = new BufferedReader(inR);
cmd = inbf.readLine();
a = inbf.readLine();
}////////////////////////////////////
} catch (IOException e1) {
e1.printStackTrace();
} finally {
try {
sct.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void run() {
System.out.print(cmd);
System.out.print(a);
}
}
// ///////////////////Client 代码:
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
public class Client {
public static void main(String []args){
String s="abc";
try{
Socket socket=new Socket("localhost",9528);
System.out.println("服务器连接成功》。。。。");
OutputStream out = socket.getOutputStream();
InputStream in = socket.getInputStream();/////////////
String str = "1\n" + s;
System.out.print(str);
out.write(str.getBytes());
////////////
while(true){
in.read();
}
////////////
} catch (UnknownHostException e1) {
e1.printStackTrace();
} catch (IOException e2) {
e2.printStackTrace();
}
}
}
追问
我按照你说的修改了,果然不会报异常了,但是newthread里面的run()函数不执行。我已经把start()放在newthread的构造函数中的while循环里了,还是没有输出,这是什么情况呢?
追答
/////////////////// Server.java里的一段代码修改下:
/*
* 服务端在读取客户端数据的时候,
* 当客户端关闭连接,就会报Connection reset:连接重置异常
*
* 所以这时候不能让客户端的程序结束...
*
*/
//InputStream in = so.getInputStream();
//InputStreamReader inR = new InputStreamReader(in);
//BufferedReader inbf = new BufferedReader(inR);
//String cmd = inbf.readLine();
//System.out.print("从客户端获取的数据为:"+cmd);
//if (cmd.equals("1")) {
NewThread thd = new NewThread(so);
System.out.println("111111111");
thd.start();
//}
// NewThread.java修改为:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.Socket;
public class NewThread extends Thread {
private Socket sct;
private String cmd;
private String a;
public NewThread(Socket s) {
sct = s;
try {
//while(true){////////////////////////
InputStream in = sct.getInputStream();
InputStreamReader inR = new InputStreamReader(in);
BufferedReader inbf = new BufferedReader(inR);
System.out.println("66666666");
cmd = inbf.readLine();
// a = inbf.readLine();
System.out.println("444444444");
//}////////////////////////////////////
} catch (IOException e1) {
e1.printStackTrace();
} finally {
try {
sct.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void run() {
System.out.print(cmd);
System.out.print(a);
}
}
展开全部
in = new BufferedReader(new InputStreamReader(s
.getInputStream()));
out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket
.getOutputStream())), true);
代码已经修改好了,百度犯什么抽,不让传。。
.getInputStream()));
out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket
.getOutputStream())), true);
代码已经修改好了,百度犯什么抽,不让传。。
追问
你发我邮箱吧947492929@qq.com,拜托了,我纠结好久了,你让我看到了曙光啊,拜谢了。
追答
已发送
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
是服务器端和客户端连接的原因,所以检查一下连接。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
dfdfdffdfdsfdfdfd
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询