3. socket-Connection Anomaly Test

Source: Internet
Author: User

ImportJava.io.BufferedReader;Importjava.io.IOException;ImportJava.io.InputStreamReader;Importjava.net.BindException;Importjava.net.ConnectException;Importjava.net.InetSocketAddress;ImportJava.net.Socket;Importjava.net.SocketAddress;Importjava.net.SocketTimeoutException;Importjava.net.UnknownHostException;/*** @name Socket Connection Exception Test *@authorWujianxiong * @date 2016-5-20*/ Public classConnecttester { Public Static voidMain (string[] args) {BufferedReader reader=NewBufferedReader (NewInputStreamReader (system.in)); String Host= "127.0.0.1"; intPort = 139; Try{String msg=Reader.readline (); //Direct Enter to end loop             while(!msg.isempty () && msg!=NULL) {string[] Strarr= Msg.split (""); Host= Strarr[0]; Port= Integer.parseint (strarr[1]); //Test ConnectionConnecttester.connect (host, Port); Msg=Reader.readline (); }        } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }            }         Public Static voidConnect (String Host,intPort) {socketaddress remoteaddress=Newinetsocketaddress (host, Port); Socket Socket=NewSocket (); Try {            LongBeginTime =System.currenttimemillis (); Socket.bind (NewInetsocketaddress ("10.0.5.128", 80)); Socket.connect (remoteaddress,1000); LongEndTime =System.currenttimemillis (); System.out.println (remoteaddress+ "Take" + (Endtime-begintime) + "Ms to connect"); } Catch(bindexception e) {System.out.println ("Local address and Port can ' t be binded"); } Catch(unknownhostexception e) {//Todo:handle ExceptionSystem.out.println ("UnKnown Host"); } Catch(connectexception e) {System.out.println ("Connect refused"); } Catch(sockettimeoutexception e) {System.out.println ("Time Out"); } Catch(IOException e) {System.out.println ("Failed"); } finally{            Try {                if(socket!=NULL) Socket.close (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }        }            }}

3. socket-Connection Anomaly Test

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.