Java编程的socket编程问题,出现 java.net.ConnectException的错误,请问该如何改正程序。谢谢!
importjava.io.*;importjava.net.*;publicclasscomm{/***@paramargs*@throwsIOException*/p...
import java.io.*;
import java.net.*;
public class comm {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
Socket echoSocket = null;
PrintWriter out = null;
BufferedReader in = null ;
try{
echoSocket = new Socket ("localhost" , 8001);
out = new PrintWriter(echoSocket .getOutputStream(),true);
in = new BufferedReader (new InputStreamReader (echoSocket.
getInputStream()));
}
catch (UnknownHostException e){
System .err .println("找不到服务器:localhost !");
System .exit(1);
}
System .out.println(in.readLine());
System .out.println(in.readLine());
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System .in));
String userInput;
while ((userInput = stdIn.readLine())!= null){
out.println(userInput);
System.out .println(in.readLine());
out.close();
in .close();
echoSocket .close();
}
ServerSocket serverSocket = null;
out = null;
in = null ;
try{
serverSocket = new ServerSocket(8001);
}
catch (IOException e){
System.err .println("不能监听端口:8001.");
System.exit(1);
}
Socket incoming = null;
while(true)
{
incoming = serverSocket.accept();
out = new PrintWriter (incoming .getOutputStream (),
true);
in =new BufferedReader(new InputStreamReader(incoming
.getInputStream()));
out.println(" 你好! …");
out.println("输入Exit 退出。");
out.flush();
while(true){
String str = in.readLine();
if(str == null){
break;
}
else{
out .println(" 服务器给客户端:"+str);
out .flush();
if(str .trim().equalsIgnoreCase("Exit")){
break;
}
}
out.close();
in .close();
serverSocket .close();
}
}
}
} 展开
import java.net.*;
public class comm {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
Socket echoSocket = null;
PrintWriter out = null;
BufferedReader in = null ;
try{
echoSocket = new Socket ("localhost" , 8001);
out = new PrintWriter(echoSocket .getOutputStream(),true);
in = new BufferedReader (new InputStreamReader (echoSocket.
getInputStream()));
}
catch (UnknownHostException e){
System .err .println("找不到服务器:localhost !");
System .exit(1);
}
System .out.println(in.readLine());
System .out.println(in.readLine());
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System .in));
String userInput;
while ((userInput = stdIn.readLine())!= null){
out.println(userInput);
System.out .println(in.readLine());
out.close();
in .close();
echoSocket .close();
}
ServerSocket serverSocket = null;
out = null;
in = null ;
try{
serverSocket = new ServerSocket(8001);
}
catch (IOException e){
System.err .println("不能监听端口:8001.");
System.exit(1);
}
Socket incoming = null;
while(true)
{
incoming = serverSocket.accept();
out = new PrintWriter (incoming .getOutputStream (),
true);
in =new BufferedReader(new InputStreamReader(incoming
.getInputStream()));
out.println(" 你好! …");
out.println("输入Exit 退出。");
out.flush();
while(true){
String str = in.readLine();
if(str == null){
break;
}
else{
out .println(" 服务器给客户端:"+str);
out .flush();
if(str .trim().equalsIgnoreCase("Exit")){
break;
}
}
out.close();
in .close();
serverSocket .close();
}
}
}
} 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询