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

一、基本概念

java基础之十九->网络编程_第1张图片
java基础之十九->网络编程_第2张图片
java基础之十九->网络编程_第3张图片

1、ip地址

java基础之十九->网络编程_第4张图片
java基础之十九->网络编程_第5张图片
java基础之十九->网络编程_第6张图片

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、端口

java基础之十九->网络编程_第7张图片

二、网络分层

java基础之十九->网络编程_第8张图片
java基础之十九->网络编程_第9张图片

三、TCP编程

java基础之十九->网络编程_第10张图片

1、Socket套接字

java基础之十九->网络编程_第11张图片
java基础之十九->网络编程_第12张图片

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编程

java基础之十九->网络编程_第13张图片
java基础之十九->网络编程_第14张图片

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)