Client
Package com.inetTes01; / * client: sending data, the server receives feedback * / Import java.io.IOException; Import a java.io.InputStream; Import a java.io.OutputStream; Import the java.net.Socket; public class ClientDemo { public static void main (String [] args) throws IOException { // create a client Socket object (Socket) Socket S = new new Socket ( "192.168.18.6", 10000 ); // Get the output stream, the write data the OutputStream OS = S .getOutputStream (); os.write ( "How are you, I'm coming on the server side.".getBytes ()); // receiving feedback server, the data read corresponding to the InputStream IS = s.getInputStream (); byte [] = BYS new new byte [1024 ]; int len = is.read (BYS); the System.out .println ( "client:" + new new String (BYS, 0 , len)); // release resources as is, os s are obtained by not requiring re-release S.CLOSE (); } }
server
Package com.inetTes01; / * receiving data, given feedback * / Import java.io.IOException; Import a java.io.InputStream; Import a java.io.OutputStream; Import the java.net.ServerSocket; Import the java.net.Socket; public class ServerDemo { public static void main (String [] args) throws IOException { // create server-side Socket object (ServerSocket) ServerSocket SS = new new ServerSocket (10000 ); // Get the input stream, the read data // the class ServerSocket accept () listens to connect to this socket and accepts it. S = the Socket ss.accept (); // get the input stream the InputStream IS = s.getInputStream (); // read data byte [] = BYS new new byte [1024 ]; int len = is.read (BYS); the System. Out.println ( "server" + new new String (BYS, 0 , len)); // feedback is given, the write data corresponding to the OutputStream OS = s.getOutputStream (); os.write ( "data has been received" .getBytes ()); // release resources ss.close (); } }