网络编程实现数据传输与反射


  1. 网络编程

    网络编程是指编写运行在多个设备(计算机)的程序,这些设备都通过网络连接起来

    可以自己编写程序传输文件

    使用inetAddress代表网络中的某一台主机

    注意要用try{}catch(){}

    如:public void inetAddress() {

    try {

    // 使用InetAddress代表网络中的某一台主机

    InetAddress inet = InetAddress.getByName("www.baidu.com");

     

    System.out.println(inet.getHostName());

    } catch (UnknownHostException e) {

    e.printStackTrace();

     

    }

     

    }

    java.net 包中提供了两种常见的网络协议的支持:

     

    TCPTCP 是传输控制协议的缩写,它保障了两个应用程序之间的可靠通信。通常用于互联网协议,被称 TCP / IP

     

     

    UDPUDP 是用户数据报协议的缩写,一个无连接的协议。提供了应用程序之间要发送的数据的数据包

     基本TCP协议的网络编程 传入2个参数:第一个需要连接的主机IP 255一个byte65535一个short1024=2^103306  mysql端口、1521 oracle的端口 80 web的默认端口 8080 tomcat默认端口 目标主机的端口号:0-65535

    2. 如何编写服务器

    ServerSocket类的方法:

    public int getLocalPort()  返回此套接字在其上侦听的端口 
    public Socket accept() throws IOException 侦听并接受到此套接字的连接 
    public void setSoTimeout(int timeout)  通过指定超时值启用/禁用 SO_TIMEOUT,以毫秒为单位 
    public void bind(SocketAddress host, int backlog)  ServerSocket 绑定到特定地址(IP 地址和端口号) 


    列子:

     

    public void testServer() {

    try {

    // 创建

    ServerSocket ss = new ServerSocket(2389);

    // 接受客户端的连接

    Socket client = ss.accept();

    InputStream is = client.getInputStream();

    byte[] b = new byte[1024];

    int len;

    StringBuilder sb = new StringBuilder();

    while ((len = is.read(b)) != -1) {

    sb.append(new String(b, 0, len));

    }

    System.out.println(sb.toString());

    OutputStream os = client.getOutputStream();

    os.write("我也是".getBytes());

    is.close();

    } catch (IOException e) {

    e.printStackTrace();

    }

    }

    3.编写客户端

    Socket 类编写

    主要方法:

    public void connect(SocketAddress host, int timeout) throws IOException

    将此套接字连接到服务器,并指定一个超时值

     

    public InetAddress getInetAddress()

      返回套接字连接的地址

    public int getPort()

    返回此套接字连接到的远程端口

    public int getLocalPort()

    返回此套接字绑定到的本地端口

    public SocketAddress getRemoteSocketAddress()

    返回此套接字连接的端点的地址,如果未连接则返回 null

     

    public InputStream getInputStream() throws IOException

    返回此套接字的输入流。

     

    public OutputStream getOutputStream() throws IOException

    返回此套接字的输出流。

     public void close() throws IOException

    关闭此套接字

    列子:

    try {               // 发给服务端    端口

    Socket s = new Socket("192.168.0.56", 2389);

    OutputStream os2= s.getOutputStream();

    InputStream is2=new FileInputStream("E:\\唯有风,懂我\\Avril Lavigne - Wish You Were Here [mqms2].mp3");

    //注意要输入完整的文件路径

    byte[] by=new byte[1024*1024];

    int len2;

    StringBuffer s2=new StringBuffer();

    while((len2=is2.read(by))!=-1) {

    os2.write(by,0,len2);

    }

    OutputStream os=s.getOutputStream();

    String msg=" 我是蓝桥小可爱哟";

    // r = new FileReader("");

    os.write(msg.getBytes());

    //关闭输出流

    s.shutdownOutput();

    InputStream is= s.getInputStream();

    byte[] b = new byte[1024];

    int len;

    StringBuilder sb = new StringBuilder();

    while ((len = is.read(b)) != -1) {

    sb.append(new String(b, 0, len));

     

    }

    System.out.println(sb.toString());

       

    } catch (UnknownHostException e) {

    e.printStackTrace();

    } catch (IOException e) {

    e.printStackTrace();

    }

    }

    4.反射(了解)

猜你喜欢

转载自blog.csdn.net/qq_42293835/article/details/80784079