Here is the code for the Server.
001 | package chatappserver; |
005 | import java.util.Vector; |
008 | public class ChatappServer { |
010 | private static int port = 1234 ; |
011 | private static ServerSocket server = null ; |
012 | private static Socket clientSocket; |
013 | private static String line; |
014 | private static BufferedReader streamIn; |
015 | private static PrintStream streamOut; |
018 | private static Vector<String> usernames = new Vector<String>(); |
019 | private static Vector<PrintStream> streams = new Vector<PrintStream>(); |
021 | public static void main(String[] args) throws IOException{ |
024 | System.out.println( "Connecting to port " + port + " ...." ); |
025 | server = new ServerSocket(port); |
026 | System.out.println( "Chat application server is now running.." ); |
028 | clientSocket = server.accept(); |
029 | chatHandler c = new chatHandler(clientSocket); |
033 | catch (IOException e){ |
034 | System.out.println( "Couldn't connect to the port!" ); |
041 | private static class chatHandler extends Thread{ |
043 | private Socket clientSocket; |
045 | public chatHandler(Socket clientSocket){ |
046 | super ( "chatHandler" ); |
047 | this .clientSocket = clientSocket; |
053 | streamIn = new BufferedReader( new InputStreamReader(clientSocket.getInputStream())); |
054 | streamOut = new PrintStream(clientSocket.getOutputStream(), true ); |
057 | streamOut.println( "Username" ); |
058 | String line = streamIn.readLine(); |
063 | synchronized (usernames){ |
064 | if (!usernames.contains(line)){ |
071 | System.out.println(e); |
074 | streamOut.println( "Welcome" ); |
075 | streams.add(streamOut); |
078 | String message = streamIn.readLine(); |
082 | for (PrintStream stream : streams){ |
083 | stream.println( "From " + line + ": " + message); |
088 | catch (IOException e){ |
089 | System.out.println(e); |
093 | if (line != null && streamOut != null ){ |
094 | usernames.remove(line); |
095 | streams.remove(streamOut); |
098 | clientSocket.close(); |
100 | catch (IOException e){ |
101 | System.out.println(e); |
The Client code is here..
05 | import javax.swing.JFrame; |
06 | import javax.swing.JTextArea; |
07 | import javax.swing.JTextField; |
08 | import javax.swing.JButton; |
10 | import java.awt.event.*; |
12 | public class ChatappClient { |
14 | private static int port = 1234 ; |
15 | JFrame window = new JFrame( "Chat" ); |
16 | JButton sendBox = new JButton( "Send" ); |
17 | JTextField inputMsg = new JTextField( 35 ); |
18 | JTextArea outputMsg = new JTextArea( 10 , 35 ); |
19 | private static BufferedReader streamIn; |
20 | private static PrintStream streamOut; |
22 | public static void main(String[] args) throws Exception{ |
23 | ChatappClient client = new ChatappClient(); |
24 | client.window.setVisible( true ); |
25 | client.window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
29 | public ChatappClient(){ |
31 | inputMsg.setSize( 40 , 20 ); |
32 | sendBox.setSize( 5 , 10 ); |
33 | outputMsg.setSize( 35 , 50 ); |
34 | inputMsg.setEditable( false ); |
35 | outputMsg.setEditable( false ); |
36 | window.getContentPane().add(inputMsg, "South" ); |
37 | window.getContentPane().add(outputMsg, "East" ); |
38 | window.getContentPane().add(sendBox, "West" ); |
40 | sendBox.addActionListener( new ActionListener(){ |
41 | public void actionPerformed(ActionEvent e){ |
42 | streamOut.println(inputMsg.getText()); |
46 | inputMsg.addActionListener( new ActionListener(){ |
47 | public void actionPerformed(ActionEvent e){ |
48 | streamOut.println(inputMsg.getText()); |
54 | private String getUsername(){ |
55 | return JOptionPane.showInternalInputDialog(window, "Server IP Address:" , "Welcome to Chat" , JOptionPane.QUESTION_MESSAGE); |
58 | private void run() throws IOException{ |
59 | Socket clientSocket = new Socket( "localhost" , port); |
60 | streamIn = new BufferedReader( new InputStreamReader(clientSocket.getInputStream())); |
61 | streamOut = new PrintStream(clientSocket.getOutputStream(), true ); |
64 | String line = streamIn.readLine(); |
65 | if (line.startsWith( "Username" )){ |
66 | streamOut.println(getUsername()); |
67 | } else if (line.startsWith( "Welcome" )){ |
68 | inputMsg.setEditable( true ); |
69 | } else if (line.startsWith( "From" )){ |
70 | outputMsg.append(line.substring( 10 )+ "\n" ); |
출저 : http://www.dreamincode.net/forums/topic/277518-chat-using-socketssingle-server-with-multiple-clients/