ezzat wrote:

Niklas Therning wrote:
ezzat wrote:
hello all,
            in my company, a Socket-server application made by the
apache-mina package. i have a task to connect to this server and get some information first I
trying to logging in then if logging is true, I try to send request and
get
the server response I tried to make this , but I have many problems using
the ordinary java socket or channelScoket
the problem, I can't get all the response message of the server.
can any body help me to get out of this task how can I starting
I think you will have to provide some more information. What does the server do? Is it a standard protocol like HTTP, FTP, etc, or a proprietary? If proprietary, please give us some details of what the protocol looks like. Also, what does your client code look like?
/Niklas


its a TCP socket.
the server collect some data and send to clients.
I'm a client , so I send loggin message th server, if true, I logged in..
after looging I start to send a request and receive a response from the
server.
I used the socket class of java and SocketChannel, but with same problem..
the 2 are blocking  sockets, i.e I can read all the data in the response
message of the server.
this code

            mySocket = new Socket(getHost(), getPort());
os = new DataOutputStream(mySocket.getOutputStream());
            os.write(getAuthenticationDateString());
            os.flush();
            Thread.sleep(500);          // must sleep a while to get the
server response
            DataInputStream is = new
DataInputStream(mySocket.getInputStream()); int nn = is.available(); // the available function get number of bytes that can
be read without blocking
            if(nn > 0)
            {
                byte[] bb = new byte[nn];
                is.read(bb);
                returnString = decryptReturnedResponse(bb);
            }
            System.out.println(returnString);
this code get some of the reseived data.
the question is how can I get all the response message that server sent
?????????


waiting your reply

You cannot assume that is.read(bb) will read a complete message from the server. You will need some means to determine whether you have read a complete message or not by inspecting the bytes you receive. The OS and network could fragment the data as it sees fit. This means that even if the server writes a big chunk of let's say 1000 bytes you might have to call is.read() multiple times to read all those 1000 bytes. If the server closes the connection after the response has been sent you should use that to determine when the complete message has been read.

Also, should you really use DataInputStream? Since you are using read(byte[]) you could use mySocket.getInputStream() directly if I understand things correctly.

HTH
/Niklas

Reply via email to