当前位置: 技术问答>java相关
请帮看看这个client向server发信息的程序问题出在哪里?
来源: 互联网 发布时间:2015-06-17
本文导语: //server端 import java.io.*; import java.net.*; public class TestEcho{ public static void main(String[] args){ try{ ServerSocket s = new ServerSocket(4413); for(;;){ Socket incoming=s.accept(); new Handl...
//server端
import java.io.*;
import java.net.*;
public class TestEcho{
public static void main(String[] args){
try{ ServerSocket s = new ServerSocket(4413);
for(;;){
Socket incoming=s.accept();
new HandleIncoming(incoming);
}
}
catch(Exception e){
System.out.println("失败 1 "+e);
}
}
}
class HandleIncoming{
HandleIncoming(Socket incom){
try{ BufferedReader in = new BufferedReader
(new InputStreamReader(incom.getInputStream()));
PrintWriter out = new PrintWriter
(incom.getOutputStream(), true /* autoFlush */);
out.println( "Hello! Enter BYE to exit." );
boolean done = false;
while(!done)
{ String str = (String)in.readLine();
if (str == null) done = true;
else
{ out.println("You input "+str );
if (str.trim().equals("BYE"))
done = true;
}
}
incom.close();
}
catch(Exception e){
System.out.println("失败 2 "+e);
}
}
}
//client端
import java.io.*;
import java.net.*;
import java.util.*;
public class Client{
public static void main(String[] args){
try{
Socket telnet=new Socket(本地机名,4413);
telnet.setSoTimeout(20000);
DataOutputStream out=new DataOutputStream(telnet.getOutputStream());
out.writeChars("BYE");
out.flush();
}
catch(IOException e){
System.out.println("失败 3 "+e);
}
}
}
先运行server端的TestEcho,
(1)如果使用'telnet localhost 4413'命令,则一切正常,
窗口显示"Hello! Enter BYE to exit." ,输入"BYE"即可退出.
(2)但运行client端的Client则出现
"失败 2 java.net.SocketException: Connection reset by peer: JVM_recv in socket input stream read"
请各位大侠指教!!
分一定会送!!
import java.io.*;
import java.net.*;
public class TestEcho{
public static void main(String[] args){
try{ ServerSocket s = new ServerSocket(4413);
for(;;){
Socket incoming=s.accept();
new HandleIncoming(incoming);
}
}
catch(Exception e){
System.out.println("失败 1 "+e);
}
}
}
class HandleIncoming{
HandleIncoming(Socket incom){
try{ BufferedReader in = new BufferedReader
(new InputStreamReader(incom.getInputStream()));
PrintWriter out = new PrintWriter
(incom.getOutputStream(), true /* autoFlush */);
out.println( "Hello! Enter BYE to exit." );
boolean done = false;
while(!done)
{ String str = (String)in.readLine();
if (str == null) done = true;
else
{ out.println("You input "+str );
if (str.trim().equals("BYE"))
done = true;
}
}
incom.close();
}
catch(Exception e){
System.out.println("失败 2 "+e);
}
}
}
//client端
import java.io.*;
import java.net.*;
import java.util.*;
public class Client{
public static void main(String[] args){
try{
Socket telnet=new Socket(本地机名,4413);
telnet.setSoTimeout(20000);
DataOutputStream out=new DataOutputStream(telnet.getOutputStream());
out.writeChars("BYE");
out.flush();
}
catch(IOException e){
System.out.println("失败 3 "+e);
}
}
}
先运行server端的TestEcho,
(1)如果使用'telnet localhost 4413'命令,则一切正常,
窗口显示"Hello! Enter BYE to exit." ,输入"BYE"即可退出.
(2)但运行client端的Client则出现
"失败 2 java.net.SocketException: Connection reset by peer: JVM_recv in socket input stream read"
请各位大侠指教!!
分一定会送!!
|
我在后面加上一点东西后就好啦,我也不知道为什么
DataOutputStream out1=new DataOutputStream(telnet.getOutputStream());
out1.writeChars("BYEn");
out1.flush();
BufferedReader in = new BufferedReader(new InputStreamReader(telnet.getInputStream()));
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(telnet.getOutputStream())), true);
String str = in.readLine();
System.out.println(str);
out.println("sdfewrw");
str = in.readLine();
System.out.println(str);
out.println("BYE");
str = in.readLine();
System.out.println(str);
DataOutputStream out1=new DataOutputStream(telnet.getOutputStream());
out1.writeChars("BYEn");
out1.flush();
BufferedReader in = new BufferedReader(new InputStreamReader(telnet.getInputStream()));
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(telnet.getOutputStream())), true);
String str = in.readLine();
System.out.println(str);
out.println("sdfewrw");
str = in.readLine();
System.out.println(str);
out.println("BYE");
str = in.readLine();
System.out.println(str);
|
在 client端
将这句: DataOutputStream out=new DataOutputStream (telnet.getOutputStream());
out.writeChars("BYE");
改为: PrintWriter out = new PrintWriter (telnet.getOutputStream(), true) ;
out.println("BYE");
我以试过的。
将这句: DataOutputStream out=new DataOutputStream (telnet.getOutputStream());
out.writeChars("BYE");
改为: PrintWriter out = new PrintWriter (telnet.getOutputStream(), true) ;
out.println("BYE");
我以试过的。