📜  在Java中创建一个套接字来向单个客户端显示消息

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

在Java中创建一个套接字来向单个客户端显示消息

本文介绍了基本的客户端-服务器连接,其中客户端连接,服务器向客户端发送消息,客户端使用套接字连接显示消息。客户端程序套接字与服务器应用程序的服务器套接字建立连接,然后服务器套接字与服务器应用程序中的内部套接字连接。

客户端程序

客户端程序使用 Socket 类与服务器建立连接。 Socket 对象需要服务器的地址和服务器的端口号。

Java
// Client program
  
import java.io.*;
import java.net.*;
  
class GFG {
    
    // driver function
    public static void main(String[] args)
    {
        try {
            
            // Create socket object by passing id address
            // and port number establish connection
            Socket socket = new Socket("localhost", 1346);
            System.out.println(
                "Connected Successfully.....");
  
            // Buffer reader to get all the input stream
            BufferedReader bs = new BufferedReader(
                new InputStreamReader(s.getInputStream()));
            System.out.println("Response from Server.....");
  
            // Print response from server
            System.out.println("Client Side : "
                               + br.readLine());
            // Close the connection
            socket.close();
        }
        catch (UnknownHostException e) {
            
            // Catch block for IP errors
            System.out.println("IP not found for" + e);
        }
        catch (IOException e) {
            
            // Catch block for data stream errors
            System.out.println("Not found data for socket"
                               + e);
        }
    }
}


Java
// Server program
  
import java.io.*;
import java.net.*;
  
class GFG {
    public static void main(String[] args)
    {
        try {
            
            // establish connection
            ServerSocket serversocket
                = new ServerSocket(1346);
  
            System.out.println("waiting for request....");
  
            // Socket object to accept all the connections
            Socket socket = serversocket.accept();
  
            System.out.println("Request Accepted...");
            
            // Printstream to print all the data
            PrintStream ps
                = new PrintStream(socket.getOutputStream());
  
            BufferedReader br = new BufferedReader(
                new InputStreamReader(System.in));
            
            System.out.println(
                "Input the data at the server...");
            
            // Printing bufferedreader data
            ps.print(br.readLine());
            socket.close();
            serversocket.close();
        }
        catch (IOException e) {
            
            // Catch block for data stream errors
            System.out.println("Not found data for socket"
                               + e);
        }
    }
}


服务器端程序

Server 程序使用 Server Socket 类与客户端建立连接。服务器套接字对象需要端口号。

Java

// Server program
  
import java.io.*;
import java.net.*;
  
class GFG {
    public static void main(String[] args)
    {
        try {
            
            // establish connection
            ServerSocket serversocket
                = new ServerSocket(1346);
  
            System.out.println("waiting for request....");
  
            // Socket object to accept all the connections
            Socket socket = serversocket.accept();
  
            System.out.println("Request Accepted...");
            
            // Printstream to print all the data
            PrintStream ps
                = new PrintStream(socket.getOutputStream());
  
            BufferedReader br = new BufferedReader(
                new InputStreamReader(System.in));
            
            System.out.println(
                "Input the data at the server...");
            
            // Printing bufferedreader data
            ps.print(br.readLine());
            socket.close();
            serversocket.close();
        }
        catch (IOException e) {
            
            // Catch block for data stream errors
            System.out.println("Not found data for socket"
                               + e);
        }
    }
}