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

Net.Sockets.Socket.BegineReceive with 1 byte buffer losses data

I am trying to develop a simple telnet client in MS.NET

I have a small wrapper around the System.Net.Sockets.Socket class
which takes care of sending and receiving data and handing it off to a
protocol handler.

What I am finding is that if I set my receive buffer size to 1 byte
this code will 'lose' characters, yet when I set the buffer to more
than 1 byte all works fine.

The problem is, that since it seems that most telnet clients operate
in 'byte at a time' mode I really want the single byte buffer
processing to work.

Has anyone else experienced a problem similar to this, and if so, what
do you do to fix it ?
private static void ReceiveCallback(IAsyncResult ar)
{
System.Threading.Monitor.Enter(recvCS);
// Retrieve the psuedo this object.
Connection psuedoThis = (Connection) ar.AsyncState;
// and get stuff we need to do our work
Socket theSocket = psuedoThis.workSocket_;

if(!ReallyConnected(theSocket)) // seems to be a problem with
detecting disconnects
{
System.Environment.Exit(-1);
}

// Read data from the client socket
int bytesRead = theSocket.EndReceive(ar);
String trace = "Socket[" + theSocket.Handle.ToString() + "] read " +
bytesRead.ToString() + " bytes from " +
theSocket.RemoteEndPoint.ToString();
System.Diagnostics.Debug.WriteLine(trace);

// take a copy of the inbound data
byte[] copy = new byte[bytesRead];
for(int i = 0; i < bytesRead; i++)
copy[i] = psuedoThis.buffer_[i];

// hand it off the the protocol handler
foreach(byte b in copy)
{
byte[] reply = psuedoThis.protocol_.handleMsg(b);
// if the protocol handler returns a reply, this must be sent NOW !
if(reply != null)
{
psuedoThis.Send(reply);
System.Console.WriteLine("Sending Reply {0}",
psuedoThis.protocol_.humanReadable(reply));
psuedoThis.Wait();
System.Console.WriteLine("Sending completed");
}
}
// resume asynchronous operation on the socket
psuedoThis.shutdownResult_ = theSocket.BeginReceive(
psuedoThis.buffer_,
0,
BufferSize,
SocketFlags.None,
psuedoThis.asyncCallback(CBType.Receive),
psuedoThis );
System.Threading.Monitor.Exit(recvCS);
}
Nov 15 '05 #1
0 2066

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

Similar topics

14
by: jack | last post by:
At this link I have two c# projects, one is a client, the other is a server. Just point the ip address of the client at the server http://www.slip-angle.com/hosted/bug/ The server does...
3
by: Robert A. van Ginkel | last post by:
In news:OZ0W9RsdDHA.2432@TK2MSFTNGP10.phx.gbl... I ask the question how I can see if all the data is on the other side of the connection. I got as answer that I should use the blocking property. I...
0
by: Stuart Norris | last post by:
Dear Group, I am having a problem setting SocketOptionName.SendTimeout on a client TCPIP application using the sockets in .NET. From the on-line help it is possible to set a...
1
by: jm | last post by:
Easy probably, please read on. I know some of you have commented already about some of my socket question. I appreciate that. I have a Form1: static void Main() { Application.Run(new...
3
by: Raj | last post by:
I want to pass a C structure from a windows server to the C# client using the Sockets. Will there be requirements to cast the data types in the C# client?
3
by: Michael Maercker | last post by:
hi! i'm really not into networking at all and have now been asigned the task of porting a vb6-code into vb.net (compact framework, in this case) and the code uses the winsock-control. i quickly...
0
by: J008 | last post by:
Just looking for some insight as to why the callback "BeginReceiveFromCallback" is not being called in my "Receive" Subroutine below (when I call BeginReceiveFrom). I am trying to read data...
14
by: eliss.carmine | last post by:
I'm using TCP/IP to send a Bitmap object over Sockets. This is my first time using C# at all so I don't know if this is the "right" way to do it. I've already found out several times the way I was...
1
by: larspeter | last post by:
Hi all. I have a problem with TcpClient ... I am conneting to a server with TcpClient and returning the answer through a webservice. It actully all works fine. BUT if I make a lot of...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
0
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,...
0
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...

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.