美文网首页
java 网络编程

java 网络编程

作者: bigpeng个人博客 | 来源:发表于2018-10-17 10:46 被阅读7次

    1、两台计算机要进行通信需要以下三个条件:
      IP地址,协议,端口号  
    2、TCP/IP协议
      tcp/ip:Transmission Control Protocol 传输控制协议。
    IP:Internet Protocol 互联网协议
    协议是目前世界上应用最广泛的协议。TCP/IP协议分为五层:

    • 应用层:http,ftp,smtp,telnet 等
    • 传输层:tcp|udp
    • 网络层: ip
    • 数据链路层:设备驱动程序
    • 物理层:网线,网卡等
      3、端口
      区分一台主机的多个不同应用程序,端口号范围为0-65535,其中0-1023位为系统保留。
      如:HTTP:80 FTP:21 Telnet:23
      IP地址+端口号组成了所谓的Socket,Socket是网络上运行的程序之间双向通信链路的终结点,是TCP和UDP的基础
      4、socket 套接字
      网络上具有唯一标识的IP地址和端口组合在一起才能构成唯一能识别的标识符套接字。
      Socket原理机制:
      通信的两端都有Socket
      网络通信其实就是Socket间的通信
      数据在两个Socket间通过IO传输
      5、socket编程
      java socket编程主要涉及到两个类:
      1) 客户端类:Socket
    1. 服务端类:ServerSocket类
      Socket 通信的步骤
    • 创建ServerSocket和Socket类
    • 打开连接到Socket的输入输出流
    • 按照协议对Socket进行读写
    • 关闭输入输出流,关闭socket
      具体步骤流程图为:


      socket.jpg

    5.1 ServerSocket 编写步骤

    public class Server {
        public static void main(String[] args) {
            ServerSocket server=null;
            PrintWriter writer=null;
            Scanner scannerIn=null;
            Scanner say=null;
            try {
                //1.创建服务端对象指定端口号
                server=new ServerSocket(8888);
                //2.等待客户端连接
                Socket client = server.accept();
                System.out.println("客人来了!"+client.getInetAddress()+"客户已成功连接");
                //3.向客户端发送消息(通过client对象的OutputStream)
    
                //3.1获取client对象的输出流对象
                writer=new PrintWriter(client.getOutputStream());
                //3.2 通过输出流写内容给客户端
                writer.println("你好!我是小娜,有什么可以帮您!");
                writer.flush();
                //4.读取客户端消息
                scannerIn=new Scanner(client.getInputStream());
                //创建键盘监听对象,获取控制台输入内容
                say=new Scanner(System.in);
                while (scannerIn.hasNextLine()) {
                    String str = scannerIn.nextLine();
                    System.out.println("客官:" + str);
                    String sayStr= say.nextLine();
                    writer.println(sayStr);
                    writer.flush();
    
                }
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                FileUtil.close(writer);
                FileUtil.close(scannerIn);
                FileUtil.close(server);
            }
        }
    }
    
    
    public class Client {
    
        public static void main(String[] args) {
            Scanner scannerIn=null;
            PrintWriter writer=null;
            Socket socket=null;
            Scanner say=null;
            try {
    
                System.out.println("等待连接服务器");
                //1.创建客户端连接 指定服务端IP和端口
                socket=new Socket("localhost",8888);
                System.out.println("连接服务器成功");
                //2.读取服务端的信息
                scannerIn=new Scanner(socket.getInputStream());
    
                String str = scannerIn.nextLine();
                System.out.println("小娜:" + str);
    
                //3.向服务端写数据
                say=new Scanner(System.in);
                writer=new PrintWriter(socket.getOutputStream());
                while (say.hasNextLine()) {
                    String s = say.nextLine();
                    writer.println(s);
                    writer.flush();
                    String serverStr=scannerIn.nextLine();
                    System.out.println("小娜:"+serverStr);
                }
    
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                FileUtil.close(writer);
                FileUtil.close(scannerIn);
                FileUtil.close(socket);
            }
        }
    }
    

    相关文章

      网友评论

          本文标题:java 网络编程

          本文链接:https://www.haomeiwen.com/subject/lalpyftx.html