File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Sockets and Internet Protocols and the fly likes socket connection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "socket connection" Watch "socket connection" New topic

socket connection

pp sangeeth

Joined: Aug 04, 2001
Posts: 8
i want to open a connection when i log into my application and the connection needs to be persistant till i
log out or until i forced to close the that i can able to send and receive data without opening the connection everytime
Kindly post me regarding this query
Lakshmi Kishore
Ranch Hand

Joined: Aug 04, 2001
Posts: 51
can u send any details of the application you are planning to use, so that i can give you some precise suggestion.


Thanks & Regards<br />glkishore
pp sangeeth

Joined: Aug 04, 2001
Posts: 8
Hai Lakshmi Kishore ,
thanks for your reply ...
Please see the following for the client side code
//open the connection
InetAddress hostAddr = InetAddress.getByName(IPADDRESS);
int hostPort = PORT;
Socket soc = new Socket(hostAddr,hostPort);
//Send value to server
PrintWriter theWriter = new PrintWriter(soc.getOutputStream(),true);
theWriter.println("Hi from client ");
//receive the data from the server
String input = "NoValue";
BufferedReader theReader = new BufferedReader(new InputStreamReader(soc.getInputStream()));
while((input = theReader.readLine()) != null)
System.out.println("SERVER REPLIED : " + input + "\n");
//close the socket ,I/O stream
***** end of the part of the code *****
now i want to split the above stpes into separate methods
for example,
so from another class ie at the start point- may be login time) of the applicaion i can call open method to establish the connection and i can call send and receive method for many time s to send and receive the data and in the end point(may be loged out ) of the application i can call close method to close the socket.
Hope now you can have an idea....
i want to save the time for open and close the socket for each and ever request and response.
Thanks & Regards,

Joined: Jun 21, 2001
Posts: 1
Girish Pednekar

Joined: Aug 10, 2001
Posts: 14
hi sangeeth, I have used something similar to what u r asking, in our application. ours is a JSP application. I am storing the reference of the socket in the session object. next time you get a request, get the associated session and from the session get the socketid. it works but you have to be careful about the session timeouts. when the session times out, automatically new session is created but all the old variables (including socketid) stored in the old session are lost. you have to create a new socket but you are left with an old open socket without any reference to it. you need to provide some mechanism to close such sockets.
I'm planning to use a pool of socket connections which is a safer and more efficient way of dealing with sockets.
pp sangeeth

Joined: Aug 04, 2001
Posts: 8
Hi GP,
In our application the server side coding are in "c" and the client side is in "java".so all the session handling things will be take care by "c".Our "c" team is working on it so that from java i can able to have one time connection and hope the issue will be solved.
Thanks ,
Girish Pednekar

Joined: Aug 10, 2001
Posts: 14
sangeeth, one way to do it would be to define a static singleton class with the getInstance(), open, close, send and receive methods. call getInstance to get a reference to this object. call open() once in the beginning of the appl. call send and receive as many times as needed. call close at the end of the appl.
[This message has been edited by Girish Pednekar (edited August 13, 2001).]
I agree. Here's the link:
subject: socket connection
It's not a secret anymore!