繁体   English   中英

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

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

ima new。 ima java开发人员(fresher),目前正在BSE项目上工作,并且面临从服务器(服务器套接字)读取客户端(客户端套接字)上的字节包的问题。 如果你能帮助我,那么请帮助我。

提前致谢

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

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

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

如果您对问题中指示的单个数据包不感兴趣,则可以使用SocketServerSocket 两者之间进行通信的第一步涉及类似于以下内容的代码:

//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
}

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

暂无
暂无

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

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