java 복습 - 서버 및 클라이언트 socke

2833 단어 java 복습
package com.atguigu;
import org.junit.Test;
import java.io.*; import java.net.InetAddress; import java.net.ServerSocket; import java.net.Socket;
/**
  • @auther Elding_xrl
  • @create 2020-06-18-15:17

  • 1, 서버 측의 ServerSocket을 만들고 자신의 포트 번호를 표시합니다
  • 2. accept () 호출은 클라이언트로부터의 socket을 받아들인다는 것을 나타낸다
  • 3, 입력 흐름을 가져옵니다
  • 4, 입력 흐름의 데이터를 읽습니다
  • 5, 자원 폐쇄



  • */public class TCPJava {
    @Test
    // 
    public void client(){
    
        OutputStream os = null;
        Socket socket = null;
        try {
            InetAddress inet = InetAddress.getByName("127.0.0.1");
            socket = new Socket(inet, 9988);
    
            os = socket.getOutputStream();
            os.write(" , GG!".getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (os != null){
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (socket != null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
    
        }
    
    
    }
    
    @Test
    // 
    public void server() throws IOException {
        /**
         *  * 1、 ServerSocket, 
         *  * 2、 accept() socket
         *  * 3、 
         *  * 4、 
         *  * 5、 
         */
        ServerSocket serverSocket = null;
        Socket accept = null;
        InputStreamReader isr = null;
        ByteArrayOutputStream bis =null;
        try {
    
            //1、 ServerSocket, 
            serverSocket = new ServerSocket(9988);
            //2、serverSoceket accept 
            accept = serverSocket.accept();
            //3、 
            InputStream os = accept.getInputStream();
    

    //방식1://isr = new InputStreamReader(os);///흐르는 데이터 읽기//char[] buffer = new char[5];///int len;//StringBuffer strB = new StringBuffer();//while ((len = isr.read(buffer)) != -1){ //strB.append(buffer,0,len);//}//System.out.println(strB);
            // :
             bis = new ByteArrayOutputStream();
            byte[] buffer = new byte[5];
    
            int len;
            while ((len = os.read(buffer)) != -1){
                bis.write(buffer,0,len);
            }
            System.out.println(bis.toString());
    
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //4、 
    

    //if (isr != null){ //isr.close();//}//if (accept != null){ //accept.close();//}//if (serverSocket != null){ //serverSocket.close();//} if (bis != null){ bis.close();
            }
            if (isr != null){
    
                isr.close();
            }
            if(accept != null){
                accept.close();
    
            }
            if( serverSocket != null){
                serverSocket.close();
            }
        }
    
    
    
    
    
    
    }
    

    }

    좋은 웹페이지 즐겨찾기