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

后端 未结 1 821
迷失自我
迷失自我 2021-01-16 17:11

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 s

相关标签:
1条回答
  • 2021-01-16 17:53

    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.

    0 讨论(0)
提交回复
热议问题