Re: How do I stop a Winsock from buffering characters?
- From: "Paul G. Tobey [eMVP]" <p space tobey no spam AT no instrument no spam DOT com>
- Date: Tue, 3 Jul 2007 13:10:38 -0700
"Right away" isn't going to happen easily. That's a function of TCP/IP.
There's a lot of data wrapped around any packet that's sent over the network
and wrapping a single byte is a serious waste of bandwidth. The TCP stack
will automatically stick bytes together into larger groups, if they are sent
close to one another in time, attempting to avoid exactly what you're trying
to do. You can turn that off, but, if this is part of your real design,
it's a bad design.
If you feel like this has to be done for testing, you want to set a socket
option to turn off the Nagle algorithm. That should be documented in
whatever the socket option setting call for managed code is. It might be
something like TCP_NODELAY, which is what it is in native code/WinSock.
Paul T.
"Don" <Don@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:0B5F1EA6-459C-4EFE-B556-800FB22FA7F2@xxxxxxxxxxxxxxxx
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
.
- Follow-Ups:
- References:
- How do I stop a Winsock from buffering characters?
- From: Don
- Re: How do I stop a Winsock from buffering characters?
- From: Paul G. Tobey [eMVP]
- Re: How do I stop a Winsock from buffering characters?
- From: Don
- How do I stop a Winsock from buffering characters?
- Prev by Date: Re: How do I stop a Winsock from buffering characters?
- Next by Date: Re: How do I stop a Winsock from buffering characters?
- Previous by thread: Re: How do I stop a Winsock from buffering characters?
- Next by thread: Re: How do I stop a Winsock from buffering characters?
- Index(es):
Relevant Pages
|
Loading