I'm not a new programmer, but this one's got me stymied; hopefully it's a fairly trivial problem.
I'm using a socket connection to receive communication from a server. Normally, the entire message is received before the program moves on with its next instructions. However, at times when the processor is particularly busy the program seems to progress with its instructions without having received the entire communication. In other words, it seems to clean out the buffer, which due to the computer's business is holding only the first packet at this point, then move on without waiting for the buffer to fill with the next packet.
Is there a way to stop the program until it has been told that the entire communication has been received? Or a way to iteratively receive the packets until the program is told the communication is finished?
Here's my code:
IPHostEntry IPAdd = Dns.Resolve("se rvername.com");
//Configure the socket
IPEndPoint IPEP = new IPEndPoint(IPAd d.AddressList[0], 2628);
Socket s = new Socket(IPEP.Add ress.AddressFam ily, SocketType.Stre am, ProtocolType.Tc p);
//Construct the message
string str = "Message"
//Declare the receive buffer. Maximum of 50,000 characters.
Byte[] Receiver = new Byte[50000];
//Connect the socket
s.Connect(IPEP) ;
//Receive greeting. Is discarded.
Int32 bytes = s.Receive(Recei ver, Receiver.Length , 0);
//Declare the send buffer. Initialize it with the query, converted into bytes.
Byte[] Sender = System.Text.Enc oding.ASCII.Get Bytes(str);
//Send the query.
s.Send(Sender, Sender.Length, 0);
//Receive the response into the byte array
bytes = s.Receive(Recei ver, Receiver.Length , 0);
//Translate the byte array into ASCII text
strResponse = System.Text.Enc oding.ASCII.Get String(Receiver , 0, bytes);
....Furthur code
Thanks for your help!
*************** *************** *************** *************** **********
Sent via Fuzzy Software @ http://www.fuzzysoftware.com/
Comprehensive, categorised, searchable collection of links to ASP & ASP.NET resources... 2 4797
<M Sutherland (ta******@hotma il.com)> wrote: I'm not a new programmer, but this one's got me stymied; hopefully it's a fairly trivial problem.
I'm using a socket connection to receive communication from a server. Normally, the entire message is received before the program moves on with its next instructions. However, at times when the processor is particularly busy the program seems to progress with its instructions without having received the entire communication. In other words, it seems to clean out the buffer, which due to the computer's business is holding only the first packet at this point, then move onwithout waiting for the buffer to fill with the next packet.
Is there a way to stop the program until it has been told that the entire communication has been received? Or a way to iteratively receive the packets until the program is told the communication is finished?
Just call Receive repeatedly until you've got all the data you
expected. Of course, if you don't know how much data to expect, you're
basically stuffed - you could use asynchronous reads to keep reading
until you've not seen anything new for a second or so, but
fundamentally sockets don't provide anything saying "the communication
is finished" until the socket is closed.
--
Jon Skeet - <sk***@pobox.co m> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Hi,
How do you know the size of what you are receiving?
My guess is that you are blocking the program until one of two conditions
are met:
1- The buffer is full
2- The socket timeout and return with the number of bytes readed.
Solution:
1- Know in advance the number of bytes to expect
2- Read one byte at a time.
Cheers,
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"M Sutherland" <ta******@hotma il.com> wrote in message
news:e7******** ******@TK2MSFTN GP09.phx.gbl... I'm not a new programmer, but this one's got me stymied; hopefully it's a fairly trivial problem.
I'm using a socket connection to receive communication from a server. Normally, the entire message is received before the program moves on with its next instructions. However, at times when the processor is particularly busy the program seems to progress with its instructions without having received the entire communication. In other words, it seems to clean out the buffer, which due to the computer's business is holding only the first packet at this point, then move on without waiting for the buffer to fill with the next packet.
Is there a way to stop the program until it has been told that the entire communication has been received? Or a way to iteratively receive the packets until the program is told the communication is finished?
Here's my code:
IPHostEntry IPAdd = Dns.Resolve("se rvername.com"); //Configure the socket IPEndPoint IPEP = new IPEndPoint(IPAd d.AddressList[0], 2628); Socket s = new Socket(IPEP.Add ress.AddressFam ily, SocketType.Stre am, ProtocolType.Tc p); //Construct the message string str = "Message" //Declare the receive buffer. Maximum of 50,000 characters. Byte[] Receiver = new Byte[50000]; //Connect the socket s.Connect(IPEP) ; //Receive greeting. Is discarded. Int32 bytes = s.Receive(Recei ver, Receiver.Length , 0); //Declare the send buffer. Initialize it with the query, converted into bytes. Byte[] Sender = System.Text.Enc oding.ASCII.Get Bytes(str); //Send the query. s.Send(Sender, Sender.Length, 0); //Receive the response into the byte array bytes = s.Receive(Recei ver, Receiver.Length , 0); //Translate the byte array into ASCII text strResponse = System.Text.Enc oding.ASCII.Get String(Receiver , 0, bytes);
...Furthur code
Thanks for your help!
*************** *************** *************** *************** ********** Sent via Fuzzy Software @ http://www.fuzzysoftware.com/ Comprehensive, categorised, searchable collection of links to ASP & ASP.NET resources... This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Robert A. van Ginkel |
last post by:
Hello Fellow Developer,
I use the System.Net.Sockets to send/receive data (no
tcpclient/tcplistener), I made a receivethread in my wrapper, the
receivethread loops/sleeps while waiting for data and then fires a
datareceived event.
Within the waitingloop there is a timeout function, but I want the the
'last-time-socket-used' variable set when the socket is finished sending.
When I send by System.Net.Sockets.Socket.Send(buffer()) (<--this...
|
by: Andre |
last post by:
Hi,
I'm implementing an asynchron socket Receiving method and I have a
question about EndReceive.
Read()
{
Socket.BeginReceive(buf,0,buf.Length,0,new
AsyncCallback(ReadCallback),dp);
}
|
by: Marten Van Keer |
last post by:
Hi;
I have two applications A and B
*** Application B listens on a network stream with a callback function:
|
by: djc |
last post by:
I read a network programming book (based on framework 1.1) which indicated
that you should 'never' use the RecieveTimeout or the SendTimeout 'socket
options' on TCP sockets or you may loose data. I now see the
socket.RecieveTimeout 'property' in the visual studio 2005 help
documentation (framework 2.0) and it has example of it being used with TCP
socket. This propery is also listed as 'new in .net 2.0'.
1) is the socket.RecieveTimeout...
|
by: Tor Erik |
last post by:
Hi,
The reason is that my application does about 16 connects and data
transfers per second, to the same 16 remote hosts. After approx 200 secs
there are 4000 sockets waiting to be garbage collected by the OS. At
this point is seems that connect loops and starts using the same local
addresses it used 4000 connections ago, resulting in an "Address already
in use" exception.
A possible solution to this would be to keep the connection to...
| |
by: BuddyWork |
last post by:
Hello,
Could someone please explain why the Socket.Send is slow to send to the same
process it sending from. Eg. Process1 calls Socket.Send which sends to the
same IP address and port, the receiver is running within Process1.
If I move the receiver into Process2 then its fast.
Please can someone explain.
|
by: george585 |
last post by:
Hello!
I am new to network programming, and understand just basics. Using some sample code, and having read documentation, I managed to create a simple app in C# and VB.NET. The application is supposed to do the following:
monitor ALL INCOMING TCP traffic on the local computer, and save certain parts of it as files - not log files though, but actual files that are sent to the computer as part of http or ftp. Basically if a user browse a page...
|
by: Zytan |
last post by:
This may be the dumbest question of all time, but...
When I set the packet size, does it mean ALL packets are that size, no
matter what? Let's say the packet size is 8KB, and I send a 5 byte
"hello", will it cause 8KB of bandwidth, or 5 bytes (plus TCP/IP
packet header, as well, of course).
(Btw, I 'set' the packet size via Socket.BeginReceive(), in the "size"
field = "The number of bytes to receive." Which seems to imply the
answer...
|
by: kashifjawed |
last post by:
I'm developing a c# asynchronous socket application for tranfering file or large data from client to server and server to client as well in chunks.
Application sometimes transfer file from client to server and sometimes not,
because connection betwenn client and server closed Abrupt.
I cannot understand why connection between client and server is closed while i cannot closed the connection until a single block
tranfered to server.
...
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |