Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to use connect() method of Socket Class  RSS feed

 
Vishnu Vardhan
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying connect to a socket, through a web-based program (running on Tomcat & using Struts). Currently I am connecting to a socket using the follwing code :

InetAddress addr = InetAddress.getByName("192.168.9.165");
Socket skt = new Socket(addr,7789);
PrintStream output;
try {
output = new PrintStream(skt.getOutputStream());
output.print("some command to the socket");
}
catch (IOException e) {
System.out.println(e);
}

System.out.println("Socket Opened");
BufferedReader in = new BufferedReader(new
InputStreamReader(skt.getInputStream()));

I feel I am getting the responses very late. I have 2 questions

(a) Is there anyway for me to improve the response speed.
(b) Can I solve this problem by using the connect method in the Socket class (if yes, could you please give me a sample code for the same)

Thanks
-Vishnu
 
Harald Kirsch
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am surprised you get a response at all. After printing a command towards the server, I would at least perform a flush() to make sure things get really out of the door.

Depending on the server, a socket.shutdownOutput() (don't use close()) may get things going. This, however, is of course only an option if there is only one command to send.
 
Vishnu Vardhan
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Herald.

I will do a flush. But how do I implement socket.shutdownOutput(). I will want to use the socket over and again.

Infact I am trying to create a socket pool.

I have one more question regarding this :

I am creating a pool in a different class (pool is an object of a third class). How can I access this object from other classes.

Thanks
Vishnu
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!