Re: Tranfering unicod charcters in Socket programming!
- From: raghupise@xxxxxxxxx
- Date: Tue, 4 Dec 2007 05:17:59 -0800 (PST)
Hi Volodymyr,
1) As you said I have to use std::wstring for unicode characters .But
how I can achieve this.
which header file i need to use?
2)What about recv() function problem.
As I explained in my earlier post i was getting junk unicode
charcters,If i send simple charcter.
Looking forward to your answers.
Kind Regards,
Raghavendra L Pise.
On Dec 4, 5:56 pm, "Volodymyr Shcherbyna"
<v_scherb...@xxxxxxxxxxxxxxx> wrote:
Hello again ;)
As I see further in your code, you have more problems. You recieve the data
in unicode, but then you pass the resulted UNICODE string to Parse method,
which is defined as:
void CParser::Parse(string strRecvbuf, CMessageIn* mi)
And this is wrong, because this method takes as an input parameter a
std::string object, which is a wrapper over ANSI string. You cannot simply
take a UNICODE string and convert it into corresponding std::string object.
You should use std::wstring for unicode characters. Also, you will need to
use appropriate unicode functions for strings manipulations.
--
Volodymyr<raghup...@xxxxxxxxx> wrote in message
news:34aba993-c626-4a8b-bca6-720576948d29@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Dear Volodymyr,
1) As you said, I have done modification as given below.
recvbuf[bytesRecv / sizeof(wchar_t)] = '\0';
2)You asked me about what is the return value after recv() function.
I am getting amazing output for this.
Earlier i was trying with these double byte charcter "????".
But this is that, Now I am sending simple string its coming as "??????
????????? " these charters.
I dont know why its coming double byte charcter,If i am sending simple
string called "unicode1".
I believe some where double byte chrcters are going to store after
executing recv() function.
3) I have another problem,
In this below line of Listen fucntion.
parser.Parse((char*)strRecvbuf.c_str(), &mi); //Rlp has done to fix
unicode
server is not going to strcuk for this above line.
Parser() defintaion s follows.
ofcouse we use strting data type for that reason its going to strcuk.
But i need your help to fix this problem also.
void CParser::Parse(string strRecvbuf, CMessageIn* mi)
{
bool bArgHasDelim = 0; //special extraction of arguments for
tagstring, whichs argument can contain c_strArgDelim
mi->SetCmd(op_error);
string strCmd("");
RetrieveCmd(strRecvbuf, &strCmd, mi);
InterpretCmd(strCmd, mi);
RetrieveArgs(strRecvbuf, mi);
}
int CParser::RetrieveCmd(string strRecvbuf, string* strCmd,
CMessageIn* mi)
{
//
//Retrieve command
//
int nCmdStartPos = 0, nCmdEndPos;
int nMessEndPos = strRecvbuf.find(c_strEndOfInMess);
//Check if the message contains a command i.e starts with "/" or ">"
if (strRecvbuf.substr(0,1) == c_strStartOfCmd ||
strRecvbuf.substr(0,1) == c_strStartOfCmd2){
nCmdStartPos++;
mi->SetShallReturnMessage(true);
//"//" or ">>" gives silent mode (no answers)
if (strRecvbuf.substr(1,1) == c_strStartOfCmd ||
strRecvbuf.substr(1,1) == c_strStartOfCmd2){
nCmdStartPos++;
mi->SetShallReturnMessage(false);}
//Find start of arguments and extract the string inbetween
nCmdEndPos = strRecvbuf.find(c_strStartOfArgs);
if (nCmdEndPos != string::npos) {
*strCmd = strRecvbuf.substr(nCmdStartPos, nCmdEndPos -
nCmdStartPos);
//Otherwise the command MUST be terminated by c_strEndOfInMess} else {
*strCmd = strRecvbuf.substr(nCmdStartPos, nMessEndPos -
nCmdStartPos);}
} else {
throw DocException("RetrieveCmd failed. No '/' in beginning of
command.");
}
return 0;
}
int CParser::RetrieveArgs(string strRecvbuf, CMessageIn* mi)
{
//
//Retrieve arguments
//
string strExtracted("");
string strArgs("");
int nArgStartPos, nArgEndPos;
nArgStartPos = strRecvbuf.find(c_strStartOfArgs);
nArgEndPos = strRecvbuf.length() - c_strEndOfInMess.length() - 1; //
do not include the "\r\n" at the end
//Check if a '(' was found in the string
if(nArgStartPos != string::npos){
strArgs = strRecvbuf.substr(nArgStartPos, nArgEndPos - nArgStartPos
+ 1);
//RemoveBackSlash(&strArgs);
//Check if there is an argument with delimiters
if(mi->GetArgHasDelim()){
//extract one argument
strExtracted = strArgs.substr(1, strArgs.size() - 2);
mi->PushArg(strExtracted);
CLogger::Log("argument with delimiters", strExtracted);}
//Check if there is only an empty paranthesis
else if (removeSigns(strArgs, c_strSpace) != c_strEmptyParenthesis)
{
//start retrieving several args between the paranthesis
int nSpacePos = 0, nArgDelimPos = 0, nLastArgDelimPos = 0;
bool bHasMoreArgs = true;
nArgEndPos = strArgs.length() - 1;
while (bHasMoreArgs){
//find the next delimiter
nArgDelimPos = GetNextDelimPos(nLastArgDelimPos, strArgs);
//check if there is only one argument left
if (nArgDelimPos == string::npos){
bHasMoreArgs = false;
nArgDelimPos = nArgEndPos;}
//extract one argument
strExtracted = strArgs.substr(nLastArgDelimPos + 1, nArgDelimPos -
nLastArgDelimPos - 1);
//Trim the arg from spaces
strExtracted = removeEdgeSigns(strExtracted, c_strSpace);
if (strExtracted.size() <= 0){
throw DocException("RetrieveArgs failed. Arg no " + toString(mi->GetNoOfArgs() + 1) + " has no length.");
}
//Remove the backslashes from the argument
//RemoveBackSlash(&strExtracted);
//save strExtacted in tne messagein vector
mi->PushArg(strExtracted);
nLastArgDelimPos = nArgDelimPos;
}
}
}
return 0;
}
Kind Regards,
Raghavendra L Pise.
On Dec 4, 12:42 pm, "Volodymyr Shcherbyna"
<v_scherb...@xxxxxxxxxxxxxxx> wrote:
Hello,
I took a brief look at the code. And I have one simple question,
- if you send a string, let's say "sometest string" do you recieve exactly
the same string in your recieve and listen methods? (After the recv method
returns)?
If so, then the problem with sending unicode text is solved. Also, I
noticed, that you incorrectly truncate the unicode strings when recieving,
for example,
bytesRecv = recv( ConnectSocket, (char*)recvbuf, 32, 0 );
[...]
recvbuf[bytesRecv] = '\0';
Here you truncate the unicode buffer at incorrect position, because
bytesRecv - is the number of bytes. And recvbuf is a multibyte array, so,
since you want to truncate on a specified character, and not the BYTE! you
should devide bytesRecv / sizeof(wchar_t) and only then trancate, so:
recvbuf[bytesRecv / sizeof(wchar_t)] = '\0';
Would be correct. Also, why do you allow to write exatly 32 bytes in this
(and other) lines. If this your protocol specification?
bytesRecv = recv( ConnectSocket, (char*)recvbuf, 32, 0 );
The recvbuf is 64 characters long, it means that it's 128 bytes, so you
should write
bytesRecv = recv( ConnectSocket, (char*)recvbuf, 128, 0 ); or BETTER:
bytesRecv = recv( ConnectSocket, (char*)recvbuf, sizeof(recvbuf), 0 );
HTH
--
Volodymyr<raghup...@xxxxxxxxx> wrote in message
news:23ff0665-d73e-40b7-9916-8a40a960d05b@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Dear Volodymyr Shcherbyna,
I have replaced string with wchar_t data tyep in my project.
The problem in my project is not giving proper response for below line
in listen function..
"if (strRecvbuf.substr(strRecvbuf.length() -
c_strEndOfInMess.length(), c_strEndOfInMess.length()) ==
c_strEndOfInMess)."
I have enlcosed my below code, that i have made changes in my code.
Really this could be design level change in our project,But I was
strcuk these issues.So i need your help to fix this problem
And If any wrong with my code snippet you are most welcome to recrtify
these errors.
You can ask any queation if you have?
Rightnow I am facing problem in Lsiten function.
1) Send Function.
int CConnection::Send(wchar_t *mess)//., CMessageIn* mi)
{
//mess += c_strEndOfOutMess;
STRCAT(mess,c_strEndOfOutMess.c_str()); //Rlp Has done to fix
Unicode problem...
int bytesSent = 0;
//bytesSent = send(ConnectSocket, mess.c_str(), mess.size(),0);
bytesSent = send(ConnectSocket, (const char *)mess,(wcslen(mess )*
sizeof(wchar_t)), 0 );
return bytesSent;
}
2) Recv function:
string CConnection::Recv(int nSecTimeout)
{
int bytesRecv;
string strRecvbuf("");
//char recvbuf[64] = ""; //Rlp Has sone to fix unicode problem
wchar_t recvbuf[64] = L" ";
bool bStopListen = false;
ResetTimer();
while(!bStopListen){
bytesRecv = SOCKET_ERROR;
while( bytesRecv == SOCKET_ERROR ) {
//bytesRecv = recv( ConnectSocket, recvbuf, 32,
0 );
bytesRecv = recv( ConnectSocket, (char*)recvbuf, 32, 0 );
if (bytesRecv == 0 ) {
CLogger::Log("bytesRecv == 0");
return strRecvbuf;
}
if (bytesRecv == WSAECONNRESET) {
CLogger::Log("WSAECONNRESET");
return strRecvbuf;
}
if (CheckTimeOut(nSecTimeout)) {
CLogger::Log("Timeout");
return strRecvbuf;
}
}
recvbuf[bytesRecv] = '\0';
STRCAT(strRecvbuf.c_str(), recvbuf);
//strRecvbuf.append(recvbuf);
if(strRecvbuf.length() < c_strEndOfInMess.length()){
bStopListen = false;
} else if (strRecvbuf.substr(strRecvbuf.length() -
c_strEndOfInMess.length(), c_strEndOfInMess.length()) ==
c_strEndOfInMess){
bStopListen = true;
}
}
return strRecvbuf.substr(0, strRecvbuf.length() -
c_strEndOfInMess.length());
}
3) Listen function
APIERR CConnection::Listen()
{
APIERR err = noErr;
//Receive variables
int bytesRecv = SOCKET_ERROR;
//Parser variables
CMessageIn mi;
CParser parser;
char sendbuf[256] = "";
wchar_t recvbuf[256] = L" ";
char peekbuf[1] = "";
string strRecvbuf("");
bool bStopListen = 0;
//Initiate the message
mi.Erase();
//No document has yet been opened
m_bDocIsOpen = false;
//Receive data
while(!bStopListen){
bytesRecv = SOCKET_ERROR;
ResetTimer();
while( bytesRecv == SOCKET_ERROR ) {
bytesRecv = recv( ConnectSocket,(char*)recvbuf, 64, 0 );
if ( bytesRecv == 0 || bytesRecv == WSAECONNRESET ||
CheckTimeOut()) {
return CloseListen();
}
}
//recvbuf[bytesRecv] = L'\0'; Rlp has doen to fix unicode problem.
recvbuf[bytesRecv] =L'\0';
STRCAT(strRecvbuf.c_str(),recvbuf);
//strRecvbuf.append(recvbuf);
//strRecvbuf.append(recvbuf); Rlp has doen to fix unicode problem.
//.//Check if the message is ended wiht \r\n
if
...
read more >>
.
- Follow-Ups:
- Re: Tranfering unicod charcters in Socket programming!
- From: Chris Becke
- Re: Tranfering unicod charcters in Socket programming!
- From: Volodymyr Shcherbyna
- Re: Tranfering unicod charcters in Socket programming!
- References:
- Re: Tranfering unicod charcters in Socket programming!
- From: raghupise
- Re: Tranfering unicod charcters in Socket programming!
- From: Volodymyr Shcherbyna
- Re: Tranfering unicod charcters in Socket programming!
- From: raghupise
- Re: Tranfering unicod charcters in Socket programming!
- From: Volodymyr Shcherbyna
- Re: Tranfering unicod charcters in Socket programming!
- Prev by Date: Re: Tranfering unicod charcters in Socket programming!
- Next by Date: Re: Tranfering unicod charcters in Socket programming!
- Previous by thread: Re: Tranfering unicod charcters in Socket programming!
- Next by thread: Re: Tranfering unicod charcters in Socket programming!
- Index(es):
Relevant Pages
|
Loading