Current Position:Home > Understanding problem with threads

Understanding problem with threads

Update:10-11Source: network consolidation
Advertisement
Hi,
yesterday (at least i think it was yesterday) I posted a question in this forum, I needed help with writing a multithreaded server. The answer was simple and I wrote my program. It kinda works, but there is one thing I don't understand:
I have a class, which handles the clientconnections and one that listens for incoming conections. I pass the socket returned by ServerSocket.accept() to the clientconnection object.
There is only ONE clientconnection-object. The listener just passes the socket and calls the start() method of the clientconnection object.
The clientconnection just writes some text to the client. Connecting more than one client at the same time works too. Everything works fine. And that's what I don't get. If I have only O N E clientconnection the texttransfer shouldn't work after i connect the second client, right?
But it does, is there a mistake in my logic or am I losing my sanity? :)

The Best Answer

Advertisement
Hi again Danbas,
Maybe you are losing your sanity... but that's ok. :)
public void startlistening(){
while(true){
try{
clientconn cc = new clientconn(ssock.accept());
}catch(IOException ie){}
//The Constructor in clientconn would look like this:
public clientconn(Socket sock){
//No idea what to do here
Every time you perform "new", you create a new object. And you are calling new every time you accept a new socket connection. So you are creating a new clientconn object each time.
  • Understanding problem with threads Update:10-11

    Hi, yesterday (at least i think it was yesterday) I posted a question in this forum, I needed help with writing a multithreaded server. The answer was simple and I wrote my program. It kinda works, but there is one thing I don't understand: I have a

  • Problem with Thread and InputStream Update:10-11

    Hi, I am having a problem with threads and InputStreams. I have a class which extends Thread. I have created and started four instances of this class. But only one instance finishes its' work. When I check the state of other three threads their state

  • Problem with threads within applet Update:10-11

    Hello, I got an applet, inside this applet I have a singleton, inside this singleton I have a thread. this thread is running in endless loop. he is doing something and go to sleep on and on. the problem is, when I refresh my IE6 browser I see more th

  • Problem with Threads and a static variable Update:10-11

    I have a problem with the code below. I am yet to make sure that I understand the problem. Correct me if I am wrong please. Code functionality: A timer calls SetState every second. It sets the state and sets boolean variable "changed" to true. T

  • Problem with threads running javaw Update:10-11

    Hi, Having a problem with multi thread programming using client server sockets. The program works find when starting the the application in a console using java muti.java , but when using javaw multi.java the program doesnt die and have to kill it in

  • Creation  of components in dinamic way. Problem with threads....? Update:10-11

    hi, I am trying to add components (JTextFields) to a JPanel in dynamic way. I would like that the user decides the number of the components, to be created, for executing a function by clicking a button. But clicking on the button, the elaboration doe

  • Problem w/ thread in Database forum? Update:10-11

    Is there a problem with this thread? Datablock writes Most threads that are answered will say "This thread is answered." and below one of the post will be marked "Correct". In this thread, it says "This question is answered. Helpf