如何创建一个基本的 Java Server?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2187626/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 04:32:13  来源:igfitidea点击:

How to create a basic Java Server?

javamultithreadingsockets

提问by IApp

Essentially I want a basic Java Server which multiple people can be connected to and when one of the connected clients (Already coded in Obj-c) sends data to it, it sends it back to everyone who is connected.

本质上,我想要一个基本的 Java 服务器,它可以连接到多个人,并且当其中一个连接的客户端(已在 Obj-c 中编码)向它发送数据时,它将数据发送回每个连接的人。

I'm a real Java Newbie and I'm not going to need Java in the forseeable future for anything but this so I want it out the way as soon as possible rather than learning Java properly from scratch. So if anyone has some source code for this or perhaps a tutorial it would be greatly appreciated.

我是一个真正的 Java 新手,在可预见的未来,除此之外我不会需要 Java,所以我希望尽快摆脱它,而不是从头开始正确学习 Java。因此,如果有人对此有一些源代码或教程,我们将不胜感激。

Thanks :) Ozzie

谢谢 :) 奥兹

采纳答案by David Titarenco

Here is a simple "Knock Knock" server courtesy of Sun:

这是 Sun 提供的一个简单的“Knock Knock”服务器:

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

public class KnockKnockServer {
    public static void main(String[] args) throws IOException {

        ServerSocket serverSocket = null;
        try {
            serverSocket = new ServerSocket(4444);
        } catch (IOException e) {
            System.err.println("Could not listen on port: 4444.");
            System.exit(1);
        }

        Socket clientSocket = null;
        try {
            clientSocket = serverSocket.accept();
        } catch (IOException e) {
            System.err.println("Accept failed.");
            System.exit(1);
        }

        PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
        BufferedReader in = new BufferedReader(
                new InputStreamReader(
                clientSocket.getInputStream()));
        String inputLine, outputLine;
        KnockKnockProtocol kkp = new KnockKnockProtocol();

        outputLine = kkp.processInput(null);
        out.println(outputLine);

        while ((inputLine = in.readLine()) != null) {
             outputLine = kkp.processInput(inputLine);
             out.println(outputLine);
             if (outputLine.equals("Bye."))
                break;
        }
        out.close();
        in.close();
        clientSocket.close();
        serverSocket.close();
    }
}

You can't get much simpler than this.

没有比这更简单的了。

回答by Keith Adler

回答by clamp

you will probably need to use the serversocketclass.

您可能需要使用serversocket类。

回答by JorgeO

I did a Java program that basically implemented a sort of chat between a client and a server. Used a socket to open up a port of the server that would hear incoming connections. You should have a thread hearing them and calling actions when ever a valid connection would come in.

我做了一个 Java 程序,它基本上实现了客户端和服务器之间的一种聊天。使用套接字打开服务器的端口,该端口将听到传入的连接。您应该有一个线程可以听到它们并在有效连接进入时调用操作。

回答by Philippe

Try using the Jetty server API. http://wiki.eclipse.org/Jetty/Tutorial/Embedding_Jettylooks like a good starting point...

尝试使用 Jetty 服务器 API。http://wiki.eclipse.org/Jetty/Tutorial/Embedding_Jetty看起来是一个很好的起点......

回答by Adam MacLeod

There is a straightforward tutorial available via Sun:

Sun 提供了一个简单的教程:

http://java.sun.com/developer/onlineTraining/Programming/BasicJava2/socket.html#server

http://java.sun.com/developer/onlineTraining/Programming/BasicJava2/socket.html#server

It starts with a basic single thread as above and extends to use multiple as required.

它从一个基本的单线程开始,并根据需要扩展到使用多个。