Simple application routines for Java network programming

Source: Internet
Author: User

Run the server first, and then run the client to get the Hello world! information sent to the server.

Server code:

Import java.io.*;
Import java.net.*;
PublicClass helloserver{
PublicStaticvoid Main (String args[])Throws IOException
{
ServerSocket ServerSocket =Null
PrintWriter out =Null
try{
ServerSocket =New ServerSocket (9999);
}
catch (IOException E)
{
System.err.println ("Counld not listen on port:9999");
System.exit (1);
}
Socket Clientsocket =Null
try{
Clientsocket = Serversocket.accept ();
}
catch (IOException e)
{
System.err.println ("Accept failed");
System.exit (1);
}
out = new PrintWriter (Clientsocket.getoutputstream (),true);
Out.println ("Hello world!");
Clientsocket.close ();
Serversocket.close ();
}
}

Client code:

Import java.io.*;
Import java.net.*;
PublicClass helloclient{
PublicStaticvoid Main (String args[])Throws IOException
{
Socket Hellosocket =Null
BufferedReader in =Null
try{
Hellosocket =New Socket ("172.20.223.73", 9999);
in =New BufferedReader (New InputStreamReader (Hellosocket.getinputstream ()));
}
catch (Unknownhostexception e) {
             System.err.println ("Don ' t know about host:localhost!");
            system.exit (1);
       }
        catch (IOException e)
         {
            System.err.println ("couldn ' t get I/O for the Connection");
            system.exit (1);
       }
        System.out.println (In.readline ());
        in.close ();
        hellosocket.close ();
   }
}

Leo Chin Source: http://www.cnblogs.com/hnrainll/This blog post, most of the network collection, reproduced please indicate the source related tags: embedded training, embedded development, embedded learning

Simple application routines for Java network programming

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.