简体   繁体   English

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

[英]to read the packet of bytes on client(client Socket) from server(ServerSocket) using java

ima new . ima new。 ima java developer(fresher) and currently im working on BSE project and im facing problem to read the packet of bytes on the client(client socket) from the server(server socket). ima java开发人员(fresher),目前正在BSE项目上工作,并且面临从服务器(服务器套接字)读取客户端(客户端套接字)上的字节包的问题。 if u can help me then please help me. 如果你能帮助我,那么请帮助我。

Thanks in advance 提前致谢

Well, if you want to interact directly with packets, then you need to use a DatagramSocket instead of the regular Socket and ServerSocket . 好吧,如果您想直接与数据包进行交互,则需要使用DatagramSocket而不是常规的SocketServerSocket

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() . 基本思想是,客户端或服务器在等待其伙伴使用send()发送数据包时将阻止recieve()调用。

If you aren't interested in the individual packets like you indicated in your question, then you will want to use Socket and ServerSocket . 如果您对问题中指示的单个数据包不感兴趣,则可以使用SocketServerSocket 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. 建立连接后,基本上将有2个读/写循环。 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. 客户端和服务器将需要类似的循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM