Article View: borland.public.jbuilder.java.language
Article #754Re: port availability
From: Alan Shiers
Date: Tue, 09 Nov 2004 17:08
Date: Tue, 09 Nov 2004 17:08
82 lines
5010 bytes
5010 bytes
Hi John,
The first method you explain is the one I am making use of. I am familiar
with how the nio package works, but that is a discussion for another time.
I'm trying to focus on the former method. As you pointed out, one generally
sets up a ServerSocket that listens on a particular port. In my case port
5000. When a client logs on, a new thread is spawned. I get that already.
My server app does exactly that. What I wanted to do next was assign port
5001 as being the port that every client uses to upload/download files. But
as I said earlier, there may be times when more than one client is trying to
upload/download files at the same time. That's when I thought a queue might
be appropriate.
Proceedure:
1) The client sends a request to upload a file.
2) The client's request goes into the queue.
3) The queue, running on it's own thread, would continually check to see if
the port is open or not. If the port is being used, then the client next in
line in the queue just has to wait for the port to open up.
4) Once the port 5001 becomes available again, the next client in the queue
is given the go ahead to proceed with the upload.
I hope this is a little clearer. Does this approach look ok?
Alan
"John McGrath [TeamB]" <ng-b@JPMcGrath.net> wrote in message
news:xn0dphn9w893v7001@newsgroups.borland.com...
> On 11/7/2004 at 9:50:02 AM, Alan Shiers wrote:
>
> > Does it make sense to use a particular port as a shared resource
> > for a particular purpose? In my case, I have my server app where
> > I want to make available a particular port that clients can use to
> > upload/download files. However, the possible problem arises that
> > several people may be trying to upload/download files on this port
> > at the same time. So, I'm thinking of implementing a queue to test
> > for the availability of the port and process each clients request
> > in turn.
>
> I see the problem here. That is not the normal way that multi-threaded
> TCP/IP server applications works. The normal way for these to work is for
> the server to create a ServerSocket, and listen for connections using
> ServerSocket.accept(). When a client connects, accept() returns a regular
> (client) Socket, which the server uses to communicate with the client.
>
> If the processing for a client is more or less instantaneous, such as with
> something like a time server that just writes out the current time and
> closes the connection, then you can do that in the main thread. But if
> the processing might take some time, you start a separate thread to
> process the client's request, and return to listening on the ServerSocket.
> Here is the basic structure of a typical server (missing details such as
> exception handling):
>
> serverSocket = new ServerSocket( WELL_KNOWN_PORT );
> while ( ! shuttingDown ) {
> Socket clientSocket = serverSocket.accept();
> Thread thread = new ClientThread( clientSocket );
> thread.start();
> }
>
> The well-known port that you use for the server socket is just used to
> establish the connection. The client socket will use a different port,
> which will be automatically allocated by the system. You do not have to
> worry about that detail.
>
> Note that with the java.nio package introduced in Java 1.4, there are ways
> of serving multiple clients in the same thread, so you would not need to
> create a separate thread for each client. If you need to handle many
> simultaneous clients, you might want to look into that. It is much more
> complex than the above approach, but it can also result in significant
> performance improvements.
>
> --
> Regards,
>
> John McGrath [TeamB]
>
> ---------------------------------------------------
> Before sending me e-mail, please read:
> http://www.JPMcGrath.net/newsgroups/e-mail.html
Technical Details
Message-ID:
<4190fa0b$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!news.glorb.com!feeder.enertel.nl!nntpfeed-01.ops.asmr-01.energis-idc.net!216.196.110.149.MISMATCH!border2.nntp.ams.giganews.com!nntp.giganews.com!news-hub.cableinet.net!blueyonder!news-peer-test!btnet!newsfeed.bt.es!caladan.landsraad.net!newsgroups.borland.com!not-for-mailReferences:
<418d3dc6@newsgroups.borland.com> <xn0dpgfdz8cuc30009@newsgroups.borland.com> <418e369d$1@newsgroups.borland.com> <xn0dphn9w893v7001@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Thread: f74b3,deac63f97d065228 X-Google-Attributes: gidf74b3,public X-Google-Language: ENGLISH,ASCII-7-bit From: "Alan Shiers" <ashiers@hfx.eastlink.ca> Newsgroups: borland.public.jbuilder.java.language References: <418d3dc6@newsgroups.borland.com> <xn0dpgfdz8cuc30009@newsgroups.borland.com> <418e369d$1@newsgroups.borland.com> <xn0dphn9w893v7001@newsgroups.borland.com> Subject: Re: port availability Date: Tue, 9 Nov 2004 13:08:14 -0400 Lines: 84 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1437 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1441 NNTP-Posting-Host: 24.222.101.2 Message-ID: <4190fa0b$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1100020235 24.222.101.2 (9 Nov 2004 09:10:35 -0700) Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!news.glorb.com!feeder.enertel.nl!nntpfeed-01.ops.asmr-01.energis-idc.net!216.196.110.149.MISMATCH!border2.nntp.ams.giganews.com!nntp.giganews.com!news-hub.cableinet.net!blueyonder!news-peer-test!btnet!newsfeed.bt.es!caladan.landsraad.net!newsgroups.borland.com!not-for-mail