Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-28-2008, 07:19 AM
Zosden's Avatar
Senior Member
 
Join Date: Apr 2008
Posts: 218
Zosden is on a distinguished road
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:

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()); } } } }
Client class:

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(); } } }
Server Class:

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(); } } }
__________________
Definition of Impossible = making a good game in Java.

Last edited by Zosden : 04-28-2008 at 07:25 AM.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 04-28-2008, 08:39 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 1,125
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Zosden, what is your problem. Can you provide more details about it. Did you get any errors?
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
Want to make your IDE the best?Vote Now
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 04-28-2008, 08:44 AM
Zosden's Avatar
Senior Member
 
Join Date: Apr 2008
Posts: 218
Zosden is on a distinguished road
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.
__________________
Definition of Impossible = making a good game in Java.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 04-28-2008, 09:09 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 1,125
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
Want to make your IDE the best?Vote Now
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 04-28-2008, 09:35 AM
Zosden's Avatar
Senior Member
 
Join Date: Apr 2008
Posts: 218
Zosden is on a distinguished road
are you going to post a suggestion or not?
__________________
Definition of Impossible = making a good game in Java.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 04-28-2008, 10:00 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 1,125
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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.

Code:
if (sendText != "");
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.

Do it as follows.

Code:
if (!sendText.isEmpty())
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
Want to make your IDE the best?Vote Now
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 04-29-2008, 05:49 AM
Zosden's Avatar
Senior Member
 
Join Date: Apr 2008
Posts: 218
Zosden is on a distinguished road
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.
__________________
Definition of Impossible = making a good game in Java.
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 04-29-2008, 05:51 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 1,125
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Which while loop you are talking about Zosden?
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
Want to make your IDE the best?Vote Now
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 04-29-2008, 07:16 AM
Zosden's Avatar
Senior Member
 
Join Date: Apr 2008
Posts: 218
Zosden is on a distinguished road
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
__________________
Definition of Impossible = making a good game in Java.
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 05-04-2008, 04:35 AM
killtestcom's Avatar
Member
 
Join Date: Apr 2008
Posts: 1
killtestcom is on a distinguished road
Send a message via Yahoo to killtestcom
KillTest. The safer,easier way to pass any IT exam. And help You to get IT CERT.
__________________
KillTest. The safer,easier way to pass any IT exam. And help You to get IT CERT.
CISCO CCNA CCNP MICROSOFT MCSE MCSE2008 IBM ORACLE EMC NORTEL ORACLE BEA LPI...
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 05-04-2008, 05:34 AM
Zosden's Avatar
Senior Member
 
Join Date: Apr 2008
Posts: 218
Zosden is on a distinguished road
What do you mean killtest
__________________
Definition of Impossible = making a good game in Java.
Bookmark Post in Technorati
Reply With Quote
  #12 (permalink)  
Old 05-04-2008, 08:18 PM
Member
 
Join Date: May 2008
Posts: 2
JavaHead08 is on a distinguished road
Quote:
Originally Posted by Zosden View Post
What do you mean killtest
LOL, he's spamming the forum. Ignore him.
Bookmark Post in Technorati
Reply With Quote
  #13 (permalink)  
Old 05-05-2008, 02:08 AM
Zosden's Avatar
Senior Member
 
Join Date: Apr 2008
Posts: 218
Zosden is on a distinguished road
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
__________________
Definition of Impossible = making a good game in Java.
Bookmark Post in Technorati
Reply With Quote
  #14 (permalink)  
Old 05-05-2008, 08:14 AM
danielstoner's Avatar
Senior Member
 
Join Date: Apr 2008
Location: Canada
Posts: 117
danielstoner is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #15 (permalink)  
Old 05-05-2008, 08:18 AM
Zosden's Avatar
Senior Member
 
Join Date: Apr 2008
Posts: 218
Zosden is on a distinguished road
Lol thanks daniel I was wondering if you could either explain it in either a lot more detail or preferably send me to a guide. Thanks again.
__________________
Definition of Impossible = making a good game in Java.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to execute an External Program through Java program Java Tip java.io 0 04-04-2008 03:40 PM
Executing a program within a program gibsonrocker800 New To Java 4 02-16-2008 10:49 PM
Thread priorities, synchronization and messaging JavaForums Java Blogs 0 12-17-2007 01:21 PM
broadcast messaging bhanu Networking 1 10-30-2007 12:41 PM
How to execute an External Program through Java program JavaBean Java Tips 0 10-04-2007 10:33 PM


All times are GMT +3. The time now is 09:23 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org