Socket.Available != 0, Socket.Read --> Exception

T

Tomasz Naumowicz

Hello Everybody,

I have the following problem with Socket:
Socket.Available reports data in the buffer (e.g. 1849 bytes) but I can't
read it because Socket.Read throws an exception
The exception ist being thrown because the connection is already closed. How
could I read the data from the buffer?

background information:
I open multiple connections to my Windows 2003 Web Server (simple HTTP GET).

pseudocode:
while (true) {
foreach open socket {
if should read
read
if should write
write
}

if new connection needed
create new connection
}

The IIS sends the data and closes the connection imediatelly. Sometimes my
application is not ready to "read" because it's doing something else at the
moment.
Shouldn't I be able to access the buffer even if the connection is closed by
the server?

Thanks!
tomek
 
G

Guest

Your socket may be set up differently, but here is the basic flow you should
have:

//IP socketcreated
int port = 11000;

IPHostEntry hostEntry =
Dns.Resolve("www.mysite.com");
IPEndPoint endPoint= new
IPEndPoint(hostEntry.AddressList[0], port);
Socket socket = new Socket(endPoint.Address.AddressFamily,
SocketType.Stream,
ProtocolType.Tcp);
try
{
socket.Connect(lep);
}
//NOTE: This is not the best catch
catch (Exception e)
{
//Would normally throw up the stack here
//Can also flag to avoid the rest of the code
}


Once the socket is set up, you will do one of the following:

//SEND
string messageString = "{Assume a request here?}";
byte[] message = Encoding.ASCII.GetBytes(messageString);

//This will block until all is sent
int sent = socket.Send(message, 0,
message.Length, SocketFlags.None);



//RECEIVE

//This is one way
byte[] bytes = new byte[1024];


while(socket.Avaialable > 0)
{
socket.Receive(bytes, 0, socket.Available
, SocketFlags.None);

//Do something with buffer here
}


//Or, if the messages are known to be short
int socketAvailable = socket.Avaialable;
byte[] bytes = new byte[socketAvailable];

socket.Receive(bytes, 0, socket.Available
, SocketFlags.None);

The question now is what you are doing specifically (code sample)?


---

Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

***************************
Think Outside the Box!
***************************
 
T

Tomasz Naumowicz

Your socket may be set up differently, but here is the basic flow you
should
have: (cut, cut, cut)
The question now is what you are doing specifically (code sample)?

Hi,
I need to keep a certain request rate (requests/second), that's why I'm
using a loop to maintain my sockets.
With every single socket I follow the basic flow. My problem is that
sometimes I'm not able to read all data from the buffer
while(socket.Avaialable > 0)
{
socket.Receive(bytes, 0, socket.Available
, SocketFlags.None);

because sometimes socket.Receive is throwing an exception.

I need to access the buffer even if the connection is closed:
socket.Available != 0 && socket.Connected = false

socket.Receive throws an exception because the connection is closed...

any ideas?
tomasz naumowicz
 

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