Home > Java > javaTutorial > How to apply Java network programming knowledge to real projects

How to apply Java network programming knowledge to real projects

WBOY
Release: 2024-05-09 11:51:02
Original
600 people have browsed it

This article provides a step-by-step guide on how to build an instant messaging (IM) application using Java network programming. This guide covers: Creating server-side components. Create client components. Run server and client code to send and receive messages. By following these steps, you can use Java network programming to develop various network applications such as web crawlers, file transfer systems, and online games.

如何将 Java 网络编程知识应用到实际项目中

A guide to applying Java network programming to real-world projects

Developing network applications in Java involves creating server-side and client end component. This article will guide you step-by-step through building a simple instant messaging (IM) application using your knowledge of Java network programming.

Server side

  1. Create a new Java project and add the necessary dependencies.
  2. Create a Server class and extend the ServerSocket class.
  3. In the main method, create a ServerSocket instance and listen to the specified port.
  4. Use the accept method to wait for incoming client connections.
  5. Create a new thread for each connection, which is responsible for handling receiving and sending messages from the client.

Client

  1. Create a new Java project and add the necessary dependencies.
  2. Create a Client class for connecting to the server.
  3. In the main method, create a Socket instance and connect to the server through the server address and port.
  4. Read and send messages from the server using the getInputStream and getOutputStream methods.

Practical Case: Simple IM Application

Using the above steps, we can build a simple IM application that allows users to send and receive messages over the network .

Server-side code:

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {

    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket(8080);

        while (true) {
            Socket clientSocket = serverSocket.accept();
            Thread thread = new Thread(new ClientHandler(clientSocket));
            thread.start();
        }
    }
}

class ClientHandler implements Runnable {

    private Socket clientSocket;

    public ClientHandler(Socket clientSocket) {
        this.clientSocket = clientSocket;
    }

    @Override
    public void run() {
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
            PrintWriter writer = new PrintWriter(clientSocket.getOutputStream());

            while (true) {
                String message = reader.readLine();
                if (message == null) {
                    break;
                }

                System.out.println("Received message: " + message);
                writer.println("Message received: " + message);
                writer.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                clientSocket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
Copy after login

Client-side code:

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

public class Client {

    public static void main(String[] args) throws IOException {
        Socket clientSocket = new Socket("localhost", 8080);

        BufferedReader reader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
        PrintWriter writer = new PrintWriter(clientSocket.getOutputStream());

        while (true) {
            BufferedReader consoleReader = new BufferedReader(new InputStreamReader(System.in));
            String message = consoleReader.readLine();

            writer.println(message);
            writer.flush();

            String serverMessage = reader.readLine();
            System.out.println("Server response: " + serverMessage);
        }
    }
}
Copy after login

Run the server-side and client-side code and you will It's time to send and receive messages between two computers!

Other application scenarios

Java network programming is also widely used in the following scenarios:

  • Web crawler
  • File transfer System
  • Video Streaming
  • Online Game

The above is the detailed content of How to apply Java network programming knowledge to real projects. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template