崩溃了!做一个群聊系统。实例化 Socket 竟然出错。麻烦各位大侠帮小弟一把
package com.afei.chat;import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.*;
public class ClientChat extends Frame {
String useName;
TextField textInput = new TextField(5);
TextArea textShow = new TextArea(10, 40);
Socket socket=null;
boolean isConnectionServer = false;// 用于判断连接到服务器没有
DataOutputStream dos = null; public static void main(String[] args) {
new ClientChat().chatFace(); } // 聊天界面
public void chatFace() {
this.setLocation(200, 39);// 起始位置
this.setSize(400, 500); //
this.setTitle(useName + "的聊天窗口");// 窗口的标题
this.addWindowListener(new WindowListener() { @Override
public void windowClosed(WindowEvent e) {
// TODO Auto-generated method stub } @Override
public void windowClosing(WindowEvent e) {
System.exit(0);// 正常退出 } @Override
public void windowDeactivated(WindowEvent e) {
// TODO Auto-generated method stub } @Override
public void windowDeiconified(WindowEvent e) {
// TODO Auto-generated method stub } @Override
public void windowIconified(WindowEvent e) {
// TODO Auto-generated method stub } @Override
public void windowOpened(WindowEvent e) {
// TODO Auto-generated method stub } @Override
public void windowActivated(WindowEvent e) {
// TODO Auto-generated method stub } } );// 窗口监听。只要关闭窗口用的
this.add(textShow, new BorderLayout().NORTH);// 增加显示组件
this.add(textInput, new BorderLayout().SOUTH); // 增加输入组件
this.pack();// 消除空格
connectServer();//连接服务器
textInput.addActionListener(new TextFieldListener()); this.setVisible(true);// 窗口可见 } // 敲回车键的监听类
private class TextFieldListener implements ActionListener { @Override
public void actionPerformed(ActionEvent e) {
String str = textInput.getText();
textShow.setText(str);
textInput.setText(null);
System.out.println(socket);
//connectServer();
sendToServer(str); } } // 连接服务器
public void connectServer() {
try {
socket=new Socket("127.0.0.1",7788);
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} // 发送数据到服务器
public void sendToServer(String str) {
try {
//dos = new DataOutputStream(socket.getOutputStream());
while (isConnectionServer) {
dos.writeUTF(str);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}}
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.afei.chat.ClientChat.connectServer(ClientChat.java:115)
at com.afei.chat.ClientChat.chatFace(ClientChat.java:89)
at com.afei.chat.ClientChat.main(ClientChat.java:27)出错信息如:
package com.afei.chat;import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.*;
public class ClientChat extends Frame {
String useName;
TextField textInput = new TextField(5);
TextArea textShow = new TextArea(10, 40);
Socket socket=null;
boolean isConnectionServer = false;// 用于判断连接到服务器没有
DataOutputStream dos = null; public static void main(String[] args) {
new ClientChat().chatFace(); } // 聊天界面
public void chatFace() {
this.setLocation(200, 39);// 起始位置
this.setSize(400, 500); //
this.setTitle(useName + "的聊天窗口");// 窗口的标题
this.addWindowListener(new WindowListener() { @Override
public void windowClosed(WindowEvent e) {
// TODO Auto-generated method stub } @Override
public void windowClosing(WindowEvent e) {
System.exit(0);// 正常退出 } @Override
public void windowDeactivated(WindowEvent e) {
// TODO Auto-generated method stub } @Override
public void windowDeiconified(WindowEvent e) {
// TODO Auto-generated method stub } @Override
public void windowIconified(WindowEvent e) {
// TODO Auto-generated method stub } @Override
public void windowOpened(WindowEvent e) {
// TODO Auto-generated method stub } @Override
public void windowActivated(WindowEvent e) {
// TODO Auto-generated method stub } } );// 窗口监听。只要关闭窗口用的
this.add(textShow, new BorderLayout().NORTH);// 增加显示组件
this.add(textInput, new BorderLayout().SOUTH); // 增加输入组件
this.pack();// 消除空格
connectServer();//连接服务器
textInput.addActionListener(new TextFieldListener()); this.setVisible(true);// 窗口可见 } // 敲回车键的监听类
private class TextFieldListener implements ActionListener { @Override
public void actionPerformed(ActionEvent e) {
String str = textInput.getText();
textShow.setText(str);
textInput.setText(null);
System.out.println(socket);
//connectServer();
sendToServer(str); } } // 连接服务器
public void connectServer() {
try {
socket=new Socket("127.0.0.1",7788);
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} // 发送数据到服务器
public void sendToServer(String str) {
try {
//dos = new DataOutputStream(socket.getOutputStream());
while (isConnectionServer) {
dos.writeUTF(str);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}}
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.afei.chat.ClientChat.connectServer(ClientChat.java:115)
at com.afei.chat.ClientChat.chatFace(ClientChat.java:89)
at com.afei.chat.ClientChat.main(ClientChat.java:27)出错信息如:
解决方案 »
- 何为原子访问(atomic access)
- 急急急!!!!!在linux 环境下,读取windows下的Excel文件。
- 用扫码枪入库问题
- SWT中button如何监听回车?
- 如何在界面中显示JRadioButton
- ResultSet.getString()无法读取值为0000-00-00 00:00:00的Mysql datetime型数据
- [基础问题]classpath哪种写法更正确,更正规
- 在JB9 里能不能在一个面板里实例化一个显示时间的线程!
- jTextArea如何得到某行上的字串?
- 关于main()方法的一个问题
- '\n'和'\r‘到底是怎么回事?
- 用输入输出解决两数求和问题(不用Scanner类来写)
请先启动,服务端。再启动,客户端。
import java.awt.Frame;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.*;
public class ClientChat extends Frame {
String useName;
TextField textInput = new TextField(5);
TextArea textShow = new TextArea(10, 40);
Socket socket=null;
boolean isConnectionServer = false;// 用于判断连接到服务器没有
DataOutputStream dos = null; public static void main(String[] args) {
new ClientChat().chatFace(); } // 聊天界面
public void chatFace() {
this.setLocation(200, 39);// 起始位置
this.setSize(400, 500); //
this.setTitle(useName + "的聊天窗口");// 窗口的标题
this.addWindowListener(new WindowListener() { @Override
public void windowClosed(WindowEvent e) {
// TODO Auto-generated method stub } @Override
public void windowClosing(WindowEvent e) {
System.exit(0);// 正常退出 } @Override
public void windowDeactivated(WindowEvent e) {
// TODO Auto-generated method stub } @Override
public void windowDeiconified(WindowEvent e) {
// TODO Auto-generated method stub } @Override
public void windowIconified(WindowEvent e) {
// TODO Auto-generated method stub } @Override
public void windowOpened(WindowEvent e) {
// TODO Auto-generated method stub } @Override
public void windowActivated(WindowEvent e) {
// TODO Auto-generated method stub } } );// 窗口监听。只要关闭窗口用的
this.add(textShow, new BorderLayout().NORTH);// 增加显示组件
this.add(textInput, new BorderLayout().SOUTH); // 增加输入组件
this.pack();// 消除空格
connectServer();//连接服务器
textInput.addActionListener(new TextFieldListener()); this.setVisible(true);// 窗口可见 } // 敲回车键的监听类
private class TextFieldListener implements ActionListener { @Override
public void actionPerformed(ActionEvent e) {
String str = textInput.getText();
textShow.setText(str);
textInput.setText(null);
System.out.println(socket);
//connectServer();
sendToServer(str); } } // 连接服务器
public void connectServer() {
try {
socket=new Socket("127.0.0.1",7788);
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} // 发送数据到服务器
public void sendToServer(String str) {
try {
//dos = new DataOutputStream(socket.getOutputStream());
while (isConnectionServer) {
dos.writeUTF(str);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}}
ServerChat.javaimport java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;public class ServerChat {
private ServerSocket ss;
Socket s = null;
private int portNo;//端口号
public ServerChat( int portNo) {
super();
this.portNo = portNo;
try {
this.ss = new ServerSocket(this.portNo);
} catch (IOException e) {
e.printStackTrace();
}
} public static void main(String[] args) {
int portNo = 7788;
ServerChat serverChat = new ServerChat(portNo);
InputStream in = null;
DataInputStream dataIn = null;
String strMessage = "";
try {
serverChat.s = serverChat.ss.accept();//与客户端建立Socket连接
in = serverChat.s.getInputStream();
dataIn = new DataInputStream(in);
while(dataIn.available()!=-1 && !(strMessage = dataIn.readUTF()).equals("end")) {
System.out.println("Client: " + strMessage);
strMessage = "";
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("客户端程序退出!");
}
}
}
// 连接服务器
public void connectServer() {
try {
socket = new Socket("127.0.0.1", 7788);
isConnectionServer = true;//连接成功
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}// 发送数据到服务器
public void sendToServer(String str) {
try {
dos = new DataOutputStream(socket.getOutputStream());
if (isConnectionServer) {
dos.writeUTF(str);
dos.flush();
}
} catch (IOException e) {
e.printStackTrace();
}
}
再运行 ClientChat.java 客户端
如何开发一个Server-Client模型的程序 开发原理:
服务器,使用ServerSocket监听指定的端口,端口可以随意指定(由于1024以下的端口通常属于保留端口,在一些操作系统中不可以随意使用,所以建议使用大于1024的端口),等待客户连接请求,客户连接后,会话产生;在完成会话后,关闭连接。 客户端,使用Socket对网络上某一个服务器的某一个端口发出连接请求,一旦连接成功,打开会话;会话完成后,关闭Socket。