Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #33258

Re: multithread client socket

Newsgroups comp.lang.java.programmer
Date 2015-12-01 11:58 -0800
References <75df5111-41b9-4833-8497-69104f011d14@googlegroups.com>
Message-ID <e8a525f0-557c-4331-b69f-211aa3a47ceb@googlegroups.com> (permalink)
Subject Re: multithread client socket
From Eric Douglas <e.d.programmer@gmail.com>

Show all headers | View raw


On Tuesday, December 1, 2015 at 12:37:00 PM UTC-5, Philipp Kraus wrote:
> Hello,
> 
> I'm using a cached thread pool which gets a lot of callable objects. Each callable create with "new Socket" a client socket to a server, sends data to the server, receive the response and returns the response with a future object. The server closes the socket after the response is send.
> 
> On large datasets the runtime cannot create new socket objects (socket already in use message). The Java client runs explicit socket.close without any error. My idea is, that each thread of the thread-pool holds a member with the socket, but I need must reconnect the client socket on each task, because the server closes the socket.
> 
> My question is, how can I use a lot of client sockets which can be closed by the server, but without re instantiation on Java side. On a socket.connect I get "socket is closed" after the server has closed it.
> 
> Thanks
> 
> Phil

Do you have some sample code?

How are you getting to the socket in use error?

I wrote a socket server class which connects to any number of clients.  In my testing the clients got 3 types of errors on creating the Socket object: ConnectException if the server was not running, UnknownHostException if they pass an invalid host name, or IOException for a general connection issue.

For each client-server session my client creates a new Socket instance and my server starts a new Socket (passed to it from ServerSocket.accept()) on a new Thread.  My server sits on an endless loop of starting new Threads every time it gets a value from that accept() method.  Whenever the client or server closes the socket, the other receives an IOException if it attempts to access the socket again which tells it to also close the socket.  When my server closes the socket it ends the thread, so if that client reconnects it gets a new socket with a new thread.  I haven't had to worry about limits to number of open sockets or active threads, so if you find a limit let us know, and you'll have to set up a queue as Martin mentioned.

Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

multithread client socket Philipp Kraus <philipp.kraus.flashpixx@gmail.com> - 2015-12-01 09:36 -0800
  Re: multithread client socket Eric Douglas <e.d.programmer@gmail.com> - 2015-12-01 11:58 -0800
    Re: multithread client socket Philipp Kraus <philipp.kraus.flashpixx@gmail.com> - 2015-12-01 23:02 -0800
      Re: multithread client socket Eric Douglas <e.d.programmer@gmail.com> - 2015-12-02 06:39 -0800
  Re: multithread client socket Jan Burse <janburse@fastmail.fm> - 2015-12-01 22:01 +0100
    Re: multithread client socket Jan Burse <janburse@fastmail.fm> - 2015-12-01 22:06 +0100
      Re: multithread client socket Eric Douglas <e.d.programmer@gmail.com> - 2015-12-01 13:55 -0800
    Re: multithread client socket Martin Gregorie <martin@address-in-sig.invalid> - 2015-12-01 22:04 +0000
      Re: multithread client socket Philipp Kraus <philipp.kraus.flashpixx@gmail.com> - 2015-12-01 23:04 -0800
        Re: multithread client socket Eric Douglas <e.d.programmer@gmail.com> - 2015-12-02 05:52 -0800
        Re: multithread client socket Martin Gregorie <martin@address-in-sig.invalid> - 2015-12-02 14:38 +0000
  Re: multithread client socket Arne Vajhøj <arne@vajhoej.dk> - 2015-12-05 20:00 -0500

csiph-web