473,657 Members | 2,572 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problems with data received thru socket

I am using the example [1] as the basis for an application I am
currently writing. The setup consists of two applications (client and
server) that talk to each other thru TCP.

I have no problems with sending information back and forth, but I have
problems _containing_ the data sent from the client to the server.

In the class I instantiate/declare a StringBuilder to collect the
data as it comes thru the socket

private StringBuilder _sb = new StringBuilder() ;

For each byte I read I append this byte to _sb, but somehow only
the initial byte is appended to _sb - all other bytes are somehow
lost. I also add all bytes to a listBox, so I have verified that the
data from the client is actually received by my application.

I have tried to display the data arriving (in this case the string
"test") with

System.String szData = new System.String(c hars);
MessageBox.Show ("szData:" + szData + "\n" + "somestring ");

The result is this:

szData:t
szData:e
szData:s
szData:t

Whereas I would expext:

szData:t
somestring
szData:e
somestring
szData:s
somestring
szData:t
somestring

So somehow the data is "disapearin g" for me.

Can you tell me why?
The relevant code on the server is this:

public void OnDataReceived( IAsyncResult asyn)
{
try
{
CSocketPacket theSockId = (CSocketPacket) asyn.AsyncState ;
//end receive...
int iRx = 0 ;
iRx = theSockId.thisS ocket.EndReceiv e (asyn);
char[] chars = new char[iRx + 1];
System.Text.Dec oder d = System.Text.Enc oding.UTF8.GetD ecoder();
int charLen = d.GetChars(theS ockId.dataBuffe r,0, iRx, chars, 0);
System.String szData = new System.String(c hars);
listBox1.Items. Add(DateTime.No w.ToString() + "\t" + szData);
_sb.Append(szDa ta); // this line is causing problems
WaitForData(m_s ocWorker );
}
catch (ObjectDisposed Exception )
{
System.Diagnost ics.Debugger.Lo g(0,"1","\nOnDa taReceived: Socket has been closed\n");
}
catch(SocketExc eption se)
{
MessageBox.Show (se.Message );
}
}

public void WaitForData(Sys tem.Net.Sockets .Socket soc)
{
try
{
if ( pfnWorkerCallBa ck == null )
{
pfnWorkerCallBa ck = new AsyncCallback (OnDataReceived );
}
CSocketPacket theSocPkt = new CSocketPacket ();
theSocPkt.thisS ocket = soc;
// now start to listen for any data...
soc .BeginReceive (theSocPkt.data Buffer ,0,theSocPkt.da taBuffer.Length ,SocketFlags.No ne,pfnWorkerCal lBack,theSocPkt );
}
catch(SocketExc eption se)
{
MessageBox.Show (se.Message );
}
}
thanks,
[1] http://www.developerfusion.com/show/3997

--
Jesper Stocholm
Copenhagen, Denmark
Nov 15 '05 #1
0 1288

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

Similar topics

5
11697
by: mscirri | last post by:
The code below is what I am using to asynchronously get data from a PocketPC device. The data comes in fine in blocks of 1024 bytes but even when I send no data from the PocketPC constant blocks of 1024 with all values set to Null arrive. Other than examine a block of 1024 to see if the entire block is null, is there any other way to determine if , say a chat message "Hi Charlie" has been received completely?
7
3932
by: Crirus | last post by:
Hi all! I use a webClient for requesting data from a server of mine. Should I worry about long ammount of data sent by server in the client side? Or, another way, should I send some kind of a terminator sign in the data in order to realise in the client that all data arrived? -- Ceers, Crirus
0
3457
by: ZR | last post by:
I am writing two applications which needs to (among other things) communicate through network, so one of them is a client and the other one is a server. I have used asynchronous socket examples found at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconblockingclientsocketexample.asp as templates (they have to be asynchronous, because application must not freeze if connection fails). I have converted the...
9
3597
by: Macca | last post by:
Hi, I have a synchronous socket server which my app uses to read data from clients. To test this I have a simulated client that sends 100 byte packets. I have set up the socket server so that its buffer is bigger than this. I did this expecting the data to be read in one pass.
2
6870
by: Macca | last post by:
My app has an asynchronous socket server. It will have 20 clients connected to the server. Each client sends data every 500 millisecondsThe Connections once established will not be closed unless there is a problem with the connection. I need to know which client has sent the incoming data as each client has its own buffer on my "server" app. I am using the standard asynch socket code from MSDN to listen for connections and they...
0
4670
by: Macca | last post by:
Hi, I am writing an asychronous socket server to handle 20+ simulataneous connections. I have used the example in MSDN as a base. The code is shown at end of question. Each connection has a number of different types of data coming in. I have a databuffer for each type of data coming in.
9
2405
by: =?Utf-8?B?SG93YXJkIFNtaXRo?= | last post by:
I am using VC++ 6.0 (with SP5 installed). When using WinXP this is with SP2 installed. I am developing an instrumentation system comprising a set of networked PCs connected using TCP/IP TCP links. Communications on the Windows PCs use a class derived from CAsyncSocket(). I have written client and server versions of my software for use on the appropriate PCs. I am also using communicating with a legacy DOS based system, with the software...
7
4011
by: Guy Davidson | last post by:
Hi Folks, I'm having some issues with an small socket based server I'm writing, and I was hoping I could get some help. My code (attached below) us supposed to read an HTTP Post message coming from a power meter, parse it, and return a proper HTTP 200 Ok message. The problem is that the socket fails to send the entire message as one message, creating a fragmented message which the power meter then fails to read and accept.
1
9754
by: starter08 | last post by:
Hi, I have a C++ routine(client-side) which uploads an xml file to a web server by making a socket connection and sending all the post request through that socket. On the server side I have a cgi script which receives all the data and creates a file in the specified directory. If I am uploading only the file all works well, however I want to send data of other fields too (field1, field2 ..etc), this fails the post request and even the file is...
0
8425
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, 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...
0
8845
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8622
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 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...
0
7355
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6177
isladogs
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...
0
5647
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();...
0
4173
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...
0
4333
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2745
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 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.