Home > Back-end >  Wrote a chat in the socket, a computer can be used in the computer program is no response, Eclipse w
Wrote a chat in the socket, a computer can be used in the computer program is no response, Eclipse w

Time:09-20

Implementation way is Client0 Client1, Client2 messaging Server forwards the message to others
Server:
 package JChat; 

Import the Java. The awt. Event. An ActionEvent;
Import the Java. The awt. Event. ActionListener;
Import the Java. IO. A DataInputStream;
Import the Java. IO. DataOutputStream;
import java.io.IOException;
The import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

The import javax.mail. Swing. JButton;
The import javax.mail. Swing. JFrame;
The import javax.mail. Swing. JTextArea;
The import javax.mail. Swing. JTextField;

Public class Server {

Public static void main (String [] args) throws the Exception {

JFrame f=new JFrame ();
F.s etTitle (" Server ");

F.s etSize (400, 400);
F.s etLocation (100, 200);
F.s etLayout (null);
F.s etResizable (false);

JButton b=new JButton (" sent ");
B.s etBounds (300, 310, 80, 30);
F.a dd (b);


Final JTextField tf=new JTextField ();
Tf. SetBounds (10, 310, 280, 30);
F.a dd (tf);

Final JTextArea ta=new JTextArea ();
Ta. SetBounds (10, 10, 300, 280);
F.a dd (ta);

F.s etDefaultCloseOperation (JFrame. EXIT_ON_CLOSE);
F.s etVisible (true);

ServerSocket ss2=new ServerSocket (8882);
System. The out. Println (" listenning on port: 8882 ");
Final Socket s2=ss2. The accept ();

ServerSocket ss1=new ServerSocket (8881);
System. The out. Println (" listenning on port: 8881 ");
Final Socket s1=ss1. The accept ();

ServerSocket ss=new ServerSocket (8880);
System. The out. Println (" listenning on port: 8880 ");
Final Socket s=ss. The accept ();

//receive c1
New Thread () {
Public void the run () {
While (true) {
Try {
A DataInputStream dis=new a DataInputStream (
S1. GetInputStream ());
A String of text=dis. ReadUTF ();
Ta. Append (text + "\ r \ n");
Int l=text. The length ();
If (l<2) {
DataOutputStream DOS=new DataOutputStream (
S.g etOutputStream ());
DOS. WriteUTF (text);
DataOutputStream dos2=new DataOutputStream (
S2. GetOutputStream ());
Dos2. WriteUTF (text);
}
The else {
If (text. CharAt (l - 2)=='@' & amp; & Text. CharAt (l - 1)=='0') {

DataOutputStream DOS=new DataOutputStream (
S.g etOutputStream ());
DOS. WriteUTF (text);
}
Else if (text. CharAt (l - 2)=='@' & amp; & Text. CharAt (l - 1)=='2') {
DataOutputStream dos2=new DataOutputStream (
S2. GetOutputStream ());
Dos2. WriteUTF (text);
}

The else {
DataOutputStream DOS=new DataOutputStream (
S.g etOutputStream ());
DOS. WriteUTF (text);
DataOutputStream dos2=new DataOutputStream (
S2. GetOutputStream ());
Dos2. WriteUTF (text);
}
}
}
The catch (Exception e) {
e.printStackTrace();
}
}
}
};
}
}

Client2 (Client1 and Client0 similar) :
 package JChat; 

Import the Java. The awt. Event. An ActionEvent;
Import the Java. The awt. Event. ActionListener;
Import the Java. IO. A DataInputStream;
Import the Java. IO. DataOutputStream;
import java.io.IOException;
The import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;

The import javax.mail. Swing. JButton;
The import javax.mail. Swing. JFrame;
The import javax.mail. Swing. JTextArea;
The import javax.mail. Swing. JTextField;

Public class Client2 {

Public static void main (String [] args) throws the Exception {

JFrame f=new JFrame ();
F.s etTitle (" Client2 ");

F.s etSize (400, 450);
F.s etLocation (600, 200);
F.s etLayout (null);
F.s etResizable (false);

JButton b=new JButton (" sent ");
B.s etBounds (300, 310, 80, 30);
F.a dd (b);

JButton c=new JButton (" @ client0 ");
C.s. etBounds (10, 350, 150, 30);
F.a dd (c);

JButton c1=new JButton (" @ client1 ");
C1. SetBounds (200, 350, 150, 30);
F.a dd (c1);

Final JTextField tf=new JTextField ();
Tf. SetBounds (10, 310, 280, 30);
F.a dd (tf);

Final JTextArea ta=new JTextArea ();
Ta. SetBounds (10, 10, 300, 280);
F.a dd (ta);

F.s etDefaultCloseOperation (JFrame. EXIT_ON_CLOSE);
F.s etVisible (true);

Final Socket s2=new Socket (" 127.0.0.3 ", 8882);

New Thread () {
Public void the run () {
While (true) {
Try {
A DataInputStream dis=new a DataInputStream (
S2. GetInputStream ());
A String of text=dis. ReadUTF ();
Ta. Append (text + "\ r \ n");
} the catch (Exception e) {
e.printStackTrace();
}
}

}
}. The start ();

B.a ddActionListener (new ActionListener () {

@ Override
Public void actionPerformed (an ActionEvent e) {

String text=tf. The getText ();
Ta. Append (text + "\ r \ n");

Try {
DataOutputStream DOS=new DataOutputStream (
S2. GetOutputStream ());
DOS. WriteUTF (text);
{} the catch (Exception ex)
ex.printStackTrace();
}

}
});

nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related