Sockets End of Send

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

When I do a Socket.Send(byte[]) from a client, how do I know that I've
reached the end of this data on the server? I've tried sending the length(4
bytes) at the beginning of the byte[] but I can't seem to read this correctly
(I always get a negative number). I'm basically trying to take an Image from
a picture box, I save it to a MemoryStream and then send it off, but i need
to know how much to read before i construct the buffer on the server side.
 
this is what i do in my class


public string Sendit (string data)
{
try
{
byte[] byteData = Encoding.ASCII.GetBytes(data);
client.BeginSend(byteData, 0, byteData.Length, 0, new
AsyncCallback(SendCallback), client);
sendDone.WaitOne();
receiveDone.WaitOne(10000,false);
return response;
}
catch (Exception e)
{
return response;
}
}


private void SendCallback(IAsyncResult ar)
{
try
{
client = (Socket) ar.AsyncState;
int bytesSent = client.EndSend(ar);
sendDone.Set();

}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
 
where are you calling Sendit() from?

Michael Evanchik said:
this is what i do in my class


public string Sendit (string data)
{
try
{
byte[] byteData = Encoding.ASCII.GetBytes(data);
client.BeginSend(byteData, 0, byteData.Length, 0, new
AsyncCallback(SendCallback), client);
sendDone.WaitOne();
receiveDone.WaitOne(10000,false);
return response;
}
catch (Exception e)
{
return response;
}
}


private void SendCallback(IAsyncResult ar)
{
try
{
client = (Socket) ar.AsyncState;
int bytesSent = client.EndSend(ar);
sendDone.Set();

}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}


Jaret Brower said:
When I do a Socket.Send(byte[]) from a client, how do I know that I've
reached the end of this data on the server? I've tried sending the length(4
bytes) at the beginning of the byte[] but I can't seem to read this correctly
(I always get a negative number). I'm basically trying to take an Image from
a picture box, I save it to a MemoryStream and then send it off, but i need
to know how much to read before i construct the buffer on the server side.
 
Hi,

I think we can call it from anywhere you need to send data.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
Is there a different way to send serialized data other then Socket.Send() or
remoting? I was able to get it to work just sending the bitmaps bytes, but
would definately rather serialize.

Tim Haughton said:
Is it .Net on both ends?

Why not serialize the bitmap? Let .Net's deserialization worry about it.

--
Regards,

Tim Haughton

Agitek
http://agitek.co.uk
http://blogitek.com/timhaughton

Jaret Brower said:
When I do a Socket.Send(byte[]) from a client, how do I know that I've
reached the end of this data on the server? I've tried sending the length(4
bytes) at the beginning of the byte[] but I can't seem to read this correctly
(I always get a negative number). I'm basically trying to take an Image from
a picture box, I save it to a MemoryStream and then send it off, but i need
to know how much to read before i construct the buffer on the server side.
 
Jaret Brower said:
Is there a different way to send serialized data other then Socket.Send() or
remoting? I was able to get it to work just sending the bitmaps bytes, but
would definately rather serialize.

It will work using the send method.

As you've noticed, you need to be able to let the receiving party know when
they've received enough data. You can do this by sending a special
termination character over the wire after completion, or by sending the size
first. Now, since you say you're getting a negative number when you send the
size, it would tend to suggest that you're doing something a little funky
somewhere. Have you tried a simple test solution, creating a server and
client, sending a simple custom type over the wire?

If you can distill it down to a simple solution (I'm not too bright) either
post it or email it to me and I'll have a look at it. Depending on when you
send it, I may be a while replying as I'm off on holiday soon for a week.

--
Regards,

Tim Haughton

Agitek
http://agitek.co.uk
http://blogitek.com/timhaughton
 
Back
Top