TCP communication Exercise 2

Client: data input from the keyboard until the input data is 886, the end of transmission data
Server: receiving data output in the console
Package com.inetTes01;
 / * 
Client: data from the keyboard input, until the input data is 886, the transmission data end 
* / 


Import the java.io. * ;
 Import the java.net.Socket; 

public  class ClientDemo01 {
     public  static  void main (String [] args) throws IOException {
         // create a client Socket object 
        Socket S = new new Socket ( "192.168.18.6", 10000 );
 //         data from the keyboard input until the input data is 886, the data transmission ends 
        BufferedReader = br new new the BufferedReader ( new new the InputStreamReader (the System.in));
         // package output stream object
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
        String line;
        while ((line = br.readLine()) != null) {
            if("886".equals(line)) {
                break;
            }
//            //获取输出流对象
//            OutputStream os = s.getOutputStream();
//            os.write(line.getBytes());
            bw.write(line);
            bw.newLine();
            bw.flush();
        }
        //释放资源
        s.close();


    }
}



Package com.inetTes01;
 / * 
server: receiving the console output data 
 * / 

Import the java.io. * ;
 Import the java.net.ServerSocket;
 Import the java.net.Socket; 

public  class ServerDemo01 {
     public  static  void main (String [] args) throws IOException {
         // Create the Socket object server 
        the ServerSocket SS = new new the ServerSocket (10000 );
         the while ( to true ) { 

            // listening client connection returns a corresponding Socket object 
            Socket S = ss.accept (); 

            // takes an input stream
//        InputStream is = s.getInputStream();
//        InputStreamReader isr = new InputStreamReader(is);
//        BufferedReader br = new BufferedReader(isr);
            BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));

            String line;
            while ((line = br.readLine())!= null) {
                System.out.println(line);
            }
        }

        //ss.close();

    }
}

 

 
 

Guess you like

Origin www.cnblogs.com/lsswudi/p/11440399.html