Results 1 to 14 of 14
Thread: Instant Messaging Program
- 04-28-2008, 06:19 AM #1
Instant Messaging Program
I want to try to make an instant messaging program. I'm having problems with it, and was wondering if someone could help me.
View class:
Client class:Java Code:import java.awt.Container; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JTextField; public class View extends JFrame implements ActionListener { Client myClient; Server myServer; private String myMessage; private JTextField myMessageTextField; private JTextField mySendMessageTextField; private JButton myEnterButton; private boolean isClient; public View(boolean isClient) { this.isClient = isClient; if(isClient) { myClient = new Client(this); } else { myServer = new Server(this); } Container contentPane = this.getContentPane(); this.setTitle("Instant Messaging"); this.setLayout(new GridLayout(3, 1)); this.setSize(300, 300); this.setLocationRelativeTo(null); myMessageTextField = new JTextField(); myMessageTextField.setEditable(false); contentPane.add(myMessageTextField); mySendMessageTextField = new JTextField(); contentPane.add(mySendMessageTextField); myEnterButton = new JButton("Send"); myEnterButton.addActionListener(this); contentPane.add(myEnterButton); this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.setVisible(true); } public String getText() { return mySendMessageTextField.getText(); } public void setMessage(String fromServer) { myMessage += "\n" + fromServer; myMessageTextField.setText(myMessage); } @Override public void actionPerformed(ActionEvent e) { if(e.getSource().equals(myEnterButton)); { if(isClient) { myClient.setText( this.mySendMessageTextField.getSelectedText()); } else { myServer.setText( this.mySendMessageTextField.getSelectedText()); } } } }
Server Class:Java Code:import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.Socket; import java.net.UnknownHostException; public class Client implements Runnable { Socket myClientSocket; PrintWriter out; BufferedReader in; String fromServer; String fromUser; View myView; Thread myThread; public Client(View aView) { try { myClientSocket = new Socket("JordanS", 4444); out = new PrintWriter(myClientSocket.getOutputStream(), true); in = new BufferedReader(new InputStreamReader(myClientSocket.getInputStream())); } catch (UnknownHostException e) { System.err.println("Don't know about host: taranis."); System.exit(1); } catch (IOException e) { System.err.println("Couldn't get I/O for the connection to: taranis."); System.exit(1); } myView = aView; this.start(); } public void start() { myThread = new Thread(this); myThread.start(); } public void stop() { myThread = null; } public static void main(String[] args) { View aView = new View(true); } @Override public void run() { while(true) { try { if((fromServer = in.readLine()) != null) { myView.setMessage(fromServer); } String sendText = this.getText(); if (sendText != ""); { out.println(fromUser); fromUser = ""; } } catch (IOException e) { this.closeConnection(); } } } private String getText() { return fromUser; } public String setText(String aMessage) { return fromUser; } private void closeConnection() { try { out.close(); in.close(); myClientSocket.close(); System.exit(0); } catch (IOException e) { e.printStackTrace(); } } }
Java Code:import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; import java.net.UnknownHostException; public class Server implements Runnable { ServerSocket myServerSocket; PrintWriter out; BufferedReader in; String fromServer; String fromUser; View myView; Socket clientSocket; Thread myThread; public Server(View aView) { ServerSocket serverSocket = null; try { serverSocket = new ServerSocket(4444); } catch (IOException e) { System.err.println("Could not listen on port: 4444."); System.exit(1); } clientSocket = null; try { clientSocket = serverSocket.accept(); } catch (IOException e) { System.err.println("Accept failed."); System.exit(1); } try { out = new PrintWriter(clientSocket.getOutputStream(), true); in = new BufferedReader(new InputStreamReader( clientSocket.getInputStream())); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } myView = aView; this.start(); } public void start() { myThread = new Thread(this); myThread.start(); } public void stop() { myThread = null; } public static void main(String[] args) { View aView = new View(false); } @Override public void run() { while(true) { try { if((fromServer = in.readLine()) != null) { myView.setMessage(fromServer); } String sendText = this.getText(); if (sendText != ""); { out.println(fromUser); fromUser = ""; } } catch (IOException e) { System.exit(0); } try { Thread.sleep(0); } catch(Exception e) { e.printStackTrace(); } } } private String getText() { return fromUser; } public String setText(String aMessage) { return fromUser; } private void closeConnection() { try { out.close(); in.close(); clientSocket.close(); myServerSocket.close(); } catch (IOException e) { e.printStackTrace(); } } }Last edited by Zosden; 04-28-2008 at 06:25 AM.
My IP address is 127.0.0.1
- 04-28-2008, 07:39 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Zosden, what is your problem. Can you provide more details about it. Did you get any errors?
- 04-28-2008, 07:44 AM #3
No I don't get any errors I just can't seem to get the program to read in the string from the other user. I'm new to networking so it could just be something dumb. In the end I would like to have this program set up then implement it into my tic tac toe project for my website.
My IP address is 127.0.0.1
- 04-28-2008, 08:09 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
ah, ok pal. Why I ask this is, have to test a lot. Just looking to give a hint without compiling and run. Hang-on, i'll try this.
- 04-28-2008, 08:35 AM #5
are you going to post a suggestion or not?
My IP address is 127.0.0.1
- 04-28-2008, 09:00 AM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
How did you handle the client request in the server, wise versa? Did you use any http response codes there? Seems to me it's not. Why I say is, if you use them easily you can send messages through the response data part.
By the way this is not a good choice pal, found it at the Client.
In your application it's fine. But it is not legal. Legal not says that it is incorrect. ;) And also you have empty body in the if loop. You have added ; at the end.Java Code:if (sendText != "");
Do it as follows.
Java Code:if (!sendText.isEmpty())
- 04-29-2008, 04:49 AM #7
I think the problem is that it just leaves the while loop for some reason I did a S.O.P within it and it just printed it out once.
My IP address is 127.0.0.1
- 04-29-2008, 04:51 AM #8
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Which while loop you are talking about Zosden?
- 04-29-2008, 06:16 AM #9
the while true loop in the run method. I work with threads a lot and i have no idea whats the matter. I think it has to do with the sockets
My IP address is 127.0.0.1
- 05-04-2008, 04:34 AM #10
What do you mean killtest
My IP address is 127.0.0.1
- 05-04-2008, 07:18 PM #11
Member
- Join Date
- May 2008
- Posts
- 2
- Rep Power
- 0
- 05-05-2008, 01:08 AM #12
I still don't know whats wrong with my program. If someone has any ideas or knows how to do it better let me know
My IP address is 127.0.0.1
- 05-05-2008, 07:14 AM #13
Just a few suggestions about the design.
1. You don't need the server to know about the view.
2. On the server keep a list of sessions. When a client connects for the first time it sends a session containing session ID 0. Then the server sends back a new allocated session ID.
3. A session is a class containing a few facts like the client name, session ID and last time of access (you want to kill an inactive session after a while)
4. Every message from the client to the servers always includes the session ID. For this you might implement a Message class.
5. The message can contain: requests for session (maybe with authentication), commands (list users for example) and messages for other users.
5. Messages from a client to other clients are distributed back by the server.
6. Messages are handled by threads created for that purpose or reused from a pool.
7. The GUI (View) uses the client but the client doesn't need the GUI to run.
Identified actors: Server, Client, Session, Message
Inside the server you have an "Incoming Message Dispatcher" and an "Outgoing Message Dispatcher". they get the incoming messages, distribute them to working threads and then distribute back to Clients the answers.
To keep it simple you can use UDP in the beginning.
Have fun :) You might attack Yahoo!'s market share with this :)Daniel @ [www.littletutorials.com]
Language is froth on the surface of thought
- 05-05-2008, 07:18 AM #14
Similar Threads
-
Executing a program within a program
By gibsonrocker800 in forum New To JavaReplies: 5Last Post: 05-12-2008, 08:24 AM -
How to execute an External Program through Java program
By Java Tip in forum java.ioReplies: 0Last Post: 04-04-2008, 02:40 PM -
broadcast messaging
By bhanu in forum NetworkingReplies: 1Last Post: 10-30-2007, 11:41 AM -
How to execute an External Program through Java program
By JavaBean in forum Java TipReplies: 0Last Post: 10-04-2007, 09:33 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks