日期:2014-05-20  浏览次数:20982 次

无解java:模拟聊天程序,错误信息java.net.ConnectException: Connection refused: connect
本人跟着老师写了一个模拟java聊天的程序,可是我写了在win7下运行不成功,报错信息如下:
java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:519)
at java.net.Socket.connect(Socket.java:469)
at java.net.Socket.<init>(Socket.java:366)
at java.net.Socket.<init>(Socket.java:180)
at com.briup.net.SocketChat.client(SocketChat.java:81)
at com.briup.net.SocketChat.main(SocketChat.java:28)
还请高手救救小弟啊,,老师也木有给我解决啊···求助啊···

源码如下:(老师那里可以运行的)

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;

public class SocketChat {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

if(args.length>0){
server();
}
else{
client();
}
}

//服务端程序
public static void server(){
System.out.println("==========server run window===========");
try {
ServerSocket ss = new ServerSocket(9999);
Socket s = ss.accept();

InputStream is = s.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
DataInputStream dis = new DataInputStream(bis);


OutputStream os = s.getOutputStream();
BufferedOutputStream bos = new BufferedOutputStream(os);
DataOutputStream dos = new DataOutputStream(bos);

//字符输入流的对象
InputStreamReader isr = new InputStreamReader(System.in);//可以将一个字节输入流转换为字符流
BufferedReader br = new BufferedReader(isr);

String info;
while(true){
info = dis.readUTF();
System.out.println("client say: "+info);

if(info.equals("bye")){
break;
}


info = br.readLine();
dos.writeUTF(info);
dos.flush();
if(info.equals("bye")){
break;
}
}

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

//客户端程序
public static void client(){
System.out.println("==========client run window===========");
try {
Socket s = new Socket("127.0.0.1",9999);

OutputStream os = s.getOutputStream();
BufferedOutputStream bos = new BufferedOutputStream(os);
DataOutputStream dos = new DataOutputStream(bos);

InputStream is = s.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
DataInputStream dis = new DataInputStream(bis);

InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);

String info;
while(true){
info = br.readLine();
dos.writeUTF(info);
dos.flush();
if(info.equals("bye")){
break;
}
info = dis.readUTF();
System.out.println("server say: " + info);
if(info.equals("bye")){
break;
}
}


} catch (UnknownHostException e) {