multi threaded server sockets

T

thewhoracle

I have an applet that needs to have a receiving thread and a sending
thread, so my server application needs to also have two threads run
from a common class, and the receiver must be able to access the
sender's output stream. For some reason when i start my threads in the
main function, one won't start until the other dies. I knwo the
solution is simple but i can't seem to make the two threads just start
running one after the other. could someone show me how to organize it
so they will?
 
T

thewhoracle

my code when they wouldn't run one after the other was something like
this:


public static void main(String[] args) {
BeginThreads();
}
public void BeginThreads(){
ComInThread cIn = new ComInThread(2002,this);
cIn.start();

ComOutThread cOut = new ComOutThread(2001,this);
cOut.start();
}


And my ComInThread class, in case that caused the problem is like
this. It gets started first.


public class ComInThread extends Thread{

public ComInThread(){ //constructor

try {
server_inSocket = new ServerSocket(2002);
while(true) {
Socket socket = server_inSocket.accept();
System.out.println("New connection accepted" +
socket.getInetAddress() + ":" +
socket.getPort());
input = new BufferedReader(new InputStreamReader

(socket.getInputStream()));
output = new PrintWriter(socket.getOutputStream());
// print received data
while(true){
String message = input.readLine();
System.out.println(message);
}
}
}catch(Exception exc){System.out.println(exc.toString());}
}
 
P

Pete Barrett

You seem to be doing everything in the constructor. Try putting real
work in the run() method.


Pete Barrett
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top