Socke, also known as "socket", is used to describe IP addresses and ports. It is a communication chain handle. Applications usually send requests to or respond to network requests through "Sockets.
Compile the socke server code:
Package com. evan. blog; import java. io. IOException; import java. io. inputStream; import java.net. serverSocket; import java.net. socket; public class BlogServer {public static void main (String [] args) {try {ServerSocket serverSocket = new ServerSocket (8080); Socket socket = serverSocket. accept (); InputStream is = socket. getInputStream (); byte [] B = new byte [1024]; int len; StringBuffer sb = new StringBuffer (); wh Ile (len = is. read (B ))! =-1) {String str = new String (B, 0, len); sb. append (str);} is. close (); System. out. println (sb. toString (); System. out. println ("Someone has accessed port 8080 of the Local Machine and passed in '" + sb. toString () + "'value");} catch (IOException e) {// TODO Auto-generated catch blocke. printStackTrace ();}}}
Client source code:
Package com. evan. blog; import java. io. IOException; import java. io. outputStream; import java.net. inetAddress; import java.net. socket; import java.net. unknownHostException; public class BlogClient {public static void main (String [] args) {try {Socket socket = new Socket (InetAddress. getLocalHost (). getHostAddress (), 8080); // obtain the local IP address OutputStream OS = socket. getOutputStream (); String str = "my blog socket"; OS. write (str. getBytes (); OS. flush (); OS. close ();} catch (UnknownHostException e) {// TODO Auto-generated catch blocke. printStackTrace ();} catch (IOException e) {// TODO Auto-generated catch blocke. printStackTrace ();}}}