编程语言
首页 > 编程语言> > javaEE(网络编程、TCP、线程池优化)

javaEE(网络编程、TCP、线程池优化)

作者:互联网

网络编程

1.客户端--服务端

安装客户端

2.浏览器和服务端

网络通信:

三要素

IPv4:

IPv6:

域名

Ip命令:

特殊id:

Ip地址操作类

InetAddress

main(){
    //1.获得本机ip对象
     InetAddress  ip =  InetAddress .getLocalHost();
    //得到域名
        InetAddress  ip =  InetAddress .getByName("www.baidu.com");
    //公网的ip
           InetAddress  ip =  InetAddress .getByName("112.82.248.76");
         

端口

标识在计算机上运行的程序,规定的是一个16的二进制,0-65535.

端口类型:

协议

连接和通讯数据的规则--------网络通讯协议

TCP:

传输层的协议:

TCP:

TCP协议的场景:

TCP的三次握手

TCP的四次挥手

UDP:

适合语音通话、视频会话

UDP

数据包:

构造器:

DatagramSocket发送端和接收端对象

构造器:

方法:

main(){
     DatagramSocket sock =new  DatagramSocket();
     //数据包
    byte[] buffer ="我是韭菜".getBytes();
	DatagramPacket packet =new DatagramPacket(buffer,buffer.length,InetAddress.getLocalHost(),8888);
    sock.send(packet);
    sock.close();
}
main(){
     DatagramSocket sock =new  DatagramSocket(8888);
     //数据包
    byte[] buffer =new byte[1024*64];
	DatagramPacket packet =new DatagramPacket(buffer,buffer.length);
    sock.receive(packet);
    String s =new String(buffer);
    socket.close();
}

多发多收

以后吧累了

TCP

面向连接,安全,可靠

java.net.Socket

Socket:

方法:

main(){
    try{
           //创建Socket管道建立连接
    Socket socket =new Socket("127.0..0..1",7777);
    //得到字节输出流
    getOutputStream is =socket.getOutputStream();
    //变成高级流
    PrintStream ps =new  PrintStream(is);
    //发送消息
    ps.print("约么");
    ps.flush(); 
        //socket.close();不建议关闭流
    }catch(Exception e){
        e.printStackTrace();
    }
}

ServerSocket(服务端)

main(){
        try{
           //创建ServerSocket管道建立连接
    ServerSocket ss =new ServerSocket(7777);
          Socket socket =ss.accept();    
    //得到字节输出流
    getInputStream is =socket.getInputStream();
    //变成高级流
    BufferedReader br =new  BufferedReader(new InputStreamReader(is));
    //收消息
            while(ms=br.readLine()!=null){
                System.out.print(socket.RemoteSocketAddress()+"说了"+ms)
            }
        //socket.close();不建议关闭流
    }catch(Exception e){
        e.printStackTrace();
    }
}

多发多收

main(){
    try{
           //创建Socket管道建立连接
    Socket socket =new Socket("127.0..0..1",7777);
    //得到字节输出流
    getOutputStream is =socket.getOutputStream();
    //变成高级流
    PrintStream ps =new  PrintStream(is);
        Scanner sc =new Scanner(System.in);
        
    //发送消息
        while(true){
            System.out.print("请说");
                String ms = sc.nextLine();
            ps.println(ms);
            ps.flush();
        }
        //socket.close();不建议关闭流
    }catch(Exception e){
        e.printStackTrace();
    }
}

ServerSocket(服务端)

main(){
        try{
           //创建ServerSocket管道建立连接
    ServerSocket ss =new ServerSocket(7777);
          Socket socket =ss.accept();    
    //得到字节输出流
    getInputStream is =socket.getInputStream();
    //变成高级流
    BufferedReader br =new  BufferedReader(new InputStreamReader(is));
    //收消息
            while(ms=br.readLine()!=null){
                System.out.print(socket.RemoteSocketAddress()+"说了"+ms);
            }
        //socket.close();不建议关闭流
    }catch(Exception e){
        e.printStackTrace();
    }
}

但是服务daunt不可以接受多个客户端信息。

多客户端

main(){
    try{
           //创建Socket管道建立连接
    Socket socket =new Socket("127.0..0..1",7777);
    //得到字节输出流
    getOutputStream is =socket.getOutputStream();
    //变成高级流
    PrintStream ps =new  PrintStream(is);
        Scanner sc =new Scanner(System.in);
        
    //发送消息
        while(true){
            System.out.print("请说");
                String ms = sc.nextLine();
            ps.println(ms);
            ps.flush();
        }
        //socket.close();不建议关闭流
    }catch(Exception e){
        e.printStackTrace();
    }
}

ServerSocket(服务端)

main(){
        try{
           //创建ServerSocket管道建立连接
    ServerSocket ss =new ServerSocket(7777);
    //收消息
            while(ms=br.readLine()!=null){
                 Socket socket =ss.accept();    
				new SerberThread(socket).start();
            }
        //socket.close();不建议关闭流
    }catch(Exception e){
        e.printStackTrace();
    }
}

线程:

public class ServerThread extends Thread{
private Socket socket;
public ServerThread(Socket socket){
    this.socket-socket;
}
    @Override
    public void run(){
        try{
             //得到字节输出流
    getInputStream is =socket.getInputStream();
    //变成高级流
    BufferedReader br =new  BufferedReader(new InputStreamReader(is));
    //收消息
            while(ms=br.readLine()!=null){
                System.out.print(socket.RemoteSocketAddress()+"说了"+ms);
            }
        //socket.close();不建议关闭流
        }catch{
            e.printStrackTrace();
        }
    }
}

线程池优化

main(){
    try{
           //创建Socket管道建立连接
    Socket socket =new Socket("127.0..0..1",7777);
    //得到字节输出流
    getOutputStream is =socket.getOutputStream();
    //变成高级流
    PrintStream ps =new  PrintStream(is);
        Scanner sc =new Scanner(System.in);
        
    //发送消息
        while(true){
            System.out.print("请说");
                String ms = sc.nextLine();
            ps.println(ms);
            ps.flush();
        }
        //socket.close();不建议关闭流
    }catch(Exception e){
        e.printStackTrace();
    }
}

ServerSocket(服务端)

//定义线程池
private Static ExecutorService pool =new ThreadPoolExecutor(3,5,6,TimeUnit.SECONDS,new ArrayBlockingQueue(2,Executor.defaultThreadFactorty(),new ThreadPoolExcutor.AbortPolicy()));
main(){
        try{
           //创建ServerSocket管道建立连接
    ServerSocket ss =new ServerSocket(7777);
    //收消息
            while(ms=br.readLine()!=null){
                 Socket socket =ss.accept();    
				Runner a =new SerberThread(socket);
                pool.excute(a);
            }
        //socket.close();不建议关闭流
    }catch(Exception e){
        e.printStackTrace();
    }
}

线程:

public class ServerThread implement Runnable{
private Socket socket;
public ServerThread(Socket socket){
    this.socket-socket;
}
    @Override
    public void run(){
        try{
             //得到字节输出流
    getInputStream is =socket.getInputStream();
    //变成高级流
    BufferedReader br =new  BufferedReader(new InputStreamReader(is));
    //收消息
            while(ms=br.readLine()!=null){
                System.out.print(socket.RemoteSocketAddress()+"说了"+ms);
            }
        //socket.close();不建议关闭流
        }catch{
            e.printStrackTrace();
        }
    }
}

优点:

即时通讯

main(){
    try{
           //创建Socket管道建立连接
    Socket socket =new Socket("127.0..0..1",7777);
        //创建一个读线程
        new ClienThread(socket).start();
    //得到字节输出流
    getOutputStream is =socket.getOutputStream();
    //变成高级流
    PrintStream ps =new  PrintStream(is);
        Scanner sc =new Scanner(System.in);
        
    //发送消息
        while(true){
            System.out.print("请说");
                String ms = sc.nextLine();
            ps.println(ms);
            ps.flush();
        }
        //socket.close();不建议关闭流
    }catch(Exception e){
        e.printStackTrace();
    }
}

ServerSocket(服务端)

//定义线程池
private Static ExecutorService pool =new ThreadPoolExecutor(3,5,6,TimeUnit.SECONDS,new ArrayBlockingQueue(2,Executor.defaultThreadFactorty(),new ThreadPoolExcutor.AbortPolicy()));
//客户端的留言
public static List<Socket> allSocket = new ArrayList<>(); 
main(){
        try{
           //创建ServerSocket管道建立连接
    ServerSocket ss =new ServerSocket(7777);
    //收消息
            while(ms=br.readLine()!=null){
                 Socket socket =ss.accept();    
                allStock.add(socket);
				Runner a =new SerberThread(socket);
                pool.excute(a);
            }
        //socket.close();不建议关闭流
    }catch(Exception e){
        e.printStackTrace();
    }
}

线程:

public class ServerThread implement Runnable{
private Socket socket;
public ServerThread(Socket socket){
    this.socket-socket;
}
    @Override
    public void run(){
        try{
             //得到字节输出流
    getInputStream is =socket.getInputStream();
    //变成高级流
    BufferedReader br =new  BufferedReader(new InputStreamReader(is));
    //收消息
            while(ms=br.readLine()!=null){
                System.out.print(socket.RemoteSocketAddress()+"说了"+ms);
                sendMssageToAll(line);
            }
        //socket.close();不建议关闭流
        }catch{
            e.printStrackTrace();
        }
    }
    private void sendMssageToAll(String msg){
        for(Socket socket:ServlerThread.allSocket){
            PrintStream ps =new  PrintStream(socket.getOutputStream());
            ps.println(msg);
            ps.flush();
        }
    }
}

标签:Socket,javaEE,TCP,ServerSocket,线程,ms,new,public,socket
来源: https://www.cnblogs.com/HJZ114152/p/16406716.html