• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

socket connection

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
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 connection.so that i can able to send and receive data without opening the connection everytime
Kindly post me regarding this query
Regards
sangeeth
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
can u send any details of the application you are planning to use, so that i can give you some precise suggestion.

------------------
glkishore,
 
pp sangeeth
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hai Lakshmi Kishore ,
thanks for your reply ...
Please see the following for the client side code
Client
*********
//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
theReader.close();
theWriter.close();
soc.close();
***** end of the part of the code *****
now i want to split the above stpes into separate methods
for example,
open
close
send
receive
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,
Sangeeth
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
test
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
thanks,
GP
 
pp sangeeth
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ,
Sangeeth
 
Girish Pednekar
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
thanks,
GP
[This message has been edited by Girish Pednekar (edited August 13, 2001).]
 
When evil is afoot and you don't have any arms you gotta be hip and do the legwork, but always kick some ... tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic