Reg to Unicode and MBCS problem in socket programing
-
Hai friends, i'm working with client server model socket programing using VC++. it has MFC SOCKET in client side and WIN32 SOCKET in server console application. i'm having proplem with receiving the strings in server side. i displayed part of code here Client Side Coding: UpdateData(1); CSocket client; client.Create (); CString sendstr; sendstr="Hello"; client.Connect ("127.0.0.1",2500); int len=sendstr.GetLength (); int sent= client.Send(LPCTSTR(sendstr),len); UpdateData(0); Server side coding: char recvbuf[32]="" ; int bytesRecv = recv( server, recvbuf, 32, 0 ); printf( "Bytes Recv: %d\n", bytesRecv ); recvbuf[bytesRecv]=NULL; printf("%S",recvbuf); if i run the code i get 5 in bytesRecv correctly.. but in recvbuf buffer three letters only available... like "hel" please help me l'm waiting for u... :^)
-
Hai friends, i'm working with client server model socket programing using VC++. it has MFC SOCKET in client side and WIN32 SOCKET in server console application. i'm having proplem with receiving the strings in server side. i displayed part of code here Client Side Coding: UpdateData(1); CSocket client; client.Create (); CString sendstr; sendstr="Hello"; client.Connect ("127.0.0.1",2500); int len=sendstr.GetLength (); int sent= client.Send(LPCTSTR(sendstr),len); UpdateData(0); Server side coding: char recvbuf[32]="" ; int bytesRecv = recv( server, recvbuf, 32, 0 ); printf( "Bytes Recv: %d\n", bytesRecv ); recvbuf[bytesRecv]=NULL; printf("%S",recvbuf); if i run the code i get 5 in bytesRecv correctly.. but in recvbuf buffer three letters only available... like "hel" please help me l'm waiting for u... :^)
Your string is not UNICODE, why do you want to print it as such? :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
Hai friends, i'm working with client server model socket programing using VC++. it has MFC SOCKET in client side and WIN32 SOCKET in server console application. i'm having proplem with receiving the strings in server side. i displayed part of code here Client Side Coding: UpdateData(1); CSocket client; client.Create (); CString sendstr; sendstr="Hello"; client.Connect ("127.0.0.1",2500); int len=sendstr.GetLength (); int sent= client.Send(LPCTSTR(sendstr),len); UpdateData(0); Server side coding: char recvbuf[32]="" ; int bytesRecv = recv( server, recvbuf, 32, 0 ); printf( "Bytes Recv: %d\n", bytesRecv ); recvbuf[bytesRecv]=NULL; printf("%S",recvbuf); if i run the code i get 5 in bytesRecv correctly.. but in recvbuf buffer three letters only available... like "hel" please help me l'm waiting for u... :^)
Are you using TCP ? If yes, put the flag TCP_NODELAY. It's a classic error issue, in fact many people are confused by TCP. It's a simple example but with the TCP_NODELAY flag you should be able to receive your 5 bytes at once. [^]
-
Hai friends, i'm working with client server model socket programing using VC++. it has MFC SOCKET in client side and WIN32 SOCKET in server console application. i'm having proplem with receiving the strings in server side. i displayed part of code here Client Side Coding: UpdateData(1); CSocket client; client.Create (); CString sendstr; sendstr="Hello"; client.Connect ("127.0.0.1",2500); int len=sendstr.GetLength (); int sent= client.Send(LPCTSTR(sendstr),len); UpdateData(0); Server side coding: char recvbuf[32]="" ; int bytesRecv = recv( server, recvbuf, 32, 0 ); printf( "Bytes Recv: %d\n", bytesRecv ); recvbuf[bytesRecv]=NULL; printf("%S",recvbuf); if i run the code i get 5 in bytesRecv correctly.. but in recvbuf buffer three letters only available... like "hel" please help me l'm waiting for u... :^)
Your client side project is UNICODE. Either select MBCS in project settings, or
**CStringA** sendstr; sendstr="Hello"; client.Connect ("127.0.0.1",2500); int len=sendstr.GetLength (); int sent= client.Send( **LPCSTR**(sendstr),len);
CString is macro, which translates to CStringA or CStringW. You can assign either Unicode or ANSI string, CStringW/CStringA will take care of that.