[Link]
html
java socket chat
The client
[Link]
package [Link]; import [Link]; import [Link]; import [Link]; import [Link]; import [Link]; import [Link]; import [Link]; import [Link]; import [Link]; import [Link]; import [Link]; import [Link]; /** * A simple Swing-based client for the chat server. * textarea to see the whole dialog. * * The client follows the Chat Protocol which is as follows. * When the server sends "SUBMITNAME" the client replies with the * desired screen name. "SUBMITNAME" * requests as long as the client submits screen names that are * already in use. When the server sends a line beginning The server will keep sending Graphically * it is a frame with a text field for entering messages and a
* with "NAMEACCEPTED" the client is now allowed to start * sending the server arbitrary strings to be broadcast to all * chatters connected to the server. When the server sends a * line beginning with "MESSAGE " then all characters following * this string should be displayed in its message area. */ public class ChatClient { BufferedReader in; PrintWriter out; JFrame frame = new JFrame("Chatter"); JTextField textField = new JTextField(40); JTextArea messageArea = new JTextArea(8, 40); /** * Constructs the client by laying out the GUI and registering a * listener with the textfield so that pressing Return in the * listener sends the textfield contents to the server. Note * however that the textfield is initially NOT editable, and * only becomes editable AFTER the client receives the NAMEACCEPTED * message from the server. */ public ChatClient() { // Layout GUI [Link](false); [Link](false); [Link]().add(textField, "North"); [Link]().add(new JScrollPane(messageArea), "Center"); [Link](); // Add Listeners [Link](new ActionListener() {
/** * Responds to pressing the enter key in the textfield by sending * the contents of the text field to the server. message. */ public void actionPerformed(ActionEvent e) { [Link]([Link]()); [Link](""); } }); } /** * Prompt for and return the address of the server. */ private String getServerAddress() { return [Link]( frame, "Enter IP Address of the Server:", "Welcome to the Chatter", JOptionPane.QUESTION_MESSAGE); } /** * Prompt for and return the desired screen name. */ private String getName() { return [Link]( frame, "Choose a screen name:", "Screen name selection", JOptionPane.PLAIN_MESSAGE); } /** Then clear * the text area in preparation for the next
* Connects to the server then enters the processing loop. */ private void run() throws IOException { // Make connection and initialize streams String serverAddress = getServerAddress(); Socket socket = new Socket(serverAddress, 9001); in = new BufferedReader(new InputStreamReader( [Link]())); out = new PrintWriter([Link](), true); // Process all messages from server, according to the protocol. while (true) { String line = [Link](); if ([Link]("SUBMITNAME")) { [Link](getName()); } else if ([Link]("NAMEACCEPTED")) { [Link](true); } else if ([Link]("MESSAGE")) { [Link]([Link](8) + "\n"); } } } /** * Runs the client as an application with a closeable frame. */ public static void main(String[] args) throws Exception { ChatClient client = new ChatClient(); [Link](JFrame.EXIT_ON_CLO SE); [Link](true); [Link](); } }