1

我是新的。我是 java 开发人员(新人),目前正在从事 BSE 项目,我面临着从服务器(服务器套接字)读取客户端(客户端套接字)上的字节包的问题。如果你能帮助我,请帮助我。

提前致谢

4

1 回答 1

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.

于 2010-09-07T14:14:15.137 回答