使用java从服务器(ServerSocket)读取客户端(客户端Socket)上的字节数据包 [英] to read the packet of bytes on client(client Socket) from server(ServerSocket) using java

查看:604
本文介绍了使用java从服务器(ServerSocket)读取客户端(客户端Socket)上的字节数据包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新的。
我是一个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.

提前感谢

推荐答案

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

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.

基本思想是客户端或服务器将阻止 recieve()调用,同时等待其合作伙伴使用 send()

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

如果您对您的问题中指出的个别数据包不感兴趣, Socket ServerSocket 。这两个代码之间的沟通的第一步涉及到类似于下面的代码:

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

你将基本上有2个读/写循环。一个在客户端,一个在服务器上。

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
}


$ b b

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

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

这篇关于使用java从服务器(ServerSocket)读取客户端(客户端Socket)上的字节数据包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆