关于java:java基础之十九网络编程

33次阅读

共计 2821 个字符,预计需要花费 8 分钟才能阅读完成。

一、基本概念



1、ip 地址



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

二、网络分层


三、TCP 编程

1、Socket 套接字


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


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();}
}

正文完
 0