swing and sockets question

General software, Operating Systems, and Programming discussion.
Everything from software questions, OSes, simple HTML to scripting languages, Perl, PHP, Python, MySQL, VB, C++ etc.
Post Reply
User avatar
porkchop
Posts: 2345
Joined: Sat Jun 22, 2002 2:05 pm
Location: Toronto, Ontario

swing and sockets question

Post by porkchop »

currently, i'm using swing to build a multithreadedserver and client. the client works fine (can send/receive messages okay). upon connecting to the server, it creates BufferedReader and PrintWriter objects to communicate with the server. To send messages to the server, I'm using a JTextField which sends a message using PrintWriter when i hit enter
however, i dont know how to send messages from the server. it's a multithreaded socket server. it uses an infinite loop to wati for connections. upon connection, it assigns a new thread to handle the connection and goes back to listening.

it can read messages from the client fine. however, it cannot send. to listen for keystrokes from the JTextField, i need to use a KeyListener like so

type.addKeyListener (new KeyListener ()
{

public void keyTyped (KeyEvent evt)
{
System.out.println ("lol1");
int code = evt.getKeyCode ();
if (code == KeyEvent.VK_ENTER)
{

out.println (type.getText ());
out.flush ();
}
}
public void keyPressed (KeyEvent evt)
{
}
public void keyReleased (KeyEvent evt)
{
}

}
);

where out is the PrintWriter object and type is the JTextField object. however, i dont know where to palce this block of code. if I put it before the infinite loop, PrintWriter out hasn't been declared yet. if i put it after, it's unreachable. what can i do?

here's my MultiServerV3 class

http://happysite.no-ip.com/multiserverv3.java
User avatar
A_old
Posts: 10663
Joined: Sun Jan 30, 2000 12:00 am
Location: Atlanta

Post by A_old »

So I guess I'm confused as to why the client handler on the server has a key listener? The ideal would be to make the server just that, a server. It does nothing but relay information from client to client. This also means you should figure out some sort of basic protocol (strings in your case) like ##FROM:NAME:TO:NAME##MSGHERE or something.

The simple way to do it would be to have your inner class "Threaded" actually consist of two seperate threads, the one implementing runnable, as you've done, which listens for incoming data on the socket and consumes it and another thread, which would be done in some simpler manner to actually do the sending on the socket.

The reason you need two threads per client is simple, the call to read from a socket (really the wrapper on the input stream of the socket is what I'm referring to) is a blocking call. That means the .readLine() call will block until data is received on that socket.

The easiest way to do this would be to have the constructor of each Threaded object start the 2ndary thread in a manner such as this:

Thread t = new Thread (new Runnable() {
public void run() {
DoSomeWorkHere();
}
}
);
t.start();


It's important to note that the 2ndary thread started in this manner has limited access to members of the class. This is called the "adapter" technique to starting a thread.

I hope I've helped a little. I know I didn't answer your question directly, but that's because I think there is some rewriting to do. I suppose if you really wanted the server to have a GUI so a user could use it to communicate with others as well (sort of a server/client mix), you could just write a client into it and have it connect to localhost, so it was treated as a client connected to itself (that's the easiest way, imho, so you don't have to handle special cases). If you want a simple example of all of this (no source), written in java, just head to my website via the link in my signature and click on amchat. Feel free to PM me or post here, I'll try to check back.

EDIT: Let me just answer the question, use the adapter method above for the data receive on the Threaded (rather than placing it in the run() method, which should now do something else) and add your keylisteners to the Threaded object, this way the data receive is on its own thread and doesn't block the key input when it's received. Basically, the run method currently blocks the thread that the Threaded object uses, so you want to spin it off onto its own so that doesn't happen, which will allow key presses to be monitored.
Post Reply