Java Socke Programmatic reading information blocking solution

Source: Internet
Author: User

1. The socket networking tool class will cause data blocking on the Read server

public class Connecttoserver {private static Boolean d=true;private static InputStream minputstream;private static Output Stream moutputstream;public Connecttoserver () {//TODO auto-generated Constructor stub}/** * Establish TCP connection * @param IP Server IP * @ param Port Port */public static Socket Conn (String Ip,int port) {try {socketsocket=new socket (inetaddress.getbyname (IP), por T), if (D) {log.i ("Socket", (socket==null) + "");} return socket;} catch (Exception e) {//Todo:handle Exception}return null;} /** * Send data msg to server * @param socket * @param msg to send data */public static void Sendreq (Socket socket,byte[] msg) {try {MOUTP Utstream=socket.getoutputstream (); Moutputstream.write (msg); Moutputstream.flush ();} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();}} /** * Data returned from the server * @param socket * @return */public static byte[] Recdata (socket socket) {int count = 0;try {minputstream=s Ocket.getinputstream (); byte[] Indatas = null;while (count = = 0) {count = Minputstream.available ();} Indatas =New Byte[count];minputstream.read (Indatas); return indatas;} catch (IOException E1) {//TODO auto-generated catch Blocke1.printstacktrace ();} return null;} /** * Close Connection freeing resource * @param socket */public static void Closeconn (socket socket) {if (moutputstream!=null) {try {MOUTPUTSTREAM.C Lose (); moutputstream=null;} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();}} if (minputstream!=null) {try {minputstream.close (); minputstream=null;} catch (IOException e) {//TODO auto-generated Catch Blocke.printstacktrace ();}} if (socket!=null) {try {socket.close (); socket=null;} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();}}}


2, when reading the server data, if the data is not read after a period of time, then close the connection to release resources. (This tool class takes 5 seconds as an example)

public class Connecttoserver {private static Boolean d=true;private static InputStream minputstream;private static Output Stream moutputstream;public Connecttoserver () {//TODO auto-generated Constructor stub}/** * Establish TCP connection * @param IP Server IP * @ param Port Port */public static Socket Conn (String Ip,int port) {try {socketsocket=new socket (inetaddress.getbyname (IP), por t); return socket;} catch (Exception e) {log.i ("exception Information", e.tostring ());} return null;} /** * Send data msg to server * @param socket * @param msg to send data */public static void Sendreq (Socket socket,byte[] msg) {try {MOUTP Utstream=socket.getoutputstream (); Moutputstream.write (msg); Moutputstream.flush ();} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();}} /** * Data returned from the server * @param socket * @return */public static byte[] Recdata (socket socket) {int count = 0;try {minputstream=s Ocket.getinputstream (); byte[] Indatas = null;while (count = = 0) {count = Minputstream.available (); try {// No response after 5 seconds (thread.sleep); if (Minputstream.avAilable () ==0) {//Close Connection Release resource closeconn (socket); catch (Exception e) {//Todo:handle exception}if (D) {log.i ("read service-side data", minputstream.available () + "");}} if (count==0) {return null;} Else{indatas = new Byte[count];minputstream.read (indatas); return indatas;}} catch (IOException E1) {//TODO auto-generated catch Blocke1.printstacktrace ();} return null;} /** * Close Connection freeing resource * @param socket */public static void Closeconn (socket socket) {if (moutputstream!=null) {try {MOUTPUTSTREAM.C Lose (); moutputstream=null;} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();}} if (minputstream!=null) {try {minputstream.close (); minputstream=null;} catch (IOException e) {//TODO auto-generated Catch Blocke.printstacktrace ();}} if (socket!=null) {try {socket.close (); socket=null;} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();}}}


Java Socke Programmatic reading information blocking solution

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.