Read Method

L

Lafayette

i´m reading some data from a socket object and when i call the read method
it returns 0 bytes to my byte array.

my code:
private void processRequest(){
byte[] buffer;

int numBytes = this._userSocket.Available;

if(numBytes > maxHeaderBytes){

numBytes = maxHeaderBytes;

}

buffer = new byte[numBytes];

try{

this._userSocket.Receive(buffer,0,numBytes,0); // HERE IS RETURNED AN EMPTY
ARRAY!!! AND Avaliable Property says that exists for exa,ple 800 bytes do be
read.

this._pacoteHttp = this._encoder.GetString(buffer);

if(this._pacoteHttp != String.Empty){

this.extractHeader();

}else{

throw new RequisicaoVaziaException("Empty request(??) " +
this._userSocket.Available + " bytes to be read",this._userSocket);

}

}

catch(SocketException ex){

throw new ConexaoEncerradaException("Connection closed" + ex.ToString());

}

}
 
J

Jon Skeet [C# MVP]

Lafayette said:
i´m reading some data from a socket object and when i call the read
method it returns 0 bytes to my byte array.

Are you sure that numBytes is non-zero when you call the method? Can
you produce a short but complete example which demonstrates the
problem?
 
L

Lafayette

thankz for the reply Jon,

I have an HTML form like this:

<html>
....
....
<form action=http://localhost:1010/SEND_MESSAGE>
<input type="text" name="MSG">
<input type="submit" name="sbmt">
</form>
.....
</html>

the data that i´m trying to read is comming from that html form.

my app listens on por 1010

in my main thread i have this code:

private void listener(){

this._listener = new
Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
this._listener.Bind(new IPEndPoint(IPAddress.Any,this._porta));
this._listener.Listen(this._porta);
while(true){
allDone.Reset();
this._listener.BeginAccept(new
AsyncCallback(this.onConnect),this._listener);
allDone.WaitOne();
}

}//listener

in onConnect event another thread is startd, the socket is passed as
contructor parameter of a class named Request
The Request class shoud read the http packet, split the querystring , post
data and so on.

when the processRequest Method is called, the avaliable property says that
are 700bytes do be read but when the read method runs, nothing is returned.

when it happens i get lost because the connection is closed by the client
(internet explorer) and i can´t send anything back to my user

thankz in advance

Lafayette





Lafayette said:
i´m reading some data from a socket object and when i call the read
method it returns 0 bytes to my byte array.

Are you sure that numBytes is non-zero when you call the method? Can
you produce a short but complete example which demonstrates the
problem?
 
J

Jared Parsons [MSFT]

What is the return value of Socket.Receive()?

This could be a problem with converting the byte[] to a string. Have you
used a debugger to step through this code and see what the array looks like
after the Receive() call?

--
Jared Parsons [MSFT]
(e-mail address removed)
This posting is provided "AS IS" with no warranties, and confers no rights.
OR if you wish to include a script sample in your post please add "Use of
included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm"
 
J

Jon Skeet [C# MVP]

Lafayette said:
thankz for the reply Jon,

I have an HTML form like this:

<snip>

As I said before, it would help if you gave a *complete* program which
demonstrates the problem. If you could write a program I could just
compile and run with no changes, along with a full HTML page to load in
IE, it'll be much easier to find out what's wrong.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top