ezzat wrote:

Niklas Therning wrote:
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


ok, I did what you said
I made loop until the is.avialble() reuturn -1
its working excellent in the first time, but when I try to make the second
request & response, I get an Exception said that  " Socket write Error"..
means that the server end the connection.

to get clear of my procedure
the procedure looks like this
1. send logging message
2. if response is not equal "ALLOW" , exit
    else
         send a request.
         receive a response
         parse the response message.
3.end


Ok ??

So the server closes the connection after it has sent the response? In that case you should call is.read() until it returns -1. Don't use is.available():

byte[] bb = new byte[1024];
int offset = 0;
int nn = 0;
while ((nn = is.read(bb, offset, bb.length - offset) != -1) {
 offset += nn;
 if (offset == bb.length) {
   // Resize bb
   byte[] temp = new byte[bb.length * 2];
   System.arrayCopy(bb, 0, temp, 0, bb.length);
   bb = temp;
 }
}

// bytes 0 -> offset in bb now contains the received data.

This will read from the socket until end of stream and copy all bytes to bb. bb will be resized if needed. I haven't tested this code myself so there may be some bugs in there but I think you get the picture. If possible you could use IoUtils in commons-io which have a very nice method which replaces the code above with:

byte[] bb = IoUtils.toByteArray(is);

See http://commons.apache.org/io/apidocs/org/apache/commons/io/IOUtils.html

HTH
/Niklas

Reply via email to