java 创建一个简单的 LAN 信使
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11079068/
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
Creating a simple LAN messenger
提问by Fasih Khatib
I want to create a simple LAN conference-chat style messenger in Java but I have no clue where to start.
It must have the following features:
我想用 Java 创建一个简单的 LAN 会议聊天风格的信使,但我不知道从哪里开始。
它必须具有以下特点:
Just imagine it as being a messenger allowing all the employees in one building to chat with each other把它想象成一个信使,让一栋楼的所有员工互相聊天
回答by Kallja
Though your question is pretty vague you seem to have the basics (sockets and all that) in place. I suggest you start by reading the All About Socketsand All About DatagramsJava lessons on Oracle's site to get started. The main content from the second lesson you may want to digest is the part about broadcasting (for the purposes of automatic server detection).
尽管您的问题很模糊,但您似乎已经掌握了基础知识(套接字和所有这些)。我建议您首先阅读Oracle 站点上的所有关于套接字和关于数据报的Java 课程以开始使用。您可能想要消化的第二课的主要内容是关于广播的部分(出于自动服务器检测的目的)。
Here's how i'd go about the implementation on a high level:
以下是我将如何在高层次上进行实施:
- Implement an application that contains server AND client functionality in one executable.
- When the app is started, run the server if no other server is detected (automatically or specified by the user).
- Always run the client. What this means, is that no dedicated server will be used as one of the clients acts as the server. Each client connects to the server (including the client running on the same machine as the server).
- 在一个可执行文件中实现一个包含服务器和客户端功能的应用程序。
- 当应用程序启动时,如果没有检测到其他服务器(自动或由用户指定),则运行服务器。
- 始终运行客户端。这意味着没有专用服务器将用作客户端之一作为服务器。每个客户端都连接到服务器(包括与服务器在同一台机器上运行的客户端)。
There obviously are numerous ways to make this kind of an application. I'm not saying that the way that i described is the best. It is, however, probably suitable to the use case you described, and its implementation is fairly simple.
显然有很多方法可以制作这种应用程序。我并不是说我描述的方式是最好的。但是,它可能适用于您描述的用例,并且其实现相当简单。
回答by Qwerky
Sounds like you need a server app and a client app for each user. They will communicate over sockets. The server will open a ServerSocket
and the clients will create Socket
s and connect to the server when they want to chat.
听起来您需要为每个用户提供一个服务器应用程序和一个客户端应用程序。他们将通过套接字进行通信。服务器会打开a ServerSocket
,客户端会创建Socket
s并在他们想要聊天时连接到服务器。
The server needs to be able to accept connections from clients. The server will hold all the global details, such as what chat rooms exist, who is in each etc. The basic behaviour is that when there are several people (clients) in a chat room, one client will say something, this is sent to the server over the socket. The server has a list of all the clients (sockets) who are in the chat room, and it sends the message to each of them.
服务器需要能够接受来自客户端的连接。服务器会保存所有的全局细节,比如有哪些聊天室,每个聊天室都有谁等。基本行为是当一个聊天室有几个人(客户端)时,一个客户端会说些什么,这被发送到服务器通过套接字。服务器具有聊天室中所有客户端(套接字)的列表,并将消息发送给每个客户端。
Finally, you need to be aware that the server will have to be multithreaded and will probably require a new thread for each client socket that connects.
最后,您需要注意服务器必须是多线程的,并且可能需要为每个连接的客户端套接字创建一个新线程。
回答by raven1981
Since you don't tell if there's going to be a server for that purpose or not, maybe, in addition to previous responses, it will be interesting for you the next link:
由于您不知道是否会有用于该目的的服务器,也许除了之前的响应之外,下一个链接对您来说会很有趣:
http://docs.oracle.com/javase/tutorial/networking/datagrams/broadcasting.html
http://docs.oracle.com/javase/tutorial/networking/datagrams/broadcasting.html
The block option can be implemented saving a list of IPs and ignoring the messages coming from them.
可以通过保存 IP 列表并忽略来自它们的消息来实现阻止选项。
You must look into swing tutorial as well so you can see how you can create the windows, textboxes, textareas, buttons, and so on, so you can create you're interface:
您还必须查看 Swing 教程,以便了解如何创建窗口、文本框、文本区域、按钮等,以便创建您的界面:
http://docs.oracle.com/javase/tutorial/uiswing/
http://docs.oracle.com/javase/tutorial/uiswing/
You can save the user quite easy using a properties file for example, but maybe you must start learning java from the beginning if you're making this kind of questions.
例如,您可以使用属性文件轻松保存用户,但如果您提出此类问题,也许您必须从头开始学习 Java。