Results 1 to 20 of 20
- 09-13-2011, 02:46 AM #1
Member
- Join Date
- Jul 2011
- Location
- kent, UK
- Posts
- 47
- Rep Power
- 0
message passing in Client/Server Chat problem
Hello, I am trying to send a message from one client to another through the server but I have run out of ideas on what to do. when the user selects a name on the userlist(JList), the user should send a message to the name selected. I have done a substantial amount of trials here and i have been on this pretty much a while now.... please help me. Thank you
the code is quite bulky but i have tried to remove most irrelevant things away from it.
The ServerChat.java
The ServerThread.javaJava Code:// Java core packages import java.io.*; import java.net.*; import java.util.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ServerChat extends JFrame { private ObjectInputStream input; private ObjectOutputStream output; private JTextField enterField; public static JTextArea displayArea; private ServerSocket server; private Socket connection; public static ServerThread c1[]; //private Client Cname; protected static String clientList[]; private static int counter = 0; public ServerChat() { super( "Server" ); Container container = getContentPane(); clientList = new String[100]; // create enterField and register listener enterField = new JTextField(); enterField.setEnabled( false ); enterField.addActionListener( new ActionListener() { // send message to client public void actionPerformed( ActionEvent event ) { //sendData( event.getActionCommand() ); } } // end anonymous inner class ); // end call to addActionListener container.add( enterField, BorderLayout.NORTH ); // create displayArea displayArea = new JTextArea(); container.add( new JScrollPane( displayArea ), BorderLayout.CENTER ); setSize( 300, 150 ); setVisible( true ); } public void runServer() { // set up server to receive connections; // process connections try { // Step 1: Create a ServerSocket. server = new ServerSocket( 5130, 100); c1 = new ServerThread[100]; while ( true ) { // Step 2: Wait for a connection. waitForConnection(); c1[counter] = new ServerThread(connection,counter); c1[counter].start(); ++counter; } } // process EOFException when client closes connection catch ( EOFException eofException ) { System.out.println( "Client terminated connection" ); } // process problems with I/O catch ( IOException ioException ) { ioException.printStackTrace(); } } private void waitForConnection() throws IOException { displayArea.append( "\nWaiting for connection " ); // allow server to accept a connection connection = server.accept(); displayArea.append( "\nClient connected"); } public static void broadCast(String Cname) { for (int i=0;i<counter ; i++) { c1[i].sendData("."+Cname); } } public static void main( String args[] ) { ServerChat application = new ServerChat(); application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE ); application.runServer(); } }
The ClientChat.javaJava Code:import java.io.*; import java.net.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ServerThread extends Thread { private Socket connection; private ObjectOutputStream output; private ObjectInputStream input; private int counter; public ServerThread(Socket con,int count) { connection = con; counter = count; } public void run() { try { getStreams(); handShake(); processConnection(); System.out.println("Hanshake Complete"); //closeConnection(); } // process EOFException when client closes connection catch ( EOFException eofException ) { System.out.println( "Client terminated connection" ); } // process problems with I/O catch ( IOException ioException ) { ioException.printStackTrace(); } } private void getStreams() throws IOException { // set up output stream for objects output = new ObjectOutputStream(connection.getOutputStream() ); // flush output buffer to send header information output.flush(); input = new ObjectInputStream(connection.getInputStream() ); } private void handShake() throws IOException { String message; try { int i; message = (String) input.readObject(); ServerChat.clientList[counter]= message; ServerChat.displayArea.append( "\nClient connected : "+message); for (i=0; i<counter;i++ ) { output.writeObject(ServerChat.clientList[i]); output.flush(); } output.writeObject("."); output.flush(); System.out.println("read message in S_Hanshake after try and for block = " +message); ServerChat.broadCast(ServerChat.clientList[i]); } // catch problems reading from client catch ( ClassNotFoundException classNotFoundException ) { System.out.println( "\nUnknown object type received" ); } catch ( IOException ioException ) { ioException.printStackTrace(); } } public void sendData(String message ) { // send object to server try { output.writeObject( message ); output.flush(); //ServerChat.broadCast(ServerChat.clientList[i]); } catch ( IOException ioException ) { System.out.println( "\nError writing object3" ); } //return message; } private void processConnection() throws IOException { // send connection successful message to client String message = "";// = "SERVER>>> Connection successful [2]"; try { do{ message=( String ) input.readObject(); System.out.println("\n"+message); }while ( !message.equals( "TERMINATE" ) ); } catch ( ClassNotFoundException classNotFoundException ) { System.out.println( "\nUnknown object type received" ); } catch ( IOException ioException ) { ioException.printStackTrace(); } } private void closeConnection() throws IOException { ServerChat.displayArea.append( "\nUser terminated connection" ); output.close(); input.close(); connection.close(); } }
Java Code:import java.io.*; import java.net.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ClientChat extends JFrame { private Toolkit toolkit; private JLabel msgLabel; private JButton sendBtn; private JTextArea genMsg; public static JList userList; private JTextField msgF; private ObjectOutputStream output; private ObjectInputStream input; private Socket client; private String chatServer; private int serverport; private String Client_name; String value[]; int value_counter; public ClientChat( String host, int port,String C_Name){ // set server to which this client connects chatServer = host; serverport = port; Client_name = C_Name; value = new String [100]; value_counter=0; toolkit = Toolkit.getDefaultToolkit(); if(toolkit.getScreenSize().getWidth() > 600) setSize(600, 605); else setSize((int)toolkit.getScreenSize().getWidth(),(int)toolkit.getScreenSize().getHeight() - 20); setResizable(false); Dimension dimension = getSize(); setLayout(new FlowLayout()); setTitle("FRESHER MARKETING COMPANY For "+ C_Name); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent evt) { System.exit(0);}}); Container container = getContentPane(); container.setLayout(new FlowLayout()); // create an ImageIcon ImageIcon banner =new ImageIcon("images\\defaultbanner.gif"); JLabel bannerLabel = new JLabel(banner); container.add(bannerLabel); // create General Message Screen genMsg = new JTextArea(30,43); genMsg.setEditable(false); genMsg.setFont(new java.awt.Font("Times New Roman", 0, 12)); // NOI18N container.add( new JScrollPane( genMsg )); JPanel genMsgPanel = new JPanel(); genMsgPanel.setLayout(new BorderLayout()); genMsgPanel.add(new JScrollPane(genMsg), BorderLayout.EAST); genMsgPanel.setBorder(BorderFactory.createLineBorder(Color.black)); container.add(genMsgPanel); // create Friend List View userList = new JList(); userList.setPreferredSize(new Dimension(150,423)); userList.setFont(new java.awt.Font("Times New Roman", 0, 12)); // NOI18N container.add(userList); JPanel userListPanel = new JPanel(); userListPanel.setLayout(new BorderLayout()); userListPanel.add(userList, BorderLayout.CENTER); userListPanel.setBorder(BorderFactory.createLineBorder(Color.black)); container.add(userListPanel); msgLabel = new JLabel ("Message:"); container.add(msgLabel); JPanel msgFPanel = new JPanel(); msgFPanel.setLayout(new BorderLayout()); msgFPanel.add(new JScrollPane(msgLabel), BorderLayout.WEST); container.add(msgFPanel); // create Message Field msgF = new JTextField(37); msgF.setEnabled( true ); msgF.setText(""); msgF.requestFocus(); //msgF msgFPanel.add(new JScrollPane(msgF), BorderLayout.CENTER); // create Send Button sendBtn = new JButton ("Send1"); container.add(sendBtn); sendBtn.addActionListener( new ActionListener() { // send message to server public void actionPerformed( ActionEvent event ) { //msgF.getText(); sendData( msgF.getText()); msgF.setText(""); } } // end anonymous inner class ); // end call to addActionListener setVisible( true ); msgFPanel.add(new JScrollPane(sendBtn), BorderLayout.EAST); } public void runClient() { // connect to server, get streams, process connection try { // Step 1: Create a Socket to make connection connectToServer(); // Step 2: Get the input and output streams getStreams(); handShake(); // Step 3: Process connection processConnection(); // Step 4: Close connection //closeConnection(); }// server closed connection catch ( EOFException eofException ) { System.out.println( "Server terminated connection" ); } // process problems communicating with server catch ( IOException ioException ) { ioException.printStackTrace(); } } private void connectToServer() throws IOException { genMsg.setText( "Attempting connection\n" ); System.out.println("Attempting connection\n"); // create Socket to make connection to server client = new Socket(InetAddress.getByName( chatServer ), serverport ); // display connection information genMsg.append( "Connected to: " +client.getInetAddress().getHostName() ); } private void getStreams() throws IOException { // set up output stream for objects output = new ObjectOutputStream(client.getOutputStream() ); // flush output buffer to send header information output.flush(); System.out.println("Got I/O streams"); // set up input stream for objects input = new ObjectInputStream(client.getInputStream() ); } private void handShake() throws IOException { String message; genMsg.append("\nMy name is: " +Client_name); try { output.writeObject(Client_name); output.flush(); message = (String) input.readObject(); while ( !message.startsWith( "." ) ) { value[value_counter++] = message; message = (String) input.readObject(); } userList.setListData(value); } // process problems sending object catch ( IOException ioException ) { genMsg.append( "\nError writing object1" ); } catch ( ClassNotFoundException classNotFoundException ) { System.out.println( "\nUnknown object type received" ); } } private void processConnection() throws IOException { String message=""; // process messages sent from server do { // read message and display it try { message = ( String ) input.readObject(); if (message.startsWith(".")) { if(!message.equals("."+Client_name)) { value[value_counter++] =message.substring(1); userList.setListData(value); } } else { genMsg.append( "\n" + message ); genMsg.setCaretPosition(genMsg.getText().length() ); } } // catch problems reading from server catch ( ClassNotFoundException classNotFoundException ) { genMsg.append( "\nUnknown object type received" ); } catch ( IOException ioException ) { genMsg.append( "\nError writing object2" ); } } while ( !message.equals( "SERVER>>> TERMINATE" ) ); } // end method process connection private void sendData( String message ) { // send object to server try { output.writeObject( userList.getSelectedValue() +": "+message ); output.flush(); genMsg.append( "\nhere is the message: " + message ); } // process problems sending object catch ( IOException ioException ) { genMsg.append( "\nError writing object3" ); } } public static void main( String args[] ) { ClientChat application; application = new ClientChat( "127.0.0.1",5130,args[0] ); application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE ); application.runClient(); } }Last edited by pappyj77; 09-13-2011 at 04:02 AM.
- 09-13-2011, 03:20 AM #2
Re: message passing in Client/Server Chat problem
Some questions about how the code is working.when the user selects a name on the userlist(JList), the user should send a message to the name selected
What does happen when you execute the program?
Does the client sent it ok?
Does the server get the message?
What does the server do when it gets the message?
Does the server send it ok?
Does the client get the message?
- 09-13-2011, 03:35 AM #3
Member
- Join Date
- Jul 2011
- Location
- kent, UK
- Posts
- 47
- Rep Power
- 0
Re: message passing in Client/Server Chat problem
the serverthread can read it ok but i just dnt know how to send it to my sever brodcast
- 09-13-2011, 03:38 AM #4
Re: message passing in Client/Server Chat problem
When I bring up two clients the first one's list only contains its name
the second client's list has no entries.
My test code:
Java Code:public static void main( String args[] ) { Thread t0 = new Thread(new Runnable() { public void run() { ServerChat servr = new ServerChat(); servr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE ); servr.runServer(); } }); t0.start(); Thread t1 = new Thread(new Runnable() { public void run() { ClientChat application = new ClientChat( "127.0.0.1",5130, "First" ); application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE ); application.runClient(); } }); t1.start(); Thread t2 = new Thread(new Runnable() { public void run() { ClientChat application = new ClientChat( "127.0.0.1",5130, "Second" ); application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE ); application.runClient(); } }); t2.start(); } // end main
- 09-13-2011, 03:45 AM #5
Member
- Join Date
- Jul 2011
- Location
- kent, UK
- Posts
- 47
- Rep Power
- 0
Re: message passing in Client/Server Chat problem
thats a bit weired i have it here fine....
- 09-13-2011, 03:47 AM #6
Re: message passing in Client/Server Chat problem
On my second test neither client got any names in the right hand list.
- 09-13-2011, 03:49 AM #7
Member
- Join Date
- Jul 2011
- Location
- kent, UK
- Posts
- 47
- Rep Power
- 0
Re: message passing in Client/Server Chat problem
ok... i will reupload the progra in a sec pls... thank you
- 09-13-2011, 04:03 AM #8
Member
- Join Date
- Jul 2011
- Location
- kent, UK
- Posts
- 47
- Rep Power
- 0
Re: message passing in Client/Server Chat problem
it has been updated. thank you...
- 09-13-2011, 04:07 AM #9
Re: message passing in Client/Server Chat problem
There are three programs. Which one is new?
- 09-13-2011, 04:13 AM #10
Member
- Join Date
- Jul 2011
- Location
- kent, UK
- Posts
- 47
- Rep Power
- 0
Re: message passing in Client/Server Chat problem
since the programs u have are not working as they should, i would suggest u replace all three, and remember to delete the class files previously created.i noticed sometimes they are not overwritten when a new program has been compiled... thnks
- 09-13-2011, 04:20 AM #11
Re: message passing in Client/Server Chat problem
Not much change.
The second client gets the name of the first one.
The first one does not get any names in the list.
- 09-13-2011, 04:25 AM #12
Member
- Join Date
- Jul 2011
- Location
- kent, UK
- Posts
- 47
- Rep Power
- 0
Re: message passing in Client/Server Chat problem
am sorry, i think the thread snippet u added must be the problem then i am not having any prolem with the JList
- 09-13-2011, 04:25 AM #13
Re: message passing in Client/Server Chat problem
You need to add a LOT more debug printlns to your code. To show what is happening when a message is received and where the logic flow goes.
- 09-13-2011, 04:27 AM #14
Re: message passing in Client/Server Chat problem
Sorry, I like the tests to fully start from a single program. I press one button and the code is executed.
Try the code I posted and see what happens with your version.
- 09-13-2011, 02:03 PM #15
Member
- Join Date
- Jul 2011
- Location
- kent, UK
- Posts
- 47
- Rep Power
- 0
Re: message passing in Client/Server Chat problem
yes i tried it with ur code and its definately not what i see when i run each client separatly.
- 09-13-2011, 02:35 PM #16
Re: message passing in Client/Server Chat problem
What's different?
- 09-13-2011, 02:37 PM #17
Member
- Join Date
- Jul 2011
- Location
- kent, UK
- Posts
- 47
- Rep Power
- 0
Re: message passing in Client/Server Chat problem
the Jlist does not update
- 09-13-2011, 02:49 PM #18
Re: message passing in Client/Server Chat problem
Is it a timing problem?
Does the client get the data to go in the list?
- 09-13-2011, 02:50 PM #19
Member
- Join Date
- Jul 2011
- Location
- kent, UK
- Posts
- 47
- Rep Power
- 0
Re: message passing in Client/Server Chat problem
Yes it is a timing problem.
it does get the data for the list.Last edited by Norm; 09-13-2011 at 02:53 PM.
- 09-13-2011, 02:54 PM #20
Similar Threads
-
Simple client and server chat system
By danborgir in forum New To JavaReplies: 1Last Post: 04-26-2011, 01:34 PM -
Multithreaded Client/Server Chat program
By f0ns in forum Threads and SynchronizationReplies: 3Last Post: 10-21-2009, 05:26 PM -
Multithread Chat server/client
By gwaldarick in forum Advanced JavaReplies: 3Last Post: 09-19-2009, 12:22 AM -
[SOLVED] UDP chat client server
By Koren3 in forum NetworkingReplies: 2Last Post: 04-25-2009, 01:51 AM -
passing info between server/client problem
By DarkBlaze in forum New To JavaReplies: 13Last Post: 07-24-2008, 03:14 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks