Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

State of a new Java socket

Tags:

java

sockets

When I declare in Java

Socket s = new Socket((String)null, 12345);

Does this actually open a socket and use system and network resources, or is that deferred until I attach an input/output buffer? I would like to create a Socket object at the start of my program that is all set up to connect to the server, and just open/close it as necessary, instead of having to pass an address and port around (it seems cleaner), but not if it means the port will be open the entire time.

EDIT
It seems from the answers that this will not work like I wanted. How can I create a closed socket that is all set up with address and only needs to connect?

like image 245
Baruch Avatar asked Mar 29 '26 18:03

Baruch


2 Answers

http://docs.oracle.com/javase/6/docs/api/java/net/Socket.html#Socket(java.net.InetAddress,%20int) <- it depends on the constructor you use. For the constructor you have specified, it connects.

like image 188
Karthik Kumar Viswanathan Avatar answered Apr 01 '26 08:04

Karthik Kumar Viswanathan


According to http://docs.oracle.com/javase/7/docs/api/java/net/Socket.html#Socket(java.lang.String,int) ,the way you are initializing your object it will be connected.

like image 45
William Avatar answered Apr 01 '26 08:04

William



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!