473,503 Members | 2,126 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

.net -> win32 stream oddity

Hi folks,

I am sending a string over a socket from a C# app to a native C++ app.
I embed the unicode symbol for the pound sign in the string being
sent. When I read data from the socket, I find that a 0xC2 has been
added into the input stream right before the pound symbol.

C# fragment :

TcpClient skt = null;
StreamWriter wrt = null;
try {
skt = local.AcceptTcpClient();
skt.NoDelay = true; //no buffering!
NetworkStream ns = skt.GetStream();

string result = "STATUS|\u00A37.89 (GBP)|
sess"
wrt = new StreamWriter(ns);
//results are EOL delimited
wrt.WriteLine(result);
wrt.Flush();
} catch (Exception e) {
Console.WriteLine(e.ToString());
} finally {
if (rdr != null) rdr.Dispose();
if (skt != null) skt.Close();
}

C++ fragment :

char inbuf[1024];

iResult = recv(sock, inbuf, 1024, 0);
if (iResult != SOCKET_ERROR && iResult 0) {
Log::Debug(__WFILE__, __LINE__, L"recvBuf has %d bytes\n", iResult);
if ( iResult 1024 ) {
Log::Debug(__WFILE__, __LINE__, L"Received way too many bytes in
response - ignoring\n");
*netStatus = 6;
} else {
--- inbuf[iResult] = '\0';
::MultiByteToWideChar(CP_ACP, 0, inbuf, iResult+1, outStr, 1024);
Log::Debug(__WFILE__, __LINE__, L"Login response : %s\n", inbuf);
Log::Debug(__WFILE__, __LINE__, L"Login response : %s\n", outStr);
*outBytes = wcslen(outStr);S
*netStatus = 0;
Log::Debug(__WFILE__, __LINE__, L"<--SendServiceMessage received :
%s\n", outStr);
}

}

The socket communicatiopn works fine, but the problem is on the C++
end, if I check inbuf right after the socket read (i.e. at the --->),
it has an extra character in it.

Sent from C# : STATUS|£7.89 (GBP)|sess

Received by C++ : STATUS|£7.89 (GBP)|sess

Does anyone have any suggestions or theroies regarding the extra
character? I am stumped.
Jun 27 '08 #1
2 1191
On Mon, 19 May 2008 06:55:53 -0700, leet-drewb <an*************@gmail.com>
wrote:
[...]
Does anyone have any suggestions or theroies regarding the extra
character? I am stumped.
It's very difficult to say without a complete code sample.

However, the first thing I'd try is changing "CP_ACP" to "CP_UTF8" in your
call to MultiByteToWideChar().

Also, you should really reconsider disabling Nagle (TcpClient.NoDelay).
It's unlikely that data coalescing is causing any significant performance
issue, and disabling it can actually cause worse performance in general,
depending on what your network i/o looks like.

Pete
Jun 27 '08 #2
leet-drewb wrote:
I am sending a string over a socket from a C# app to a native C++ app.
Sockets deal with binary data. Strings are text data. To convert from
text to binary you need an encoder. If you don't supply an encoder, a
default will be chosen.
I embed the unicode symbol for the pound sign in the string being
The "pound sign" is ambiguous; to a lot of Americans, it means #.
I believe from your source you mean '£', unicode 0x00A3.
sent. When I read data from the socket, I find that a 0xC2 has been
added into the input stream right before the pound symbol.
This is because of the encoding. The default encoding you are using is
doing this. When you use a StreamWriter constructor overload that
doesn't specify an encoding, you end up with Encoding.Default, which
ought to be the system default ANSI codepage.

What you need to do is explain what you expect to see on the wire. What
encoding is the C++ side expecting? A specific ANSI codepage? UTF-8?
UTF-16? ASCII (which doesn't include 0x00A3)? Both sides need to agree
on the encoding.
C# fragment :
wrt = new StreamWriter(ns);
Here, you are not specifying the encoding. To get the desired result,
you need to use the right encoding, and use the StreamWriter(Stream,
Encoding) overload, and be sure the receiving side is expecting the same
encoding.
C++ fragment :
--- inbuf[iResult] = '\0';
::MultiByteToWideChar(CP_ACP, 0, inbuf, iResult+1, outStr, 1024);
Is the default ANSI code page on the C++ system the same as on the
sending system? You shouldn't rely on this default for an over the wire
protocol, IMO.
Log::Debug(__WFILE__, __LINE__, L"Login response : %s\n", inbuf);
Log::Debug(__WFILE__, __LINE__, L"Login response : %s\n", outStr);
*outBytes = wcslen(outStr);S
*netStatus = 0;
Log::Debug(__WFILE__, __LINE__, L"<--SendServiceMessage received :
%s\n", outStr);
}

}

The socket communicatiopn works fine, but the problem is on the C++
end, if I check inbuf right after the socket read (i.e. at the --->),
it has an extra character in it.
Yes, that's before you've decoded (Multibyte ANSI->Unicode) the binary
data.

-- Barry

--
http://barrkel.blogspot.com/
Jun 27 '08 #3

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

Similar topics

0
1418
by: Matthew Mueller | last post by:
Hello, I had searched the web but not found any info on cross compiling (other than the fact that distutils doesn't support it.) So, since I managed to get it working, I thought I'd share my...
12
2110
by: Tim Rowe | last post by:
If I do from __future__ import division then eval(1/2) gives me 0.5 as expected. But if I do print input("enter a sum: ") and enter 1/2 as the sum I get 0 as if I hadn't done the import. I thought...
3
2739
by: Nicolas Lehuen | last post by:
Hi, Is it me, or does anyone else get significantly better pystone results under Cygwin versus the standard Win32 build ? CYGWIN 1.5.6 + python 2.3.3-1 : $ time python...
0
2719
by: F. GEIGER | last post by:
py2exe and datetime -> No module named datetime I've begun to use the stdlib module datetime instead of my home brewn classes. Since then a py2exe app doesn't run anymore: Traceback (most...
1
4804
by: Bob Smith | last post by:
so is there any alternative ( standard only ) to using in_avail()? I need to poll with given intervals the input stream, and if there is data to be read I want to read it. thank you /B
43
2677
by: michael.f.ellis | last post by:
The following script puzzles me. It creates two nested lists that compare identically. After identical element assignments, the lists are different. In one case, a single element is replaced. In...
10
1331
by: Vivien Parlat | last post by:
Hello, I have a little question about operator >between integers: does anyone know why, when I write: "1 >x", the returned value is 1 for all values multiple of 32 and 0 else (this does not...
1
3740
by: Ezmeralda | last post by:
Hello, I need to an TCP/IP Interface for communication with an embedded device. Since I have two different design ideas in mind, I am wondering whether you could give me some hints to decide:...
2
3999
by: Ilkinho | last post by:
I`ve got class Stock and operator >> defined for it: istream &operator>>(istream &is, Stock& e){ is>>e.stockName>>e.producer>>e.date.m_day; return is; } Also class BunchOfStocks which...
0
7287
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
7353
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...
1
7011
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
5596
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,...
1
5023
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3180
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1521
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
747
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
401
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.