swing and sockets question
Posted: Tue May 16, 2006 7:25 pm
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
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