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.
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
Server
class and extend the ServerSocket
class. main
method, create a ServerSocket
instance and listen to the specified port. accept
method to wait for incoming client connections. Client
Client
class for connecting to the server. main
method, create a Socket
instance and connect to the server through the server address and port. 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(); } } } }
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); } } }
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:
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!