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.

Reply via email to