473,698 Members | 2,337 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

asynchronous socket problem

I am using the C# asynchronous socket functionality for a server, and
it appears to work fine if I just receive data and echo it back to the
client. The problem occurs when I try to handle sending data
independent of a receive. The send happens just fine, but when I
receive the next set of data from the client (in a size, size, data
set of packets), I lose however many packets of data I sent. For
example, a normal client send would include 3 separate packets of
data (p1-4 bytes, p2-4 bytes, p3-however many bytes are represented by
the size in p1). The server then echoes back the same data.

After sending one packet of data independent of receiving data, the
client sends the same 3 packets of data. However, the receive
callback is only entered twice.

It seems to be related to the beginReceive function, but I can't
figure out how. Prior to calling the sendTest, I have started a
beginReceive. Is this ended when I send data? I've tried adding a
beginReceive after the send, but this doesn't appear to help. Any
assistance would be greatly appreciated!

public void send(SocketData data)
{
SocketState ss = (SocketState)co nnectionTable[
data.getSocketH andle()];
send(ss, data.getXmlStri ng());
clearSocketStat e(ref ss);
}

/// <summary>
/// This function creates a callback to send data.
/// </summary>
/// <param name="handler"> The socket state from which to get
the
/// socket to send data.</param>
/// <param name="data">The data to send</param>
public void send(SocketStat e handler, string data)
{
// Convert the string data to byte data using big endian
encoding.
byte[] byteData = Encoding.ASCII. GetBytes(data);

byte[] sizeBytes =
BitConverter.Ge tBytes((int)byt eData.Length);
byte[] compressedBytes =
BitConverter.Ge tBytes((int)byt eData.Length);
if(byteData.Len gth > compressionSize )
{
// compress the data
//recalculate the compressed byte length
compressedBytes =
BitConverter.Ge tBytes((int)byt eData.Length);
}

byte[] sendBytes = new byte[byteData.Length +
sizeBytes.Lengt h +
compressedBytes .Length];

// Add bytes for size and compressed size to the message
to be sent
sizeBytes.CopyT o(sendBytes, 0);
compressedBytes .CopyTo(sendByt es, sizeBytes.Lengt h);
byteData.CopyTo (sendBytes, sizeBytes.Lengt h +
compressedBytes .Length);
handler.buffer = sendBytes;
printBytes(send Bytes.Length, ref handler);
// Begin sending the data to the remote device.
handler.theSock et.Send(sendByt es);
//TODO: remove this after testing
Console.WriteLi ne("Sent {0} bytes to client.",
sendBytes.Lengt h);

}
public void sendTest()
{
Console.WriteLi ne("sendTest entry point");

try
{

while(connectio nTable.Count == 0)
{
// just spin here until there is a connection
}

IEnumerator ie = connectionTable .Keys.GetEnumer ator();
ie.MoveNext();
SocketState ss =
(SocketState)co nnectionTable[ie.Current];
IntPtr si = ss.theSocket.Ha ndle;
SocketData data = new SocketData(si," testSend Data");
send(data);
data.setXmlStri ng("testSend Data1");
// send(data);
data.setXmlStri ng("testSend Data2");
// send(data);
}
catch (Exception e)
{
Console.WriteLi ne("Exception occurred: {0}, stack
trace {1}",
e.Message, e.StackTrace);
}

Console.WriteLi ne("Done");
}
Nov 15 '05 #1
0 1446

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

Similar topics

6
2932
by: Zunbeltz Izaola | last post by:
Hi, I have the following problem. I'm developing a GUI program (wxPython). This program has to comunicate (TCP) whit other program that controls a laboratory machine to do a measurement. I have a dialog box, wiht two buttoms "Start measurement" and "Stop". "Start" executes a function that do the measurement in the following way.
3
2815
by: Corne Oosthuizen | last post by:
I'm writing a Telnet Server application using Asynchronous sockets. I spawn a listener thread to handel incomming connections and create a separate client socket for each new connection. I then set the new client socket to BeginReceive(). My problem: When two client socket connections send data
0
1319
by: Darren Thomas | last post by:
Dear all, I have written a TCP server as a windows service that accepts connections using the System.Net.Sockets.Socket class. I use the various asynchronous methods in the socket class. I'm having a problem at the moment when I shut the service down. When I stop the service I attempt to shutdown and close the socket. But when the service is started again and the Socket.Bind statement is encountered, an error occurs telling me that it is...
1
6212
by: Niels Johansen | last post by:
Hello, When using the asynchronous read method in the BufferedStream class, , it seems to me that it blocks like the normal synchronous read method. Why is it so? Why does the BufferedStream.BeginRead() not behave similar to the NetworkStream.BeginRead() ?? The following short program illustrates my problem.: using System;
4
3817
by: taskswap | last post by:
I have a legacy application written in C that I'm trying to convert to C#. It processes a very large amount of data from many clients (actually, upstream servers - this is a mux) simultaneously. I've read through what must be dozens of ways to do socket communication in C#, and it seems they all devolve into three basic options - Socket.Select, IOCP through a native interface, and Asynchronous callbacks. I'm fine using Asynchronous...
2
6874
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...
4
3601
by: Engineerik | last post by:
I am trying to create a socket server which will listen for connections from multiple clients and call subroutines in a Fortran DLL and pass the results back to the client. The asynchronous socket client and asynchronous socket server example code provided in the .NET framework developers guide is a great start but I have not dealt with sockets before and I am struggling with something. From what I can tell the sample server code ...
1
6677
by: Alper AKCAYOZ | last post by:
Hello, I have developped asynchronous socket communication with blocking Socket commands (accept, connect, send, receive) by using threads on Windows .NET Forms. It is working properly. Now I want to code the similar program with Asynchronous Socket commands of .NET using Managed C++ on Windows .NET Forms. My problem is with delegates. I have to use "static" methods as parameter in delegate constructor like below:...
6
6996
by: Pat B | last post by:
Hi, I'm writing my own implementation of the Gnutella P2P protocol using C#. I have implemented it using BeginReceive and EndReceive calls so as not to block when waiting for data from the supernode. Everything I have written works fine sending and receiving uncompressed data. But now I want to implement compression using the deflate algorithm as the Gnutella protocol accepts: Accept-Encoding: deflate Content-Encoding: deflate in the...
2
3428
by: Nicolas Le Gland | last post by:
Hello everyone here. This is my first post in this newsgroup, I hope I won't be to much off-topic. Feel free to redirect me to any better group. I am getting strange timing issues when failing to asynchronously connect sockets on closed or filtered ports, but I'm quite unsure if this is a PHP issue or my misunderstanding, as it seems that socket streams only wrap around <sys/socket.h>.
0
8608
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,...
0
9161
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
9029
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8897
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,...
0
5860
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
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3050
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
2
2332
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2006
bsmnconsultancy
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...

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.