java网络编程三要素及socket编程

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lsf921016/article/details/61147761

1.IP地址

  • 网络中host的标识
  • 不易记忆,可用主机名或域名
  • 本地回环地址,localhost:127.0.0.1

    存在于 :

java.net.InetAddress

常见方法:

        //获取本地主机ip地址对象
        InetAddress ip=InetAddress.getLocalHost();

        System.out.println(ip.getHostName());
        System.out.println(ip.getHostAddress());

        //获取其他主机 ip地址对象
        InetAddress ip2=InetAddress.getByName("www.baidu.com");//host 可以是ip地址,主机名,域名
        System.out.println(ip2.getHostName());
        System.out.println(ip2.getHostAddress());

2.Port number

  • 用于标识进程的逻辑地址
  • 有效端口号:0-65535
  • 系统保留端口号:0-1024

3.传输协议

-通讯的规则
常见协议:HTTP,TCP,UDP,RTP,SMTP

1. UDP:

1.将 数据、源、目的封装成数据包,不需要建立连接
2.每个数据包大小限制在64kb
3.无连接不可靠
4.速度快

2. TCP

1。建立连接,形成数据传输通道
2。在连接中进行大量数据传输
3.通过三次握手建立连接,是可靠协议。
(三次握手:1.request for connection  2.response to connection 3.acknowledge for response)

4.socket

建立网络通信连接至少要一对端口号(socket)。socket本质是编程接口(API),对TCP/IP的封装,TCP/IP也要提供可供程序员做网络开发所用的接口,这就是Socket编程接口;HTTP是轿车,提供了封装或者显示数据的具体形式;Socket是发动机,提供了网络通信的能力。

udp传输:

  1. 使用DatagramSocket和DatagramPacket
  2. 建立发送端,接收端
  3. 调用socket发送接收方法
  4. 关闭socket
package socket.udp;

import java.io.IOException;
import java.net.*;

/**
 * 创建UDP传输的发送端
 * /        *思路:
 * /          1.建立UDP的socket服务
 * /          2.将要发送的数据封装到datagram中
 * /          3.通过UDP的socket服务将datagram发送出去
 * /          4.关闭socket服务
 */
public class UdpSender {
    public static void main(String[] args) throws IOException {

        //1.建立UDP的socket服务
        DatagramSocket ds=new DatagramSocket();

        //2.将要发送的数据封装到datagram中
        String str="this is a text to send";
        byte[] buf=str.getBytes();
        DatagramPacket dp=new DatagramPacket(buf, buf.length, InetAddress.getByName("10.0.0.3"),10000);

        //3.通过UDP的socket服务将datagram发送出去,使用send方法
        ds.send(dp);

        //4.关闭socket服务
        ds.close();
        System.out.println("sender closed");

    }

}
//创建UDP接收端
public class UdpReceiver {
    public static void main(String[] args) throws IOException {
        System.out.println("receiving.......");
        //1.建立udp服务
        DatagramSocket ds = new DatagramSocket(10000);
        //2.创建数据包,用于储存收到的数据,方便用数据包对象的方法解析这些数据
        byte[] buf = new byte[1024];
        DatagramPacket dp = new DatagramPacket(buf, buf.length);

        //3.使用socket服务的receive方法,将接受到的数据存入数据包中
        ds.receive(dp);//阻塞式

        //4.通过数据包中的方法解析包内数据,包括地址,端口,数据
        String ip=dp.getAddress().getHostAddress();
        int port=dp.getPort();
        String text=new String(dp.getData(),0,dp.getLength());
        System.out.println(ip+":"+port+":"+text);
        //5.关闭资源
        ds.close();
        System.out.println("receiver closed");
    }
}

TCP传输

**
 * 
 *
 * TCP 传输,客户端建立的过程:
 *
 1.创建客户端socket对象,建议对象创建时就明确目的地
 2.如果连接建立成功,说明数据通道(socket流)已建立,
 可以通过getInputStream或getOutputStream来获取。
 3.用输出流将数据写出
 */
public class Client {
    public static void main(String[] args) throws IOException {
        //创建客户端socket服务
        Socket socket=new Socket("10.0.0.3",9999);

        //获取socket流中的输出流
        OutputStream out = socket.getOutputStream();

        //使用输出流将指定数据写出去
        out.write("第一条tcp发送的数据".getBytes());

        //读取服务端返回的数据,使用socket输入流
        InputStream in=socket.getInputStream();
        byte[] buf=new byte[1024];
        int len=in.read(buf);
        String text=new String(buf,0,len);
        System.out.println(text);

        //关闭资源
        socket.close();
    }
}
/**
 * 
 * 建立tcp服务端的思路:
 * 1.通过Serversocket创建服务端socket服务
 * 2.服务端必须对外提供一个端口
 * 3.获取连接的客户端对象
 * 4.通过客户端对象获取socket流,读取客户端发来的数据
 * 5.关闭资源,关闭客户端,服务端
 */
public class Server {
    public static void main(String[] args) throws IOException {
        //创建服务端对象
        ServerSocket ss=new ServerSocket(9999);

        //获取连接的客户端对象
        Socket s=ss.accept();
        String ip=s.getInetAddress().getHostAddress();

        //通过socket对象获取输入流,来读取客户端发来的数据
        InputStream in=s.getInputStream();
        byte[] buf=new byte[1024];
        int len=in.read(buf);
        String text=new String(buf,0,len);
        System.out.println(ip+"server:"+text);

        //使用客户端socket对象的输出流给客户端返回数据
        OutputStream out=s.getOutputStream();
        out.write("gocha".getBytes());

        s.close();
    }
}

猜你喜欢

转载自blog.csdn.net/lsf921016/article/details/61147761