473,406 Members | 2,371 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,406 software developers and data experts.

Can get my client to read the data when receiving them over Asynchronous TCP in C#

Hi everyone,

I'm trying to make an application who sends querys to a tcp server who will then send an answer containing "rows" of commas separated values as an answer.

I'm pretty new to tcp programming, and it seems that i'm stuck here, because I can't read the answer when it comes. And since i don't know how many rows i will get (it can be arround 1000 sometimes) i can put my buffer too high neither...

here is the code I use...

Expand|Select|Wrap|Line Numbers
  1.  
  2. private void WaitForData()
  3. {
  4.             if (pfnCallBack == null)
  5.                 pfnCallBack = new AsyncCallback(OnDataReceived);
  6.             // now start to listen for any data...
  7.             m_asynResult = client.BeginReceive(m_DataBuffer, 0, m_DataBuffer.Length, SocketFlags.None, pfnCallBack, null);
  8.         }
  9.  
  10.         public void OnDataReceived(IAsyncResult asyn)
  11.         {
  12.             //end receive...
  13.             int iRx = 0;
  14.             iRx = client.EndReceive(asyn);
  15.             char[] chars = new char[iRx + 1];
  16.             System.Text.Decoder d = System.Text.Encoding.UTF8.GetDecoder();
  17.             int charLen = d.GetChars(m_DataBuffer, 0, iRx, chars, 0);
  18.             System.String szData = new System.String(chars);
  19.             if (isNewData)
  20.             {
  21.                 isNewData = false;
  22.                 receivedData = szData;
  23.             }
  24.             else
  25.             {
  26.                 receivedData = receivedData + szData;
  27.             }
  28.                 WaitForData();
  29.         }
  30.  
  31.  
  32.         // Fonction sendMessage
  33.         // -- Sert a envoyé un message à l'hôte de la connection client
  34.         // -- Reçoit en parametre un message de type string
  35.         // -- Envoie un erreur à la console si il y a un problème
  36.         // -- Sends message to the server 
  37.         public void sendMessage(String message)
  38.         {
  39.             try
  40.             {
  41.                 message = message + "\r\n";
  42.                 byte[] byData = System.Text.Encoding.ASCII.GetBytes(message);
  43.                 isNewData = true;
  44.                 client.Send(byData);
  45.                 WaitForData();
  46.             }
  47.             catch (SocketException se)
  48.             {
  49.                 MessageBox.Show(se.Message);
  50.             }
  51.         }
  52.  
  53.         public string getMessage()
  54.         {
  55.             string retour;
  56.             if (receivedData != "")
  57.             {
  58.                 retour = receivedData;
  59.             }
  60.             else
  61.             {
  62.                 retour = "Chaîne vide!";
  63.             }
  64.             return retour;
  65.         }
  66.  
the above code comes from tcpSocket class it is triggered by an action button from another form here...

Expand|Select|Wrap|Line Numbers
  1.  
  2. unClient.sendMessage(message);
  3. reponse = unClient.getMessage();
  4.  
it seems that reponse is filled too fast for the answer to come from the tcp server...

anyone got an idea how to resolve this problem? any mechanism that can wait till all the data is received before to put it in the reponse variable?

Thanx
Aug 30 '08 #1
1 1243
JosAH
11,448 Expert 8TB
You should have posted this question in the .NET forum where C# questions are
answered. This is the C and C++ forum.

kind regards,

Jos (moderator)

ps. I'll move this thread over to the correct forum for you
Aug 30 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Mike Dole | last post by:
I'm working on a client - server application based on the 'How to Sockets Server and How to Sockets Client' code from the Visual Basic ..NET Resource Kit. Since I want to be able to send 'big...
15
by: Michael Rybak | last post by:
hi, everyone. I'm writing a 2-players game that should support network mode. I'm now testing it on 1 PC since I don't have 2. I directly use sockets, and both client and server do...
3
by: Matthew King | last post by:
Hi all I've written a asynchronous socket client class, but i've found that in order to consume it I have to use events, and cannot simply for example SocketClient client = new...
2
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...
7
by: Ole | last post by:
Hi, I'm going to develop a socket communication between an instrument (running CE 5.0 with Compact Fraework V2) and a PC. As the instrument should only connect to one PC at a time I believe that...
5
by: raghubr | last post by:
Hi all, Can any one pls guide me through..I need to transfer the file from server to client and client to server using sockets in an Asynchronous mode so this file transfer doesn't hinder the...
0
by: 85ssp | last post by:
I am creating a small server client program that is meant for up to 70 connections from 70 different computers on a network. Everything in the program functions correctly except when testing...
1
by: JohnDenver62 | last post by:
Hi all, I am trying to write a client/server application for TCP or UDP actually at this point the protocol is not important, I am trying to sort out the logic / flow diagram of the problem. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
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.