java socket chat room

Related articles:
wife want to derail the thought of the day struggling
chat rooms (chat room) is an online space, in order to ensure the focus of conversation, chat rooms usually have a certain topic of conversation now offers chat, private messages, the action of these basic. additional features chat and filter the user list function so beautiful anchor kicking completed, check IP, the appointment of the main room for improvement in the future are: stability: the current chat room has not been a large amount of test users, but also stability can not be fully guaranteed. Because it is a single circulation process server, a user communication deadlock will lead to a deadlock everyone. If concurrent multi-process server, so that the stability can be improved. but such a system will be much larger server resource consumption . Server:
Package ChatIV;
Import the java.net *;.
Import the java.io. *;
Import Classes in java.util *;.
// broadcast chat room server
public class Server {
public static void main (String args []) {
the try {
the ServerSocket the ServerSocket new new = SS (8186); // achieve the Socket
List = new new Sockets the ArrayList (); // Create a set of stored text message
the while (to true) {
the Socket S = Ss.accept (); // listener 8186
sockets.add (S); // add S Socket objects to the collection, the stored contents to the collection heard
Thread t1 = new ChatThread(s,sockets);//线程
t1.start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
class ChatThread extends Thread{
Socket s;
List sockets;
public ChatThread(Socket s,List sockets){
this.s=s;
this.sockets=sockets;
}
public void run(){
try {
BufferedReader in = new BufferedReader(
new InputStreamReader(s.getInputStream()));//包装成字符流
while(true){
String str = in.readLine();//读BufferedReader
for(int i=0;i<sockets.size();i++){//利用For循环遍历集合
Socket s2 = (Socket) sockets.get ( i); // Create the Socket object S2, turn into strong Socket, and acquires index
PrintWriter out = new PrintWriter (s2.getOutputStream ( )); // text stream output
out.println (str); // print character
out.flush (); // Flushes the stream
}
}
} the catch (IOException E) {}
the finally {
the try {
S.CLOSE ();
} the catch (IOException E) {}
}
}
}
Client:
Package ChatIV;
Import java.awt.event.ActionEvent The;
Import java.awt.event.ActionListener;
Import java.io.BufferedReader;
Import java.io.IOException;
Import the java.io.InputStreamReader;
Import the java.io .PrintWriter;
Import java.net.Socket;
import java.net.UnknownHostException;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
//广播聊天室客户端
public class Client {
JTextArea jta;
JTextField jtf;
BufferedReader in;
PrintWriter out;
//Swing画一个界面
private void initGUI(){
JFrame f=new JFrame("Client");
f.setSize(400,300);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jta=new JTextArea();
jta.setEditable(false);
f.add(new JScrollPane(jta));
jtf=new JTextField();
f.add(jtf,"South");
f.setVisible (to true);
jtf.addActionListener (the ActionListener new new () {
public void the actionPerformed (the ActionEvent the arg0) {
String text = jtf.getText ();
jtf.setText ( "");
Out.println (text);
OUT. the flush ();
}
});
}
// initialize connection
Private void initNet () {
the try {
Socket S = new new Socket ( "127.0.0.1", 8186); // Create Socket 8186 port, random port number, the port used to avoid to
out = new PrintWriter (s.getOutputStream () ); // text stream output, luoti.13229.com, obtained using getOutputStream Socket object ()
in the BufferedReader new new = (the InputStreamReader new new (s.getInputStream ())); / / character stream packaged
} the catch (UnknownHostException E) {
e.printStackTrace ();
} the catch (IOException E) {
e.printStackTrace();
}
}
//消息接收
private void receive(){
try {
while(true){
String str = in.readLine();//读BufferedReader
if(str==null){
return ;
}
jta.append("说"+str+"\n");//加载到JTextArea中,显示
}
} catch (IOException e) {
e.printStackTrace();
}
}
private Client(){
this.initGUI();
this.initNet();
}
public static void main(String args[]){
Client c = new Client();
c.receive();
}
}

Reproduced in: https: //www.cnblogs.com/521taobao/archive/2012/03/17/2402454.html

Guess you like

Origin blog.csdn.net/weixin_34403693/article/details/93355871