zoukankan      html  css  js  c++  java
  • 网络编程

    1.IP

    ip相关类InetAddress

    • static InetAddress getLocalHost()返回本地主机
    • String getHostName() 返回IP地址的主机名
    • String getAddress() 返回IP地址字符串
    • static InetAddress getByName(String host) 给定主机名的情况下确定IP
        public static void main(String[] args) throws UnknownHostException{
            InetAddress i =InetAddress.getLocalHost();//返回本地主机
            System.out.println(i.toString());
            //String getHostName 
            System.out.println("address:"+i.getHostName());//返回主机名
            // String getHostAddress()
            System.out.println("ip:"+i.getHostAddress());//返回 IP 
            InetAddress i2 =InetAddress.getByName("www.qq.com");
            System.out.println(i2.toString());
        }
    }

    2.网络通信协议

    UDP:

    1.将数据及源和目的封装成数据包中,不需要建立连接

    2.每个数据包的大小限制在64k内

    3.因为是无连接,是不可靠协议

    4.不需要建立连接,速度快。

    DatagramSocket类:此类表示用来发送和接收数据报包的套接字。

    DatagramPacket类:此类表示数据报包。

    发送端:

    public class UdpSend2 {
        public static void main(String[] args) throws IOException{
            DatagramSocket ds = new DatagramSocket();
            byte[] buf ="zhangsan".getBytes();
            DatagramPacket dp = new  DatagramPacket(buf,buf.length,InetAddress.getByName("192.168.1.103"), 10001);
            ds.send(dp);
            ds.close();
        }
    }

    接收端:

    public class UdpRec2 {
        public static void main(String[] args) throws IOException{
            DatagramSocket ds = new DatagramSocket(10001);
            byte[] buf = new byte[1024];
            DatagramPacket dp = new DatagramPacket(buf,buf.length);
            ds.receive(dp);
            String data = new String(dp.getData(),0,dp.getLength());
            System.out.println(data);
        }
    
    }

     键盘录入

    发送端:

     1 import java.io.BufferedReader;
     2 import java.io.IOException;
     3 import java.io.InputStreamReader;
     4 import java.net.DatagramPacket;
     5 import java.net.DatagramSocket;
     6 import java.net.InetAddress;
     7 
     8 public class UdpSend {
     9     public static void main(String[] args) throws IOException{
    10         DatagramSocket ds = new DatagramSocket();
    11         BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
    12         String str = br.readLine();
    13         while(str!=null){
    14             if("886".equals(str))
    15                 break;
    16             byte[] buf =str.getBytes();
    17             DatagramPacket dp = new DatagramPacket(buf,buf.length,InetAddress.getByName("192.168.1.103"),10000);
    18             ds.send(dp);
    19         }
    20         ds.close();
    21     }
    22 }


    接收端

     1 import java.io.IOException;
     2 import java.net.DatagramPacket;
     3 import java.net.DatagramSocket;
     4 
     5 public class UdpRec {
     6     public static void main(String[] args) throws IOException{
     7         DatagramSocket ds = new DatagramSocket(10000);
     8         while(true){
     9             byte[] buf =new byte[1024];
    10             DatagramPacket dp = new DatagramPacket(buf,buf.length);
    11             ds.receive(dp);
    12             String data = new String(dp.getData(),0,dp.getLength());
    13             System.out.println(data+"....");
    14         }
    15         
    16     }
    17 }

    TCP:

    1.建立连接,形成传输数据的通道。

    2.在连接中进行大数据传输。

    3.通过三次握手完成连接,是可靠协议。

    4.必须建立连接,效率会稍低。

    Socket类:此类实现客户端套接字(也可以就叫“套接字”)。

    ServerSocket类:此类实现服务端套接字。

    Tcp实现图片的上传

    客户端代码

     1 import java.io.*;
     2 import java.net.*;
     3 public class PicClient {
     4     public static void main(String[] args) throws UnknownHostException, IOException{
     5         Socket s = new Socket("192.168.1.104",10007);
     6         FileInputStream fis = new FileInputStream("d:\\1.jpg");
     7         OutputStream out = s.getOutputStream();
     8         byte[] buf = new byte[1024];
     9         int len =0;
    10         while((len=fis.read(buf))!=-1){
    11             out.write(buf,0,len);
    12         }
    13         //告诉服务端数据已写完
    14         s.shutdownOutput();
    15         InputStream in = s.getInputStream();
    16         byte[] bufIn = new byte[1024];
    17         int num = in.read(bufIn);
    18         System.out.println(new String(bufIn,0,num));
    19         fis.close();
    20         s.close();
    21         
    22     }
    23 }

    服务端

     1 import java.io.*;
     2 import java.net.*;
     3 public class PicServer {
     4     public static void main(String[] args) throws IOException{
     5         ServerSocket ss = new ServerSocket(10007);
     6         Socket s = ss.accept();
     7         InputStream in = s.getInputStream();
     8         FileOutputStream fos = new FileOutputStream("server.bmp");
     9         byte[] buf = new byte[1024];
    10         int len =0;
    11         while((len=in.read(buf))!=-1){
    12             fos.write(buf,0,len);
    13         }
    14 
    15         OutputStream out = s.getOutputStream();
    16         out.write("上传成功".getBytes());
    17         fos.close();
    18         s.close();
    19         ss.close();
    20         
    21     }
    22 }
  • 相关阅读:
    C51中的 xbyte的使用
    使用正则表达式替换日期格式
    C#制作windows窗体的图书管理系统
    《短码之美》读书笔记3
    VS2019创建第一个ASP.NET网站
    观影大数据分析(上)
    Git提交文件报错解决
    软件设计简单工厂模式
    记录一次MySQL启动异常的解决
    将本机web项目映射到公网访问
  • 原文地址:https://www.cnblogs.com/malinkang/p/2598017.html
Copyright © 2011-2022 走看看