Home >Java >javaTutorial >Network programming in Java
With the rapid development of the Internet, network programming has become more and more important. As a popular programming language, Java naturally has strong network programming capabilities. This article will provide a brief introduction to network programming in Java.
In Java, network programming requires the use of two important classes: Socket and ServerSocket. The Socket class is used to establish client-side connections, while ServerSocket is used to create server-side connections. Socket objects are created by specifying the IP address and port number, while ServerSocket is created by specifying the local port number.
When doing network programming, you need to understand some basic network transmission protocols, such as TCP/IP, UDP and HTTP. TCP/IP and UDP are the two most commonly used protocols. TCP/IP is a connection-oriented protocol that provides reliable data transmission, while UDP is a connectionless protocol that provides faster data transmission. The HTTP protocol is an offline request and response protocol, which is widely used in communication between web servers and browsers.
The following is an example of a simple client/server program:
Client:
import java.net.*; import java.io.*; public class Client { public static void main(String [] args) { String serverName = args[0]; int port = Integer.parseInt(args[1]); try { System.out.println("连接到主机:" + serverName + " ,端口号:" + port); Socket client = new Socket(serverName, port); System.out.println("远程主机地址:" + client.getRemoteSocketAddress()); OutputStream outToServer = client.getOutputStream(); DataOutputStream out = new DataOutputStream(outToServer); out.writeUTF("Hello from " + client.getLocalSocketAddress()); InputStream inFromServer = client.getInputStream(); DataInputStream in = new DataInputStream(inFromServer); System.out.println("服务器响应: " + in.readUTF()); client.close(); } catch (IOException e) { e.printStackTrace(); } } }
Server side:
import java.net.*; import java.io.*; public class Server extends Thread { private ServerSocket serverSocket; public Server(int port) throws IOException { serverSocket = new ServerSocket(port); serverSocket.setSoTimeout(10000); } public void run() { while(true) { try { System.out.println("等待客户端连接,端口号为:" + serverSocket.getLocalPort() + "..."); Socket server = serverSocket.accept(); System.out.println("远程主机地址:" + server.getRemoteSocketAddress()); DataInputStream in = new DataInputStream(server.getInputStream()); System.out.println(in.readUTF()); DataOutputStream out = new DataOutputStream(server.getOutputStream()); out.writeUTF("感谢连接我:" + server.getLocalSocketAddress() + " Goodbye!"); server.close(); } catch (SocketTimeoutException s) { System.out.println("Socket timed out!"); break; } catch (IOException e) { e.printStackTrace(); break; } } } public static void main(String [] args) { int port = Integer.parseInt(args[0]); try { Thread t = new Server(port); t.start(); } catch (IOException e) { e.printStackTrace(); } } }
This program demonstrates simple communication between client and server. When the client runs, it sends a string to the server, and the server responds with the string.
Network programming in Java can be performed using protocols such as TCP/IP and UDP. When writing network programs, you need to use the Socket class and ServerSocket class.
The above is just the introductory part of network programming in Java. There are many things that need to be understood in depth about network programming, such as the sending and receiving of data packets, multi-threaded network programming, and accessing web servers through HTTP.
The above is the detailed content of Network programming in Java. For more information, please follow other related articles on the PHP Chinese website!