Java进阶教程7:Java网络编程基础与实战

Java进阶教程7:Java网络编程基础与实战

本文将介绍Java网络编程的基础知识,如Socket编程、URL、URLConnection等,并通过实例讲解如何进行实战应用。

Java网络编程基础

Socket编程

Socket编程是Java网络编程的基础,它允许我们在不同主机之间建立全双工通信。Socket编程涉及到两个主要的类:ServerSocketSocketServerSocket用于监听客户端连接,Socket用于建立与客户端的连接。以下是一个简单的Socket服务器端示例:

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

public class SocketServer {
    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket(8080);
        System.out.println("服务器已启动,等待客户端连接...");

        while (true) {
            Socket socket = serverSocket.accept();
            System.out.println("客户端已连接:" + socket.getInetAddress().getHostAddress());

            BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            PrintWriter out = new PrintWriter(socket.getOutputStream(), true);

            String message = in.readLine();
            System.out.println("接收到客户端消息:" + message);

            out.println("你好,我是服务器,已收到你的消息:" + message);

            in.close();
            out.close();
            socket.close();
        }
    }
}

URL

URL(Uniform Resource Locator,统一资源定位符)是网络资源的位置表示。Java中的URL类提供了表示和解析URL的方法。以下是一个简单的URL示例:

import java.net.URL;

public class URLExample {
    public static void main(String[] args) {
        try {
            URL url = new URL("https://www.example.com");
            System.out.println("URL解析成功:" + url.toExternalForm());
        } catch (MalformedURLException e) {
            System.out.println("URL解析失败:" + e.getMessage());
        }
    }
}

URLConnection

URLConnection类提供了用于连接和操作URL的方法。它支持HTTP、HTTPS和FTP协议。以下是一个简单的HTTP GET请求示例:

import java.io.*;
import java.net.URL;
import java.net.URLConnection;

public class URLConnectionExample {
    public static void main(String[] args) throws IOException {
        URL url = new URL("https://www.example.com");
        URLConnection connection = url.openConnection();
        connection.setRequestMethod("GET");

        BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        String line;
        StringBuilder response = new StringBuilder();

        while ((line = in.readLine()) != null) {
            response.append(line);
        }

        in.close();
        connection.disconnect();

        System.out.println("HTTP响应:" + response.toString());
    }
}

实战应用

以下是一个简单的网络聊天室程序,使用Socket编程实现。

服务器端

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

public class ChatServer {
    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket(8080);
        System.out.println("服务器已启动,等待客户端连接...");

        while (true) {
            Socket socket = serverSocket.accept();
            System.out.println("客户端已连接:" + socket.getInetAddress().getHostAddress());

            BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            PrintWriter out = new PrintWriter(socket.getOutputStream(), true);

            String message = in.readLine();
            System.out.println("接收到客户端消息:" + message);

            out.println("你好,我是服务器,已收到你的消息:" + message);

            Broadcast(socket, message);

            in.close();
            out.close();
            socket.close();
        }
    }

    public static void Broadcast(

好好学习,天天向上

京ICP备2023026874号-1

京公网安备11010702002577号