java.net.ServerSocket doesn't catch every connection

M

mmaenz

Hi,

My problem is that ServerSocket.accept doesn't catch every connection
made to the socket.
I wrote a little server that gets a connection and spawns a new thread
giving the ServerSocket as parameter to the constructor. Then the
Thread is start()'ed.

The Code is:

ServerSocket serversock = new ServerSocket(port);
while (true) {
Socket socket = serversock.accept();
HandleSocket hs = new HandleSocket(socket);
hs.start();
}

But this code does not catch every connection that will be made to it
and I get a timeout with
the client application. If I retry connection it often works, but the
problem happens randomly.
I can't find anything, that could cause this problem. I'm really a bit
disappointed. I wrote the same application with C++ and had no
problems. But I need it platform independent, thats why I want to use
Java.

Anyone had this problem before? Can anyone help me?

Thanks in advance
- Michael
 
H

hiwa

(e-mail address removed) ã®ãƒ¡ãƒƒã‚»ãƒ¼ã‚¸:
Hi,

My problem is that ServerSocket.accept doesn't catch every connection
made to the socket.
I wrote a little server that gets a connection and spawns a new thread
giving the ServerSocket as parameter to the constructor. Then the
Thread is start()'ed.

The Code is:

ServerSocket serversock = new ServerSocket(port);
while (true) {
Socket socket = serversock.accept();
HandleSocket hs = new HandleSocket(socket);
hs.start();
}

But this code does not catch every connection that will be made to it
and I get a timeout with
the client application. If I retry connection it often works, but the
problem happens randomly.
I can't find anything, that could cause this problem. I'm really a bit
disappointed. I wrote the same application with C++ and had no
problems. But I need it platform independent, thats why I want to use
Java.

Anyone had this problem before? Can anyone help me?

Thanks in advance
- Michael
Your client code is simply wrong.
 
M

mmaenz

hiwa said:
Your client code is simply wrong.

Well, I don't think so. The client is in my case a webbrowser. I tried
Internet Explorer, Firefox and Netscape and the problem exists with all
of them.

I put an archive with source of my program on:
http://downloads.ragnaruk.de:81/JProxy.zip

The goal of the program is to act like a proxy. A incoming connection
is redirected as
described in jproxy.conf. The source is pretty simple. I didn't take
care of anything, just quick and dirty.

Any help is welcome!

thx
 
F

Frank van Schie

Hi,

My problem is that ServerSocket.accept doesn't catch every connection
made to the socket.
I wrote a little server that gets a connection and spawns a new thread
giving the ServerSocket as parameter to the constructor. Then the
Thread is start()'ed.

The Code is:

ServerSocket serversock = new ServerSocket(port);
while (true) {
Socket socket = serversock.accept();
HandleSocket hs = new HandleSocket(socket);
hs.start();
}

Mainly some general stuff:
I'm not entirely sure, but wouldn't you want a Thread.sleep(50) or so in
the infinite loop to take care of that 100% CPU load you get with this?
Or does the serversock.accept() do this for you?

In HandleSocket, you create a Thread whose run() method executes
precisely once, with no iteration. Why make it a Thread at all? What if
you get 10000 (or arbitrarily high number) connection attempts per
second with processing times of a second? Stack overflow, that's what.
You also have the overhead associated with it. Better to have a smaller
Thread pool, make HandleSocket implement Runnable (no change in your
code) and hand your HandleSocket objects off to the Threadpool to be
handled. Any backlog that develops can be stored in lists or something.

Oh, and method names start with a small letter, with further
camel-casing. This aids readability, and saves you from getting slammed
in the bean bag with a heavy blunt object by the person who has to
maintain your code. You wouldn't want to get slammed in the bean bag.
But this code does not catch every connection that will be made to it
and I get a timeout with
the client application. If I retry connection it often works, but the
problem happens randomly.

Is this used for a huge amount of connections? Maximum queue size is 50.
If there are already 50 connections waiting to be accept()'ed, any new
connections are automatically refused.
 
R

Red Orchid

(e-mail address removed) wrote or quoted in
Message-ID: said:
ServerSocket serversock = new ServerSocket(port);
while (true) {
Socket socket = serversock.accept();
HandleSocket hs = new HandleSocket(socket);
hs.start();
}


As I know as, the following code is one thread model.

<code>
while (...) {
try {
Socket sock = serverSock.accept();

// do something
}
catch (Exception e) {
}
finally {
if (sock != null) {
try {
sock.close();
}
catch (IOException e) {
}
}
}
}
</code>

With this model,
subsequent connection request should wait her turn in a queue
until previous request is completed.

Your code seems to has the above model.
If so, the thread 'HandleSocket' may be wrong.

If you want multi-thread model,
it will be worth to check 'ServerSocketChannel'.
 
C

Chris Uppal

ServerSocket serversock = new ServerSocket(port);
while (true) {
Socket socket = serversock.accept();
HandleSocket hs = new HandleSocket(socket);
hs.start();
}

Is HandleSocket a subclass of Thread ?

If it isn't then the above won't start a new thread, so connections can be
misses if there's are enough attempts while hs.start() is running.

If it is a subclass of Thread, then it probably shouldn't be. In nearly all
cases it's better to make your class implement Runnable, and then start the new
thread with some variant of:

MyHander handler = new MyHandler();
Thread thread = new Thread(handler);
thread.start();

but making that change probably won't solve your immediate problem.

-- chris
 

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

No members online now.

Forum statistics

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

Latest Threads

Top