473,394 Members | 1,787 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Sockets End of Send

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.
Nov 17 '05 #1
6 2516
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" wrote:
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.

Nov 17 '05 #2
where are you calling Sendit() from?

"Michael Evanchik" wrote:
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" wrote:
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.

Nov 17 '05 #3
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" <br******@nospam.nospam> wrote in message
news:7C**********************************@microsof t.com...
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.

Nov 17 '05 #4
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."

Nov 17 '05 #5
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" wrote:
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" <br******@nospam.nospam> wrote in message
news:7C**********************************@microsof t.com...
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.


Nov 17 '05 #6
"Jaret Brower" <br******@nospam.nospam> wrote in message
news:7B**********************************@microsof t.com...
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
Nov 17 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Dr.Kadzija | last post by:
i have a client-server application. client and server should communicate via tcp sockets. ok, so i use Sockets, PrintWriter and BufferedReader. the problem is that: both client and server will send...
1
by: Tim Black | last post by:
My application requires sending a large piece (~2MB) of data to several devices on a network via TCP sockets. I have experimented with different methods for doing this and this has raised some...
1
by: Dmitry Akselrod | last post by:
Hello everyone, I have a vb.net application that wraps the TCPListener object in a class. The server connects to the local interface and establishes itself on port 9900. It then polls for...
6
by: Laxmikant Rashinkar | last post by:
Is there any way to use a C# socket in promiscuous mode? Any sample code that shows how this is done? any assistance is much appreciated! thanks LK
0
by: Stuart Norris | last post by:
Dear Group, I am having a problem setting SocketOptionName.SendTimeout on a client TCPIP application using the sockets in .NET. From the on-line help it is possible to set a...
7
by: Bill English | last post by:
How do I send an object from one computer to another? -- I am a 14 year old C# developer, I am completely self taught, so please don't get mad if I ask a stupid question. Thanks.
4
by: BadOmen | last post by:
Hi, What is the different between 'System.Net.Sockets.Socket' and 'System.Net.Sockets.TcpClient'? When do I use System.Net.Sockets.TcpClient and System.Net.Sockets.Socket?? Yours, Jonas
1
by: verge | last post by:
hello everyone! how's it going? like everyone in here im in need of some help and good friendship along the way...take a look at this: //MODIFIED SO IT DEALS WITH WINDOWS FTP USING ACTIVE...
6
by: buc | last post by:
Why does VB.NET UDP sockets send data on random ports?. If I set a simple socket up to transmit a UDP packet on a port, look at the packet with a sniffer, the actual packets source port and...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.