使用java从服务器(ServerSocket)读取客户端(客户端Socket)上的字节包
我是新的。 ima java 开发人员(新手),目前正在从事 BSE 项目,我面临着从服务器(服务器套接字)读取客户端(客户端套接字)上的字节包的问题。如果你能帮助我,请帮助我。
提前致谢
i m a new .
i m a java developer(fresher) and currently i m working on BSE project and i m facing problem to read the packet of bytes on the client(client socket) from the server(server socket). if u can help me then please help me.
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,如果您想直接与数据包交互,那么您需要使用
DatagramSocket
而不是常规的Socket
和ServerSocket
。然后,您应该访问此链接来查看一个很好的教程关于如何开始发送和接收单个数据包。
基本思想是,客户端或服务器在等待其伙伴使用
send()
发送数据包时将阻塞recieve()
调用。如果您对问题中指出的单个数据包不感兴趣,那么您将需要使用 Socket 和 ServerSocket。两者之间通信的第一步涉及类似于以下内容的代码:
建立连接后,基本上将有 2 个读/写循环。一种在客户端,一种在服务器。
您将需要为客户端和服务器提供类似的循环。
Well, if you want to interact directly with packets, then you need to use a
DatagramSocket
instead of the regularSocket
andServerSocket
.Then, you should visit this link to see a good tutorial on how to get started with sending and receiving individual packets.
The basic idea is that the Client or Server will block on the
recieve()
call while it waits for its partner to send a packet usingsend()
.If you aren't interested in the individual packets like you indicated in your question, then you will want to use
Socket
andServerSocket
. The first step to communicating between the two involves code that will look similar to the following:After you get connections set up, you will have basically 2 read/write loops. One on the client, and one on the server.
You will need a similar loop for the client and the server.