java基础之十九->网络编程

一、基本概念

image.png
image.png
image.png

1、ip地址

image.png
image.png
image.png

2、IP地址所对应的对象->InetAddress

public class InetAddressDemo {
    public static void main(String[] args) throws UnknownHostException {
        InetAddress localHost = InetAddress.getLocalHost();
        //主机名和IP地址
 System.out.println(localHost);
        InetAddress liu = InetAddress.getByName("www.baidu.com");
        System.out.println(liu);
        System.out.println(liu.getHostAddress());
        System.out.println(liu.getHostName());
    }
}

3、端口

image.png

二、网络分层

image.png
image.png

三、TCP编程

image.png

1、Socket套接字

image.png
image.png

package com.msbline.SocketPkg.tcpPkg.server;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Server2 {
    public static void main(String[] args) throws IOException {
        //服务端需要使用ServerSocket来开放本地端口
 ServerSocket server = new ServerSocket(10086);
        //接收client传输过来的数据,需要定义socket对象
 Socket accept = server.accept();
        //通过server获取输入流对象
 InputStream inputStream = accept.getInputStream();
        byte[] buf = new byte[1024];
        int length = inputStream.read(buf);
        System.out.println("客户端传输的数据是:"+new String(buf,0,length));
        OutputStream outputStream = accept.getOutputStream();
        outputStream.write("hello client".getBytes());
        outputStream.close();
        inputStream.close();
        accept.close();
        server.close();
    }
}
package com.msbline.SocketPkg.tcpPkg.client;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
/**
 * 客户端向服务端发送数据
 */
public class Client2 {
    public static void main(String[] args) throws IOException {
        //创建socket,实际上是开启实现io的虚拟接口(此接口
 //不算Java中的接口,而是类似于网线的插槽),需要指定数据接收放的io和端口
 Socket client = new Socket("127.0.0.1",10086);
        //-----向外进行输出------
 OutputStream outputStream = client.getOutputStream();
        outputStream.write("hello java".getBytes());
        //-----接收服务器端返回的消息------
 InputStream inputStream = client.getInputStream();
        byte[] buf = new byte[1024];
        int length = inputStream.read(buf);
        System.out.println("服务端相应"+new String(buf,0,length));
        inputStream.close();
        outputStream.close();
        client.close();
    }
}

四、UDP编程

image.png
image.png

package com.msbline.SocketPkg.tcpPkg.server;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class UdpServer {
    public static void main(String[] args) throws Exception {
        DatagramSocket server = new DatagramSocket(10001);
        byte[] buf = new byte[1024];
        //用来接收数据
 DatagramPacket packet = new DatagramPacket(buf,buf.length);
        //接收数据
 server.receive(packet);
        System.out.println(new String(packet.getData(),0,packet.getData().length));
        server.close();
    }
}
package com.msbline.SocketPkg.tcpPkg.client;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Scanner;
public class UdpClient {
    public static void main(String[] args) throws Exception {
        DatagramSocket datagramSocket = new DatagramSocket(10086);
        Scanner scanner = new Scanner(System.in);
        String str = scanner.nextLine();
        DatagramPacket packet = new DatagramPacket(str.getBytes(),
                str.getBytes().length,
                InetAddress.getByName("localhost"),10001);
        datagramSocket.send(packet);
        datagramSocket.close();
    }
}

你可能感兴趣的:(java)