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



Hi Paul

My reader and writers are of different types?
There server is like this:

s = new NetworkStream(soc);
sr = new BinaryReader(s);
sw = new BinaryWriter(s);

and the client is like this:

Stream s = client.GetStream();

StreamReader sr = new StreamReader(s);
StreamWriter sw = new StreamWriter(s);


Is this what you mean? Am I doing this wrong?



--
Don


"Paul G. Tobey [eMVP]" wrote:

Well, your readers and writers are of different types on the client and
server ends, no? That should be a flag for you. I don't, frankly, know
what will happen when you do that, especially if the desktop end is ASCII...

Paul T.

"Don" <Don@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:8FF734EE-8E59-44C2-A3A4-1FCBE708CB8F@xxxxxxxxxxxxxxxx
Hi Paul,

Sorry all my server code did not make it in the post here is is.


using System;
using System.Collections.Generic;
using System.Text;

using System.Threading;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Configuration;

using Messages;


namespace SocketTestServer
{
class TCPServer
{
static TcpListener listener;
Service Service1;

public TCPServer(Messages.Messages.MessageFn msgFn)
{
listener = new TcpListener(2055);
listener.Start();
#if LOG
Console.WriteLine("Server mounted, listening to port 2055");
#endif
Service1 = new Service(msgFn);
}

class Service
{
Socket soc;
Stream s;
BinaryReader sr;
BinaryWriter sw;
bool bRunMessageService;
Thread thMessageThread;
const int cBufferSize = 5;
char [] cBuffer;
Messages.Messages.MessageFn msgFn;

public Service(Messages.Messages.MessageFn msgFn)
{
this.msgFn = msgFn;

soc = listener.AcceptSocket();
//soc.SetSocketOption(SocketOptionLevel.Socket,
// SocketOptionName.ReceiveTimeout,10000);
#if LOG
Console.WriteLine("Connected: {0}", soc.RemoteEndPoint);
#endif
try
{
cBuffer = new char[cBufferSize];
s = new NetworkStream(soc);
sr = new BinaryReader(s);
sw = new BinaryWriter(s);
//sw.AutoFlush = true; // enable automatic flushing

// Start Message Service
bRunMessageService = true;
thMessageThread = new Thread(new
ThreadStart(MessageService));
thMessageThread.Start();

}
catch (Exception e)
{
#if LOG
Console.WriteLine(e.Message);
#endif
}
}

public void MessageService()
{
Int32 lType = 0;
Int32 lSize = 0;
string myString;
while (bRunMessageService)
{
//int iRvalue = sr.Read(cBuffer, 0, cBufferSize);
try
{
myString = sr.ReadString();
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));
}


}
}


I set a break point at the call to:
myString = sr.ReadString();

1. The fist character does not show up the rest do
2. Why do I have to send so many characters for if to call the function
MessageService()?

--
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();
.



Relevant Pages

  • 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?
    ... Stream s = client.GetStream; ... I just wan to send one character at a time ... 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 the ...
    (microsoft.public.windowsce.embedded)
  • Re: How do I stop a Winsock from buffering characters?
    ... Stream s = client.GetStream; ... 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 ... first character of the client send. ...
    (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)