Java 小程序网络
我正在研究 java 网络,并且想要获得更多知识并学习 java 的这个主题,为此,我决定制作一个简单的在线聊天类型小程序,以便它可以在客户端之间进行交互。
它的小程序部分很好,我知道如何做到这一点,但我不确定从哪里开始寻找网络部分,或者具体要寻找什么。我有自己的网络服务器,并且希望能够将其用作实际的服务器部分,并且我不确定我所查看的教程是否是我需要查看的内容。
基本上,我只想要客户 ->服务器<-客户端,以便所有客户端都可以看到相同的消息,因此不知道从哪里开始。
这些是我引用的主要页面。
http://docs.oracle.com/javase/tutorial/networking/sockets /clientServer.html
http://docs.oracle.com/javase/tutorial/networking/overview/networking.html
提前致谢
I am looking into java networking and want to be more knowledgeable and learn this subject of java, and to do so I decided I would make a simple online chat type applet so that it can interface between the clients.
The applet part of it is fine, and I understand how to do that, but I am not sure where to begin looking, or specifically what to be looking at, for the networking part. I have my own webserver and want to be able to use that as the actual server part, and I'm not sure if the tutorials I have looked at is what I need to be looking at.
Basically, I just want Client -> Server <- Client so that all of the clients can see the same message, so not sure where to start.
These were the main pages I was referencing.
http://docs.oracle.com/javase/tutorial/networking/sockets/clientServer.html
http://docs.oracle.com/javase/tutorial/networking/overview/networking.html
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
请查看此小程序教程。请注意,使用 Java 小程序,您只能与提供小程序的服务器进行通信。这是出于安全原因。
Have a look at this applet tutorial. Note that with Java applets you can only communicate with the server from which the applet was served. This is for security reasons.
套接字是 - IP 地址 + 端口号。
(将端口号想象为同一个房间的许多门,以便许多人可以同时进入)。
现在,聊天服务器的工作是侦听通过计算机的该端口号上的 TCP 发出的所有请求。这是一个无限循环,例如:
while(true) {监听端口号xxxx}
。现在,聊天客户端功能是在该端口号上的该 IP 地址处建立与服务器的连接(例如 127.0.0.1:2020)。现在建立连接后,您可以向服务器发送数据。(在聊天服务器的情况下,当服务器从一个客户端接收消息时,它会将消息广播给所有客户端)。
java.net 包提供了 ServerSocket 和 Socket 类来执行这些任务。
java.net 中常用的对象有:
MalformURLException、BindException、ConnectException、UnknownHOstException、SocketTimeoutException、PortUnreachableException、
URL、URI、URLStreamHandler、
Inet4地址,
MulticastSocket、ServerSocket、Socket、URLEncoder、URLDecoder。
GUI部分你可以自己制作,正如你所说的你擅长小程序(我擅长小程序)。
A Socket is - IP Address + Port Number.
(Think of a port numbers as many doors to same room so that many people can get into it at a same time).
Now, a chat server's job is to listen to all the requests coming through TCP on that port number of the machine. It is a infinite loop like:
while(true) { listen to port number xxxx }
. Now a chat client function is to establish a connecion to the server at that IP Address on that port number (eg. 127.0.0.1:2020). Now after connection is established you can send data to the server.(In case of chat server when the server recieves message from one client, it broadcasts the message to all the clients).
java.net package provides ServerSocket and Socket class to perform these tasks.
Commonly used objects in java.net are-
MalformURLException, BindException, ConnectException, UnknownHOstException, SocketTimeoutException, PortUnreachableException,
URL, URI, URLStreamHandler,
Inet4Address,
MulticastSocket, ServerSocket, Socket, URLEncoder, URLDecoder.
The GUI part you can make it yourself as you said you are good in applet ( I am bad in applet).
您可以访问此教程。它将逐步展示用 java 构建聊天应用程序,
首先没有 GUI,最后得到一个带有 GUI 的基本聊天应用程序。一切顺利 :)
。
you can visit this tutorial. It will show step by step building chat application in java,
first without GUI, and end up with a basic chat application with GUI. All the best :)
.