Socket communication handle leakage and read blocking

Source: Internet
Author: User
Tags flush

Client programs
public class Socketclient {
public static void Main (string[] args) throws Exception {
while (true) {
Thread.Sleep (5000);
New Clientthread (). Start ();
}
}
}

Class Clientthread extends Thread {
public void Run () {
Socket socket = NULL;
InputStream in = null;
OutputStream out = null;
try {
Socket = new Socket ("10.1.24.147", 8888);
in = Socket.getinputstream ();
out = Socket.getoutputstream ();
SYSTEM.OUT.PRINTLN ("Established a connection ...");
String text = "B7a30350be4f9f58482576b600239d54?" Open ";
byte[] block = new byte[500];
SYSTEM.OUT.PRINTLN ("Begin to write");
Out.write (Text.getbytes ());
Out.flush ();
Bytearrayoutputstream bOut = new Bytearrayoutputstream ();
SYSTEM.OUT.PRINTLN ("Begin to read");
int num = in.read (block); A
while (num > 0) {
System.out.println ("in While");
Bout.write (block, 0, num);
num = In.read (block);
}
System.out.println ("read finished");
System.out.println (Bout.tostring ());
catch (Exception e) {
E.printstacktrace ();
}finally {
try {
Out.close (); B
catch (Exception e) {
}
try {
In.close (); B
catch (Exception e) {
}
try {
Socket.close (); B
catch (Exception e) {
}
}
}
}

Service-side Programs
public class Socketserver {
Public Socketserver () {
try {
ServerSocket server = null;
try {
Server = new ServerSocket (8888);
System.out.println ("Server starts...port =" +server.getlocalport ());
catch (Exception e) {
System.out.println ("Can not be listen to." + E);
}
while (true) {
New Serverthread (Server.accept ()). Start ();
System.out.println ("server.accept () starts ...");
}
catch (Exception e) {
System.out.println ("Error." + e);
}
}

public static void Main (string[] args) {
New Socketserver ();
}
}

Class Serverthread extends Thread {
Socket socket = NULL;

Public serverthread (socket socket) {
This.socket = socket;
}

public void Run () {
InputStream in = null;
OutputStream out = null;
try {
in = Socket.getinputstream ();
out = Socket.getoutputstream ();
byte[] block = new byte[500];
Bytearrayoutputstream bOut = new Bytearrayoutputstream ();
SYSTEM.OUT.PRINTLN ("Begin to read");
int num = in.read (block);
while (num > 0) {
System.out.println ("in While");
Bout.write (block, 0, num);
System.out.println ("in While,begin to read");
num = In.read (block); H
}
System.out.println ("read finished");
System.out.println (Bout.tostring ());
SYSTEM.OUT.PRINTLN ("Begin to write");
Out.write (Bout.tostring (). GetBytes ());
Out.flush ();
catch (Exception e) {
E.printstacktrace ();
finally {
try {
Out.close (); I
catch (Exception e) {
}
try {
In.close (); I
catch (Exception e) {
}
try {
Socket.close (); I
catch (Exception e) {
}
}
}
}
The observed phenomenon
The server side does not read the data sent by the client, nor does the client read the server-side response.
The client is plugged in a and the service ends up at H.
Server block at H, the server has read once, and fetch data, prepare read the second time, because there is no data in the stream, and read always try to read more data, so plugging in H.
The client is blocked at a because the server has no data to write, so there is no data to read in the stream.

That is, when there is no data in the socket stream, read blocks and waits for a timeout, and waits forever (this is different from the read file) If no timeout is set.

Client-->server
If the server is not connected, the client will only occupy one handle, regardless of how many connections the client initiates.
Java 12827 OMS 6u sock 0,0 168883381 can ' t identify protocol

If the server is connected, the client will use a handle for each connection as long as it is not closed, lsof results are as follows:
Java 11544 OMS 113u IPv4 168861112 TCP test3050:35686->10.1.24.147:8888 (established)
Java 11544 OMS 114u IPv4 168862353 TCP test3050:35687->10.1.24.147:8888 (established)

If the server-side process kills at this point, the client-side lsof results are as follows:
Java 11544 OMS 180u sock 0,0 168845122 can ' t identify protocol
Java 11544 OMS 181u sock 0,0 168845124 can ' t identify protocol
Because the client did not close the socket, causing the connection to leak.

If you remove the comment at point B, the client's handle is released even if you do not remove the comment from the I.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.