mwcmp
asked on
Socket Programming (Client recieving invalid characters)
Hi, I am currently developing a application that involves socket programming. Currently developing the server application on visual studio 6.0, and client application on embedded visual 3.0.
I did a socket connection between the server & client. As the server sends back information back to the client, it displays invalid charcters after the actual data (Eg. Apple*^@*^, Orange #&$($%@)
I suppose its the size, and I have to set a null at the end of the string. Is that so, could anyone guide me along on ways that I could solve this problem. Thank you.
I did a socket connection between the server & client. As the server sends back information back to the client, it displays invalid charcters after the actual data (Eg. Apple*^@*^, Orange #&$($%@)
I suppose its the size, and I have to set a null at the end of the string. Is that so, could anyone guide me along on ways that I could solve this problem. Thank you.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Hi mwcmp,
jkr has the right idea, but the code has a couple errors. you need to make sure you null terminate the buffer before the end of the buffer... so for a buffer of length 32 you only want to read 31 bytes, and then put the 0 terminator in the 31st position (since arrays are 0 based).
int bytesRecv = SOCKET_ERROR;
char recvbuf[32] = "";
while( bytesRecv == SOCKET_ERROR ) {
bytesRecv = recv( m_socket, recvbuf, 31, 0 );
if ( bytesRecv == 0 || bytesRecv == WSAECONNRESET ) {
// error
}
// zero-terminate string according to the amount of bytes received
recvbuf[bytesRecv] = 0;
good luck,
aaron
jkr has the right idea, but the code has a couple errors. you need to make sure you null terminate the buffer before the end of the buffer... so for a buffer of length 32 you only want to read 31 bytes, and then put the 0 terminator in the 31st position (since arrays are 0 based).
int bytesRecv = SOCKET_ERROR;
char recvbuf[32] = "";
while( bytesRecv == SOCKET_ERROR ) {
bytesRecv = recv( m_socket, recvbuf, 31, 0 );
if ( bytesRecv == 0 || bytesRecv == WSAECONNRESET ) {
// error
}
// zero-terminate string according to the amount of bytes received
recvbuf[bytesRecv] = 0;
good luck,
aaron
<disclaimer>
hey jkr, you da man around these parts so i by no means meant to step on anyones toes. keep up the great info.
peace. -a
hey jkr, you da man around these parts so i by no means meant to step on anyones toes. keep up the great info.
peace. -a
HI Friend,
i dont know what are using , I wanna ask are u making the client/server socket architecture in MFC based classes like CSocket or CAsyncSocket or are using raw Socket Api's
if you are using the raw Api like this
int recv(
SOCKET s,
char FAR *buf,
int len,
int flags
);
the total byte of data recv is returned by the above function
and you create the string like that
char buffer[1000];
int iRec=recv(hSocket,buffer,9 99,0);
buffer[iRec]=NULL;
is the required string by you
Now in MFC
int Receive( void* lpBuf, int nBufLen, int nFlags = 0 );
it s same functionality
i think it will help
i dont know what are using , I wanna ask are u making the client/server socket architecture in MFC based classes like CSocket or CAsyncSocket or are using raw Socket Api's
if you are using the raw Api like this
int recv(
SOCKET s,
char FAR *buf,
int len,
int flags
);
the total byte of data recv is returned by the above function
and you create the string like that
char buffer[1000];
int iRec=recv(hSocket,buffer,9
buffer[iRec]=NULL;
is the required string by you
Now in MFC
int Receive( void* lpBuf, int nBufLen, int nFlags = 0 );
it s same functionality
i think it will help
>>jkr has the right idea, but the code has a couple errors
That's a sample just taken to illustrate the zero-termination :o)
That's a sample just taken to illustrate the zero-termination :o)
ASKER
int bytesRecv = SOCKET_ERROR;
char recvbuf[32] = "";
SOCKET AcceptSocket;
// Receive data to the server
while (1)
{
AcceptSocket = SOCKET_ERROR;
while ( AcceptSocket == SOCKET_ERROR )
{
AcceptSocket = accept( m_socket, NULL, NULL );
}
tmp_socket = m_socket;
m_socket = AcceptSocket;
break;
}
That's my current codes, how should i change to remove those invalid characters?
char recvbuf[32] = "";
SOCKET AcceptSocket;
// Receive data to the server
while (1)
{
AcceptSocket = SOCKET_ERROR;
while ( AcceptSocket == SOCKET_ERROR )
{
AcceptSocket = accept( m_socket, NULL, NULL );
}
tmp_socket = m_socket;
m_socket = AcceptSocket;
break;
}
That's my current codes, how should i change to remove those invalid characters?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER