473,386 Members | 1,799 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,386 software developers and data experts.

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)connectionTable[
data.getSocketHandle()];
send(ss, data.getXmlString());
clearSocketState(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(SocketState handler, string data)
{
// Convert the string data to byte data using big endian
encoding.
byte[] byteData = Encoding.ASCII.GetBytes(data);

byte[] sizeBytes =
BitConverter.GetBytes((int)byteData.Length);
byte[] compressedBytes =
BitConverter.GetBytes((int)byteData.Length);
if(byteData.Length > compressionSize)
{
// compress the data
//recalculate the compressed byte length
compressedBytes =
BitConverter.GetBytes((int)byteData.Length);
}

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

// Add bytes for size and compressed size to the message
to be sent
sizeBytes.CopyTo(sendBytes, 0);
compressedBytes.CopyTo(sendBytes, sizeBytes.Length);
byteData.CopyTo(sendBytes, sizeBytes.Length +
compressedBytes.Length);
handler.buffer = sendBytes;
printBytes(sendBytes.Length, ref handler);
// Begin sending the data to the remote device.
handler.theSocket.Send(sendBytes);
//TODO: remove this after testing
Console.WriteLine("Sent {0} bytes to client.",
sendBytes.Length);

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

try
{

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

IEnumerator ie = connectionTable.Keys.GetEnumerator();
ie.MoveNext();
SocketState ss =
(SocketState)connectionTable[ie.Current];
IntPtr si = ss.theSocket.Handle;
SocketData data = new SocketData(si,"testSend Data");
send(data);
data.setXmlString("testSend Data1");
// send(data);
data.setXmlString("testSend Data2");
// send(data);
}
catch (Exception e)
{
Console.WriteLine("Exception occurred: {0}, stack
trace {1}",
e.Message, e.StackTrace);
}

Console.WriteLine("Done");
}
Nov 15 '05 #1
0 1433

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

Similar topics

6
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...
3
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...
0
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...
1
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...
4
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. ...
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...
4
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...
1
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...
6
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...
2
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...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: 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: 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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...

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.