📜  如何在Java中的特定端口创建套接字?

📅  最后修改于: 2022-05-13 01:54:48.263000             🧑  作者: Mango

如何在Java中的特定端口创建套接字?

套接字是网络上运行的两个程序之间双向通信链路的一个端点。套接字类用于表示客户端程序和服务器程序之间的连接。套接字编程,我们基本上是客户端-服务器编程,其中套接字用作它们之间的链接。我们需要导入 ' Java.net包在我们的程序中提供了两个类,即 Socket 类和 ServerSocket 类。 Socket 类实现连接的客户端,ServerSocket 类实现连接的服务器端。

程序:

为了创建套接字,之后需要使用 Socket 和 ServerSocket 类导入 ' Java.net ' 包,我们创建该类的对象。

  1. 服务器在一个众所周知的端口上打开一个 ServerSocket 并等待输入。
  2. 同时,客户端使用服务器的主机名和这个众所周知的端口地址打开一个(客户端)套接字。
  3. 它向服务器发送请求消息以初始化通信会话。

在服务器端

import java.net.Socket;
ServerSocket mySsocket= new ServerSocket(portnumber);

在客户端

import java.net.Socket;
Socket myCsocket= new Socket( address, portnumber);

示例 1:服务器端

Java
// Java program for creating socket on Server-side
// Server program depicting creation of a socket
// at a specific port
 
// Importing majorly Socket and ServerSocket class
// from java.net package
import java.net.*;
 
// Class
public class GFG {
 
    // Main driver method
    public static void main(String[] args) throws Exception
    {
 
        // Try block to check for exceptions
        try {
 
            // Creating an object of ServerSocket class
            // with the custom port number - 80
            ServerSocket mySsocket = new ServerSocket(80);
 
            // Display commands for better readability
            System.out.println("Server started");
            System.out.println("Waiting for a client ...");
 
            // Here it will wait for any client which wants
            // to get connected to this server
 
            // Establishing a connection
            // using accept() method()
            Socket socket = mySsocket.accept();
 
            // Display message
            System.out.println(
                "Client accepted through the port number: "
                + mySsocket.getLocalPort());
 
            // getLocalPort() function returning the port
            // number which is being used
        }
 
        // Catch block to handle for exceptions
        catch (Exception e) {
 
            // Simply return/exit
            return;
        }
    }
}


Java
// Java program for creating socket on Client-side
// Client program depicting creation of a socket
// at a specific port
 
// Importing majorly Socket and ServerSocket class
// from java.net package
import java.net.*;
 
// Class
public class GFG {
 
    // Main driver method
    public static void main(String[] args) throws Exception
    {
 
        // Try block to check for exceptions
        try {
 
            // Creating an object of Socket class where
            // port number same as server side program
            Socket myCsocket = new Socket("localhost", 80);
 
            // creating client with local ip address
            // port number as '80'
 
            // Display message for better readability
            System.out.println("Connected to Server");
        }
 
        // Catch block to handle exceptions
        catch (Exception e) {
 
            // Simply return and exit the program
            return;
        }
    }
}


输出:

Server started
Waiting for a client ...
Client accepted through the port number: 80

示例 2:客户端

Java

// Java program for creating socket on Client-side
// Client program depicting creation of a socket
// at a specific port
 
// Importing majorly Socket and ServerSocket class
// from java.net package
import java.net.*;
 
// Class
public class GFG {
 
    // Main driver method
    public static void main(String[] args) throws Exception
    {
 
        // Try block to check for exceptions
        try {
 
            // Creating an object of Socket class where
            // port number same as server side program
            Socket myCsocket = new Socket("localhost", 80);
 
            // creating client with local ip address
            // port number as '80'
 
            // Display message for better readability
            System.out.println("Connected to Server");
        }
 
        // Catch block to handle exceptions
        catch (Exception e) {
 
            // Simply return and exit the program
            return;
        }
    }
}

输出:

Connected to Server