问题
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
回答1:
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.
来源:https://stackoverflow.com/questions/3659318/to-read-the-packet-of-bytes-on-clientclient-socket-from-serverserversocket-u