This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Sockets and Internet Protocols and the fly likes Client Server -- same socket Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Client Server -- same socket" Watch "Client Server -- same socket" New topic
Author

Client Server -- same socket

smitha verghese
Ranch Hand

Joined: Jun 21, 2003
Posts: 42
Hi,
I have implemeted the client server program where the client sends a file to the server.
The server uses a fixed port 1500 to receive the data.But the client responds thro some other PORT!!!
i WANT BOTH THE CLIENT AND THE SERVER COMMUNICATE THRO SAME PORT COULD U HELP ME IN THIS?
Thanks in advance
regards
Smitha
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Well, the Socket class has several four-argument constructors that let you specify the local address and port as well as the remote address and port; just use one of those and then you can specify that the local port should be 1500.
Now, I have to ask you why you want to do this? It's not a very common requirement. A very few protocols (like Berkeley lpr) once used privileged client-side ports as a very weak security mechanism -- but these days, that doesn't make any sense. Using a fixed local port means that only one client on the local machine will be able to connect to the remote service at a time; for some kinds of software, this would be a serious impediment.


[Jess in Action][AskingGoodQuestions]
smitha verghese
Ranch Hand

Joined: Jun 21, 2003
Posts: 42
Well This application I have developed to release it to the customers where the port number that will b purchased by us will b just one.
And so we wanted to use just 1 port for both the client as well as the server.
Since we cant get more than 1 port for purchase!!!
Hope u can get me!
Pls reply if u didnt understand

Smitha
gautham kasinath
Ranch Hand

Joined: Dec 01, 2000
Posts: 583
well,
Although the server binds to one port to listen ( also called passive port ), when a request comes in, another port ( internally handled ) must be assigned to the incomming connection, for processing ( also called the active port ).
After the assignment, the server resumes listening on the passive port.
typically for a multi threaded server, that spans a unique thread for each connection, for processing the code would be

Well, in this case, there is only one listening port ( passive port ) and an active port is ramdomly assigned by the n/w layer, as soon as a connection is accepted.
You can see this by printing the client sockets attributes,or using tcpdump.
Technically, the code fragment above must solve your problem of being restricted to using one port.
To fine tune the performance of the server, because too many threads in a system is a problem, you would have to use something like a queue with a non-blocking socket ( see concept of IOCompletionPort, VC++ ).
Cheers
Lupo


"In the country of the blind, the one eyed man is the King"
Gautham Kasinath CV at : http://www.geocities.com/gkasinath
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Client Server -- same socket