【程序员笔记】socket使用笔记

一 socket解释

Socket就是套接字,就是通过ip地址和通信端口组成套接字地址,然后客户端通过这个地址和服务器进行数据交换。

【程序员笔记】socket使用笔记_第1张图片
维基百科解释

socket具体流程

【程序员笔记】socket使用笔记_第2张图片
socket活动流程

二 代码实践

server

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.rmi.RemoteException;

public class Server {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        try { 
            boolean flag=true;
            Socket client=null;
            String inputLine; 
            ServerSocket serverSocket =new ServerSocket (9000);
            System.out.println("server srart to listen port 9000");
        
            while(flag) { 
                client=serverSocket.accept();
                DataInputStream input=new DataInputStream(new BufferedInputStream(client.getInputStream())); 
                PrintStream output=new PrintStream(new BufferedOutputStream(client.getOutputStream())); 

                while (( inputLine= input.readLine())!=null) { 
                    if(inputLine.equals("-1")){ 
                        flag=false; 
                        break; 
                    } 
                    inputLine=checkNum(Integer.parseInt(inputLine));
                output.println(inputLine); 
                output.flush(); 
                } 
            
                output.close(); 
                input.close(); 
                client.close(); 
            } 
            serverSocket.close(); 
        }catch(IOException e){
            
        } 
        
    }

    private static String checkNum(int num) throws RemoteException {
        // TODO Auto-generated method stub
        
        for(int i=2;i<(num/2+1);i++){
            if(isPrimeNum(i)&&isPrimeNum(num-i)){
                return "Number1="+i+" ; Number2="+(num-i);
            }
        }
        return "not fount";
    }
    
    private static Boolean isPrimeNum(int num){
        
        for(int i=2;i<(num/2+1);i++){
            if(num%i==0){
              return false;
            }
        }
        return true;
    }

}

client

import java.io.DataInputStream;
import java.io.OutputStream;
import java.net.Socket;

public class Client {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        try{
                Socket clientSocket =new Socket ("127.0.0.1",9000);
                OutputStream output =clientSocket.getOutputStream();
                DataInputStream input=new DataInputStream(clientSocket.getInputStream());
                
                int c;
                String response;
                while (( c= System.in.read())!=-1){
                    output.write((int)c);
                    if(c=='\n'){
                        output.flush();
                        response=input.readLine();
                        System.out.println(response);               
                    }
                }
                output.close();
                input.close();
                clientSocket.close();
        } catch (Exception e){
                System.err.println("Exception :"+e);
        }
    }
}

在编写过程中注意一下流的问题就可以啦,其他地方比较简单。直接使用已经做好的api就好。

你可能感兴趣的:(【程序员笔记】socket使用笔记)