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