简体   繁体   中英

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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