Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2006

Tip: Looking for answers? Try searching our database.

java.net.ServerSocket doesn't catch every connection

Thread view: 
mmaenz@yahoo.de - 24 Jun 2006 19:29 GMT
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
hiwa - 25 Jun 2006 00:36 GMT
mmaenz@yahoo.de :

> Hi,
>
[quoted text clipped - 26 lines]
> Thanks in advance
> - Michael
Your client code is simply wrong.
mmaenz@yahoo.de - 25 Jun 2006 10:42 GMT
> 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
Frank van Schie - 25 Jun 2006 13:02 GMT
> Hi,
>
[quoted text clipped - 12 lines]
>      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.

Signature

Frank

Red Orchid - 25 Jun 2006 13:19 GMT
mmaenz@yahoo.de wrote or quoted in
Message-ID: <1151173791.889853.37340@y41g2000cwy.googlegroups.com>:

> 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'.
Chris Uppal - 25 Jun 2006 15:51 GMT
> 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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.