Home > Back-end >  Small white for answers
Small white for answers

Time:11-20

When my client after closure, the server will report errors, have who can help me to solve this problem under the
 package com. Java; 

Import the Java. IO. BufferedReader;
import java.io.IOException;
Import the Java. IO. InputStreamReader;
Import the Java. IO. OutputStreamWriter;
The import java.net.ServerSocket;
The import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

/* *
* the server
* function: receive the client sends the message, and then sent to all online clients
*/
Public class Server {
Public static List Sockets=new ArrayList (a);
Public static void main (String [] args) throws IOException {
System. The out. Println (" the server to start ");
ServerSocket server=new ServerSocket (2000);
While (true) {
System. The out. Println (" waiting for the client connection ");
The Socket s=server. The accept ();
Sockets. The add (s);
System. The out. Println (" the client is connected, the number of online "+ sockets. The size ());
New ServerThread (s). The start ();
}
}

}
The class ServerThread extends Thread {
Private Socket s;
Public ServerThread (Socket s) {
Enclosing the s=s;
}

@ Override
Public void the run () {
Try {
BufferedReader br=
New BufferedReader (
New InputStreamReader (s.g etInputStream ()));
//get the logged in user's user name, and then send all clients welcome XXX login
String username=br. ReadLine ();
For (Socket Socket: Server sockets) {
OutputStreamWriter osw=
New OutputStreamWriter (socket. GetOutputStream ());
Osw. Write (" [system messages] "+ username +" login \ n ");

Osw. Flush ();
}
//receive chat messages, and then forwarded to all the clients
While (true) {
The String content=br. ReadLine ();
For (Socket Socket: Server sockets) {
OutputStreamWriter osw=
New OutputStreamWriter (socket. GetOutputStream ());
Osw. Write (" : "username + + content +" \ n ");

Osw. Flush ();
}
System. Out. Println (" log: "+ username +" : "+ content);

}
} the catch (IOException e) {

e.printStackTrace();
}

}
}

 package com. Java; 

The import javax.mail. Swing. *;
import java.awt.*;
Import the Java. The awt. Event. An ActionEvent;
Import the Java. The awt. Event. ActionListener;
Import the Java. The awt. Event. WindowAdapter;
Import the Java. The awt. Event. WindowEvent;
Import the Java. IO. BufferedReader;
import java.io.IOException;
Import the Java. IO. InputStreamReader;
Import the Java. IO. OutputStreamWriter;
The import java.net.Socket;

/* *
* interact with the server
* receive server sends message
* to the server sends the message
*/
Public class Client extends JFrame {
Public static void main (String [] args) {
The new Login ();
}
Private Socket s;
private String username;

JPanel jp1, jp2;
JTextArea jta.//multiline text, which is used to display content
JTextField JTF;//a single text, which is used to input content
The JScrollPane JSP;//the scroll bar
Public Client (Socket s, String username) {
Enclosing the s=s;
Enclosing the username=username;
Jp1=new JPanel ();
Jta=new JTextArea (20, 30);/line/column
JSP=new JScrollPane (jta);//the jta has the function of the scroll bar

Jp1. Add (JSP);
//used to input content JTF panel
Jp2=new JPanel ();
JTF=new JTextField (30);
Jp2. Add (JTF);

Enclosing setTitle (" current user "+ username);
Enclosing setLayout (new BorderLayout ());
This. The add (BorderLayout. CENTER, jp1);
This. The add (BorderLayout. SOUTH, jp2);
This pack ();

this.setVisible(true);
Enclosing addWindowListener (new WindowAdapter () {
@ Override
Public void windowClosing (WindowEvent e) {
//the dispose ();
//s.i sClosed ();
System.exit(0);
}
});
JTF. AddActionListener (new MyListener ());
//start receiving server message thread
New GetThread (). The start ();
}
The class MyListener implements ActionListener {

@ Override
Public void actionPerformed (an ActionEvent e) {
/* jta. SetText (jta. GetText () + "\ n" + JTF. GetText ());
JTF. SetText (" "); */

//to the server sends the message
The String content=JTF. GetText ();
Try {
OutputStreamWriter osw=
New OutputStreamWriter (s.g etOutputStream ());
(osw. Write the content + "\ n");
Osw. Flush ();
{} the catch (IOException IOException)
IoException. PrintStackTrace ();
}
}
}

The class GetThread extends Thread {
Public void the run () {
Try {
BufferedReader br=
New BufferedReader (
New InputStreamReader (s.g etInputStream ()));
While (true) {
The String content=br. ReadLine ();
Jta. SetText (jta. GetText () + "\ n" + content);
JTF. SetText (" ");
}
} the catch (IOException e) {
e.printStackTrace();
}
}
}
}



 package com. Java; 

The import javax.mail. Swing. *;
import java.awt.*;
Import the Java. The awt. Event. An ActionEvent;
Import the Java. The awt. Event. ActionListener;
import java.io.IOException;
Import the Java. IO. OutputStreamWriter;
The import java.net.Socket;

/* *
* login window
* records user name
*/
Public class Login extends JFrame implements ActionListener {
JLabel label=new JLabel (" please enter the user name ");
JTextField JTF=new JTextField (20);
Public the Login () {
Enclosing setLayout (new FlowLayout ());
This. The add (label);
This. The add (JTF);
This pack ();
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related