Re: How do I stop a Winsock from buffering characters?



Hi Paul,

Byte the way I am executing this code on the same PC. I did try changing
the client to sent it like this:

try
{
Stream s = client.GetStream();

StreamReader sr = new StreamReader(s);
StreamWriter sw = new StreamWriter(s);
sw.AutoFlush = true;
//Console.WriteLine(sr.ReadLine());
Int32 var1 = 0x01;
Int32 var2 = 0x02;
string myString =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZzyxwvutsrqponmlkjihgfedcbaZYXWVUTSRQPONMLKJIH";
System.Text.ASCIIEncoding encoding = new
System.Text.ASCIIEncoding();
byte[] bCommand = encoding.GetBytes(myString);

int iCounter = 1;
while (true)
{
/*
Console.Write("Name: ");
name = Console.ReadLine();
sw.WriteLine(name);
if (name == "") break;
Console.WriteLine(sr.ReadLine());
*/
if (iCounter != 0)
{
iCounter--;
sw.Write(bCommand);
sw.Flush();
}
// sw.Write(var2);
// sw.WriteLine("test string");
// Console.WriteLine(sr.ReadLine());
}
s.Close();
}
finally
{
// code in finally block is guranteed
// to execute irrespective of
// whether any exception occurs or does
// not occur in the try block
client.Close();
}

Doing this causes the MessageService() to never get called no matter howmany
characters I add to the string. I just wan to send one character at a time
from the client code and have the server see it right away. How do I do it?
Thanks in advance

The

--
Don


"Paul G. Tobey [eMVP]" wrote:

A little too much code to read in-line...

I don't see the server reads that you're talking about as having dropped the
first character of the client send. You've sent a bunch of server-side
code, (at least it *looks* like it might be server-side code), but not the
code in question. At a guess, you're probably sending Unicode from the
Windows CE side and receive ASCII on the server side. Since most of the
Unicode characters will be 0x00xy where xy makes up the ASCII character,
that's probably the source of the problem. If you convert everything to
bytes, and convert strings to ASCII before converting them to bytes, if
ASCII is what you want to send, I think you'll find your problem.

Paul T.

"Don" <Don@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:865A7119-F69B-45F3-9C1D-045506172955@xxxxxxxxxxxxxxxx
HI

I have two apps one is a Winsock Client as in the following:

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace SocketTestClient
{


class EmployeeTCPClient
{
public static void Main(string[] args)
{
//string name = (args.Length < 1) ? Dns.GetHostName() :
args[0];
string name = Dns.GetHostName();
try
{
IPAddress[] addrs = Dns.Resolve(name).AddressList;
foreach (IPAddress addr in addrs)
Console.WriteLine("{0}/{1}", name, addr);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}

TcpClient client = null;

do
{

try
{
//TcpClient client = new TcpClient(args[0], 2055);
client = new TcpClient(name, 2055);
}
catch (SocketException e)
{
Console.WriteLine(e.Message);
Thread.Sleep(500); // Try again in 0.5s
}
} while (client == null);

try
{
Stream s = client.GetStream();
StreamReader sr = new StreamReader(s);
StreamWriter sw = new StreamWriter(s);

sw.AutoFlush = true;
//Console.WriteLine(sr.ReadLine());
Int32 var1 = 0x01;
Int32 var2 = 0x02;
string myString =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZzyxwvutsrqponmlkjihgfedcbaZYXWVUTSRQPONMLKJIH";
int iCounter = 1;
while (true)
{
/*
Console.Write("Name: ");
name = Console.ReadLine();
sw.WriteLine(name);
if (name == "") break;
Console.WriteLine(sr.ReadLine());
*/
if (iCounter != 0)
{
iCounter--;
sw.Write(myString);
sw.Flush();
}
// sw.Write(var2);
// sw.WriteLine("test string");
// Console.WriteLine(sr.ReadLine());
}
s.Close();
}
finally
{
// code in finally block is guranteed
// to execute irrespective of
// whether any exception occurs or does
// not occur in the try block
client.Close();
}
}
}
}

and one is a Winsock Server as in the following


lType = sr.ReadInt32();
lSize = sr.ReadInt32();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
msgFn(lType, sr);
}
}

public int Close()
{
// Shut down message service - belt and braces
bRunMessageService = false;
thMessageThread.Abort();

// Close streams
s.Close();
#if LOG
Console.WriteLine("Disconnected: {0}", soc.RemoteEndPoint);
#endif
soc.Close();
return 0;
}
}
}


class TestTCPServer
{
static TCPServer listener;
static Messages.Messages msgList;

public static void Main()
{
msgList = new Messages.Messages();
listener = new TCPServer(new
Messages.Messages.MessageFn(msgList.myMessageFn));
}


}
}

using System;
using System.IO;

namespace Messages
{
class Messages
{
public delegate int MessageFn(long MessageType, BinaryReader sr);


public int myMessageFn(long MessageType, BinaryReader sr)
{
switch (MessageType)
{
case 1:
double arg1 = sr.ReadDouble();
double arg2 = sr.ReadDouble();
break;
default:
break;
}

return 0;
}
}
}


When I send the string myString from the client to the server there are
two
problems

1. The first character gets dropped
2. The string doesn't get there unless I add another character to
myString.

This suggests to me that there is some buffering going on. Is there away
where I can send one character from the client and have the Server see it
right away with no buffering?

--
Don



.



Relevant Pages

  • Re: How do I stop a Winsock from buffering characters?
    ... it's applied at the OS level to the socket. ... Stream s = client.GetStream; ... from the client code and have the server see it right away. ... first character of the client send. ...
    (microsoft.public.windowsce.embedded)
  • Re: How do I stop a Winsock from buffering characters?
    ... from the client code and have the server see it right away. ... I don't see the server reads that you're talking about as having dropped ... first character of the client send. ... public delegate int MessageFn(long MessageType, ...
    (microsoft.public.windowsce.embedded)
  • Re: file transfer with sockets
    ... confirmation from client for each chunk before sending the next? ... to server, server interprets command, executes it, then simply returns ... files (hence the reading in chunks). ... sending the bytes over the stream. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: How do I stop a Winsock from buffering characters?
    ... So you have been able to send one character at a ... I have set the property NoDelay on both my clent and server app and I ... Stream s = client.GetStream; ... from the client code and have the server see it right away. ...
    (microsoft.public.windowsce.embedded)
  • Re: How do I stop a Winsock from buffering characters?
    ... The client is just doing this to send: ... Stream s = client.GetStream; ... I just wan to send one character ... from the client code and have the server see it right away. ...
    (microsoft.public.windowsce.embedded)

Loading