Java MultiThreading / blocking

My code blocks/stops at this line...
  Socket s = ss.accept();

How can I keep that from happening?


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;

import javax.swing.JOptionPane;


public class TCPServerThread implements Runnable {
	
	public void run() {
		//while(true){
		try{
			while(true){
			ServerSocket ss = new ServerSocket (4444);
			//JOptionPane.showMessageDialog(null, "ok");
			Socket s = ss.accept();
				
			InputStreamReader IR = new InputStreamReader(s.getInputStream());
			BufferedReader BR = new  BufferedReader(IR);
			
			String m = BR.readLine();
			JOptionPane.showMessageDialog(null, m);
			
			if (m!=null) {
				PrintStream PS = new PrintStream(s.getOutputStream());
				PS.println("message received");
			}	
			s.close();
			ss.close();
			Thread.sleep(999);
			}
		}
		catch(Exception e){}
		}
	//}
	
}

Open in new window


HERE IS THE CODE THAT STARTS THE THREADING...

JButton button = new JButton("server");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				TCPServerThread t  = new TCPServerThread();
				try {
					t.run();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				}
		});

Open in new window

pderingAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

krakatoaCommented:
Post the exception trace please for us.
0
CEHJCommented:
How can I keep that from happening?
You can't. It's meant to happen. Look at the API docs
0
mccarlIT Business Systems Analyst / Software DeveloperCommented:
As CEHJ said, you can't stop the .accept() call from blocking, that is what it is supposed to do. However, I can see where I think you are going wrong. Even though you have named your class ending with Thread and it is implementing Runnable, it still isn't actually a separate thread yet. Try the following code for your button creation...
JButton button = new JButton("server");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				Thread t  = new Thread(new TCPServerThread());
				try {
					t.start();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				}
		});

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.