使用java从服务器(ServerSocket)读取客户端(客户端Socket)上的字节包

发布于 2024-09-18 00:28:43 字数 109 浏览 3 评论 0原文

我是新的。 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

也只是曾经 2024-09-25 00:28:43

好吧,如果您想直接与数据包交互,那么您需要使用 DatagramSocket 而不是常规的 SocketServerSocket

然后,您应该访问此链接来查看一个很好的教程关于如何开始发送​​和接收单个数据包。

基本思想是,客户端或服务器在等待其伙伴使用 send() 发送数据包时将阻塞 recieve() 调用。

如果您对问题中指出的单个数据包不感兴趣,那么您将需要使用 Socket 和 ServerSocket。两者之间通信的第一步涉及类似于以下内容的代码:

//Server
// this call will block until the client tries to connect to the server
Socket cientConn = new ServerSocket(8878).accept();
// now you can use the connection's input and output streams to send data

/******************/

// Client
Socket serverConn = new Socket(addressOfServer, 8878);
// now you can use the connections input and output streams

建立连接后,基本上将有 2 个读/写循环。一种在客户端,一种在服务器。

while(true) [
    // check for data from an input stream
    ...
    // respond with message back
}

您将需要为客户端和服务器提供类似的循环。

Well, if you want to interact directly with packets, then you need to use a DatagramSocket instead of the regular Socket and ServerSocket.

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 using send().

If you aren't interested in the individual packets like you indicated in your question, then you will want to use Socket and ServerSocket. The first step to communicating between the two involves code that will look similar to the following:

//Server
// this call will block until the client tries to connect to the server
Socket cientConn = new ServerSocket(8878).accept();
// now you can use the connection's input and output streams to send data

/******************/

// Client
Socket serverConn = new Socket(addressOfServer, 8878);
// now you can use the connections input and output streams

After you get connections set up, you will have basically 2 read/write loops. One on the client, and one on the server.

while(true) [
    // check for data from an input stream
    ...
    // respond with message back
}

You will need a similar loop for the client and the server.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文