InteractiveMind
asked on
ServerSocket#close() without closing Socket?
Hi, I figured that it would be easier to ask here, then test it out myself... :-)
If I were to do the following:
ServerSocket srv = new ServerSocket( port );
Socket sock = srv.accept();
srv.close(); // Or reinitialize srv.
Will that also close the Socket connection??
Ta.
If I were to do the following:
ServerSocket srv = new ServerSocket( port );
Socket sock = srv.accept();
srv.close(); // Or reinitialize srv.
Will that also close the Socket connection??
Ta.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
;-)
I don't think that is correct.
Closing the serverSocket will not close its active connections. (it will stop listening and clear is pending connection).
See this example:
import java.net.*;
import java.io.*;
public class TestServerSocketClose
{
public static void main(String st[]) throws Exception
{
ServerSocket server = new ServerSocket(1929);
new Thread()
{
public void run()
{
try
{
Thread.sleep(1000);
Socket client = new Socket("localhost", 1929);
DataInputStream clientIn = new DataInputStream(client.get InputStrea m());
DataOutputStream clientOut = new DataOutputStream(client.ge tOutputStr eam());
for (int i = 0; i < 10; i++)
{
clientOut.writeChars("hell o world\n");
clientOut.flush();
Thread.sleep(1000);
}
client.close();
}
catch (Exception ex)
{
}
}
}.start();
Socket socket = server.accept();
DataInputStream in = new DataInputStream(socket.get InputStrea m());
DataOutputStream out = new DataOutputStream(socket.ge tOutputStr eam());
String line = null;
boolean wasClosed = false;
while ((line = in.readLine()) != null)
{
System.out.println(line);
if (!wasClosed)
{
System.out.println("Closin g serverSocket....");
server.close();
wasClosed = true;
System.out.println("Server Socket isClosed -> " + server.isClosed());
}
}
}
}
Closing the serverSocket will not close its active connections. (it will stop listening and clear is pending connection).
See this example:
import java.net.*;
import java.io.*;
public class TestServerSocketClose
{
public static void main(String st[]) throws Exception
{
ServerSocket server = new ServerSocket(1929);
new Thread()
{
public void run()
{
try
{
Thread.sleep(1000);
Socket client = new Socket("localhost", 1929);
DataInputStream clientIn = new DataInputStream(client.get
DataOutputStream clientOut = new DataOutputStream(client.ge
for (int i = 0; i < 10; i++)
{
clientOut.writeChars("hell
clientOut.flush();
Thread.sleep(1000);
}
client.close();
}
catch (Exception ex)
{
}
}
}.start();
Socket socket = server.accept();
DataInputStream in = new DataInputStream(socket.get
DataOutputStream out = new DataOutputStream(socket.ge
String line = null;
boolean wasClosed = false;
while ((line = in.readLine()) != null)
{
System.out.println(line);
if (!wasClosed)
{
System.out.println("Closin
server.close();
wasClosed = true;
System.out.println("Server
}
}
}
}
ASKER
Oh. -_^
Well, in that case; thank you! lol. :-)
Please see: https://www.experts-exchange.com/questions/21409873/Cheers-Aozarov.html
Thank you.
Well, in that case; thank you! lol. :-)
Please see: https://www.experts-exchange.com/questions/21409873/Cheers-Aozarov.html
Thank you.
ASKER
Cheers :-)