Socket class freeze

Why does my program freeze up when i run this funtion on a click button event?

public void runServer()

{int counter = 1;

try {

server=new ServerSocket(4000,100);

while(true)

{

Chat.display.setText("waiting for connection\n");

connection=server.accept();

Chat.display.append("Connection "+counter+

"recieved from "+ connection.getInetAddress).getHostName());

output=new ObjectOutputStream(connection.getOutputStream());

output.flush();

input=new ObjectInputStream(connection.getInputStream());

Chat.display.append("\nGot I/O stream\n");

message="server>>connection successfull";

output.writeObject(message);

output.flush();

}

But when i run it from main like this it runs with no freeze

public static void main(String args[])

{

Chat ch = new Chat();

ch.runServer()

}

I Dont want it to run from main. i want to run it when i click a button.

[1011 byte] By [Jino] at [2007-9-27 15:38:58]
# 1

With the button click, you run it in the AWT event thread. Now what that means is that while you're doing server.accept() (which blocks the thread) the AWT event thread can't handle events, redraw components etc.

Make a class extending Thread that has all the code you demonstrated. Then when the button is pressed, you launch your thread. I would also make sure that only 1 thread is created too, unless you for some reason want many.

If you have no idea what I'm talking about, search the forums for the following words. Thread, multithreading.

Kayaman at 2007-7-5 23:41:09 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 2
***************************************************************This question was answered by a friend of the water cooler.Vote for off-topic threads/forums at "Discuss the JDC website" forum
Kayaman at 2007-7-5 23:41:09 > top of Java-index,Archived Forums,New To Java Technology Archive...