【问题标题】:How to publish socket server in JAVA?如何在 JAVA 中发布套接字服务器?
【发布时间】:2022-06-21 15:45:20
【问题描述】:

我想在我的项目的窗口主机或其他主机上发布我的套接字服务器。但是,我找不到如何做到这一点。因为我找不到任何例子。 这里有服务器和客户端,但我将使用服务器一。客户端只是为了测试它。 此代码也可以在 Windows 终端中运行。这是一个很好的观点。请帮助我如何解决它。

服务器.JAVA

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

public class Server
{
    //initialize socket and input stream
    private Socket          socket   = null;
    private ServerSocket    server   = null;
    private DataInputStream in       =  null;

    // constructor with port
    public Server(int port)
    {
        // starts server and waits for a connection
        try
        {
            server = new ServerSocket(port);
            System.out.println("Server started");

            System.out.println("Waiting for a client ...");

            socket = server.accept();
            System.out.println("Client accepted");

            // takes input from the client socket
            in = new DataInputStream(
                    new BufferedInputStream(socket.getInputStream()));

            String line = "";

            // reads message from client until "Stop" is sent
            while (!line.equals("Stop"))
            {
                try
                {
                    line = in.readUTF();
                    System.out.println(line);

                }
                catch(IOException i)
                {
                    System.out.println(i);
                }
            }
            System.out.println("Closing connection");

            // close connection
            socket.close();
            in.close();
        }
        catch(IOException i)
        {
            System.out.println(i);
        }
    }

    public static void main(String args[])
    {
        Server server = new Server( 5000);
    }
}

Client.java

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

public class Client
{
    // initialize socket and input output streams
    private Socket socket            = null;
    private DataInputStream  input   = null;
    private DataOutputStream out     = null;

    // constructor to put ip address and port
    public Client(String address, int port)
    {
        // establish a connection
        try
        {
            socket = new Socket(address, port);
            System.out.println("Connected");

            // takes input from terminal
            input  = new DataInputStream(System.in);

            // sends output to the socket
            out    = new DataOutputStream(socket.getOutputStream());
        }
        catch(UnknownHostException u)
        {
            System.out.println(u);
        }
        catch(IOException i)
        {
            System.out.println(i);
        }

        // string to read message from input
        String line = "";

        // keep reading until "Stop" is input
        while (!line.equals("Stop"))
        {
            try
            {
                line = input.readLine();
                out.writeUTF(line);
            }
            catch(IOException i)
            {
                System.out.println(i);
            }
        }

        // close the connection
        try
        {
            input.close();
            out.close();
            socket.close();
        }
        catch(IOException i)
        {
            System.out.println(i);
        }
    }

    public static void main(String args[])
    {
        Client client = new Client("127.0.0.1", 5000);
    }
}

【问题讨论】:

    标签: java sockets server publish


    【解决方案1】:

    没有“发布”之类的东西。只需运行您的服务器。而已。现在客户端可以连接到它了。

    如果您的客户端和服务器之间有防火墙,那么您必须适当地配置防火墙。如果您在具有私有地址的网络上(典型的家庭 LAN 设置),那么您必须在 NAT 框(“路由器”)中安排端口转发。

    【讨论】:

      猜你喜欢
      • 2013-02-11
      • 1970-01-01
      • 2011-11-28
      • 1970-01-01
      • 2021-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-21
      相关资源
      最近更新 更多