You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@mina.apache.org by ezzat <ez...@hotmail.com> on 2007/10/24 09:35:22 UTC

socket problem

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
-- 
View this message in context: http://www.nabble.com/socket-problem-tf4682668s16868.html#a13380605
Sent from the Apache MINA Support Forum mailing list archive at Nabble.com.


Re: socket problem

Posted by ezzat <ez...@hotmail.com>.


Niklas Therning wrote:
> 
> ezzat wrote:
>> dear HTH
>> /Niklas
>>
>>
>> I have tried all this solutions before, 
>> the first request and response is fine. but when I tried to get the
>> second
>> response, I faced with the Exception "socket write error"
>> I do looping like this
>>
>> a=0;
>> while( a <10)
>> {
>>    send request
>>    reseive response
>>    a++;
>> }
>> this exception  exst only in the second request-response 
>>
>> plz help
>>
>>   
> The write error probably means that the server has closes down the 
> connection after sending the response. If that's the case you will have 
> to reconnect and log in again before sending the second request.
> 
> HTH
> /Niklas
> 
> 


many thanks Mr HTH/Niklas

bu I must login once in the whole seesion
the procedure looks like this 

try
{
   connect with the server
  if connect and logged in
   {
       a=0;
       while( a<100)
      {
            send request
            receive response // using looping until reading all the response
message
            parse response
           a++;
      }
   }

in the first iteration  of the loop , ervery thing goes well. but in the 
second I got the exception
but when I didn't use the looping in reading, I get no exception but can't
read the whole response message

thnaks
}







-- 
View this message in context: http://www.nabble.com/socket-problem-tf4682668s16868.html#a13383585
Sent from the Apache MINA Support Forum mailing list archive at Nabble.com.


Re: socket problem

Posted by Niklas Therning <ni...@trillian.se>.
ezzat wrote:
> dear HTH
> /Niklas
>
>
> I have tried all this solutions before, 
> the first request and response is fine. but when I tried to get the second
> response, I faced with the Exception "socket write error"
> I do looping like this
>
> a=0;
> while( a <10)
> {
>    send request
>    reseive response
>    a++;
> }
> this exception  exst only in the second request-response 
>
> plz help
>
>   
The write error probably means that the server has closes down the 
connection after sending the response. If that's the case you will have 
to reconnect and log in again before sending the second request.

HTH
/Niklas


Re: socket problem

Posted by ezzat <ez...@hotmail.com>.
dear HTH
/Niklas


I have tried all this solutions before, 
the first request and response is fine. but when I tried to get the second
response, I faced with the Exception "socket write error"
I do looping like this

a=0;
while( a <10)
{
   send request
   reseive response
   a++;
}
this exception  exst only in the second request-response 

plz help

-- 
View this message in context: http://www.nabble.com/socket-problem-tf4682668s16868.html#a13382553
Sent from the Apache MINA Support Forum mailing list archive at Nabble.com.


Re: socket problem

Posted by Niklas Therning <ni...@trillian.se>.
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


Re: socket problem

Posted by ezzat <ez...@hotmail.com>.


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 ??
-- 
View this message in context: http://www.nabble.com/socket-problem-tf4682668s16868.html#a13381645
Sent from the Apache MINA Support Forum mailing list archive at Nabble.com.


Re: socket problem

Posted by Niklas Therning <ni...@trillian.se>.
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


Re: socket problem

Posted by ezzat <ez...@hotmail.com>.


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
-- 
View this message in context: http://www.nabble.com/socket-problem-tf4682668s16868.html#a13381282
Sent from the Apache MINA Support Forum mailing list archive at Nabble.com.


Re: socket problem

Posted by Niklas Therning <ni...@trillian.se>.
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