Solved

Socket Programming (Client recieving invalid characters)

Posted on 2004-09-21
10
195 Views
Last Modified: 2013-11-20
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.

0
Comment
Question by:mwcmp
  • 3
  • 2
  • 2
  • +1
10 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 250 total points
Comment Utility
If you receive data like

    int bytesRecv = SOCKET_ERROR;
    char recvbuf[32] = "";

    while( bytesRecv == SOCKET_ERROR ) {
        bytesRecv = recv( m_socket, recvbuf, 32, 0 );
        if ( bytesRecv == 0 || bytesRecv == WSAECONNRESET ) {
            // error
        }

    // zero-terminate string according to the amount of bytes received
    recvbuf[bytesRecv + 1] = 0;

the last line will correctly zero-terminate the string.
0
 

Author Comment

by:mwcmp
Comment Utility
Ok, i shall try it out and let you know again. Thanks!
0
 
LVL 8

Expert Comment

by:AaronReams
Comment Utility
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
0
 
LVL 8

Expert Comment

by:AaronReams
Comment Utility
<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
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 1

Expert Comment

by:cool_alok
Comment Utility
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,999,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

   
0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
>>jkr has the right idea, but the code has a couple errors

That's a sample just taken to illustrate the zero-termination :o)
0
 

Author Comment

by:mwcmp
Comment Utility
  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?
0
 
LVL 8

Assisted Solution

by:AaronReams
AaronReams earned 250 total points
Comment Utility
Well you aren't actually receiving any characters with that code.  After you accept the socket you need to do a recv to get characters.

After your while loop... add the following code to continually receive characters.

char buffer[1024];
while(true)
{
          bytesRecv = recv(AcceptSocket, buffer, 1023, 0);
          if(bytesRecv==0)
          {
            //Connection closed
              break;
          }
      else
      if(bytesRecv<0)
      {
            //Connection failed
            break;
      }
      buffer[bytesRecv]=0;
      printf("Your characters received are = %s",buffer);

}
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now