1

I am working on a tcp server and when writing to the client(telnet) after displaying the correct output in one line the next line will consist of random characters and letters.

here is the function where i am reading and writing from the server:

void manageConnection(int in, int out)
{
    int readCount,bufCount;
    char inBuf[BUF_LEN], outBuf[BUF_LEN], inData[BUF_LEN], hostname[40];
    char prefix[100];
    char endOfData = '\n';
    int i, revCount;
    char revBuf[BUF_LEN];
    
    gethostname(hostname,40);
    sprintf(prefix,"\tC%d", getpid() );
    fprintf(stderr,"\n%s starting up\n",prefix);
    
    sprintf(outBuf,"\n\n connected to TCP server on host: %s \n"\
                    "enter X to exit otherwise enter the"\
                    "string to do something cool\n",hostname);
    
    write(out,outBuf,strlen(outBuf));
    while(1)
    {
        bufCount = 0;
        while(1)
        {
            readCount = read(in,inData,BUF_LEN);
            if (readCount > 0 )
            {
                if ( (bufCount + readCount) > BUF_LEN)
                {
                    fprintf(stderr,"buffer limit exceeded\n");
                    close(in);
                    exit(EXIT_FAILURE);
                }
                fprintf(stderr,"string from client is %s \n",inData);
                memcpy(&inBuf[bufCount], inData, readCount);
                bufCount=bufCount+readCount;
                if (inData[readCount - 1] == endOfData)
                {
                    break;
                }
            }
            else if (readCount == 0 )
            {
                fprintf(stderr,"\n%s Client has closed connection\n",prefix);
                close(in);
                exit(EXIT_FAILURE);
            }
            else
            {
                sprintf(prefix,"\tC %d: while reading from connection", getpid() );
                perror(prefix);
                close(in);
                exit(EXIT_FAILURE);
            }
        }
     inBuf[bufCount-2] = '\0';
        
        if (inBuf[0] == 'X')
        {
            break;
        }
        revCount = serverProcessing(inBuf,revBuf);
        sprintf(outBuf," the server recieved %ld characters, which when the string is processed"\
                "are:\n%s\n\n enter next string:",strlen(revBuf),revBuf);
        write(out,outBuf,strlen(outBuf));
    }
    fprintf(stderr,"\n%s client has closed the connection\n",prefix);
    close(in);
}

my current output:

terminal where i run the telnet command:

" enter X to terminate, otherwise enter string"

(input) apple

" the server recieved 7 characters which when processed is ApPlE"

"@#V#!@"

on the server terminal:

" client entered apple"

"@V"

EDIT: i added the line > inBuf[bufCount-2] = '\0'; as mentioned in the comments for null terminating my string

on the client side i recieve no more rubbish letters but the letters still appear on the server side

william_
  • 1,131
  • 6
  • 20
  • are you null terminating your strings? – Daniel A. White Aug 10 '20 at 02:25
  • @DanielA.White please see my edit code – william_ Aug 10 '20 at 02:30
  • i mean i would be defensive and fill your buffers with `\0` to start. – Daniel A. White Aug 10 '20 at 02:34
  • Please provide an MCVE ([Minimal, Complete, Verifiable Example](https://stackoverflow.com/help/mcve) — or MRE or whatever name SO now uses) or an SSCCE ([Short, Self-Contained, Correct Example](http://sscce.org/)). – Jonathan Leffler Aug 10 '20 at 02:51
  • @JonathanLeffler for me to post a minimal reproductive example i would need to add unnecessary lines of codes which have no relevant to the issue i am having? From past questions i have received down votes for posting too much code as it was not relevant to the question – william_ Aug 10 '20 at 03:16
  • If you post the minimal code, you won't be posting too much code. Since your problem seems to be resolved, you can get away without an MCVE this time. You can't necessarily expect to do so in future. – Jonathan Leffler Aug 10 '20 at 04:13
  • Because you're printing it wrongly, ignoring the read count. It should be `fprintf(stderr,"string from client is %.*s \n",readCount ,inData);`. You're also not processing Telnet escapes: instead you are treating them as data. – user207421 Aug 10 '20 at 05:29

1 Answers1

-1
  1. initialize all variables like so,
int readCount = 0, bufCount = 0;
char inBuf[BUF_LEN]  = {0};
char outBuf[BUF_LEN] = {0};
char inData[BUF_LEN] = {0};
char hostname[40] = {0};
char prefix[100]  = {0};
char endOfData = '\n';
int i = 0, revCount = 0;
char revBuf[BUF_LEN] = {0};
  1. Null terminate each string after processing.
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • thanks for this, could you please expand on null null terminate each string? i do not quite understand – william_ Aug 10 '20 at 03:17
  • @william_ if you don't know what a null-terminated string is then you really [need a good book](https://stackoverflow.com/q/562303/995714). In the meantime read [What is a null-terminated string?](https://stackoverflow.com/q/2037209/995714) and https://en.wikipedia.org/wiki/Null-terminated_string – phuclv Aug 10 '20 at 04:19
  • 1
    You can't assume you've received an entire string, or an entire message, or that messages are strings, or that there isn't a null in the middle of the data received. The correct solution is to not ignore the read count. – user207421 Aug 10 '20 at 05:30
  • @Marquis of Lorne, I agree with your analysis. the number of bytes read should not be ignored. – Bhaskar Tallamraju Aug 10 '20 at 07:53
  • @MarquisofLorne are you saying to go through the string in a loop and check each character first? – william_ Aug 10 '20 at 08:06
  • @william_ Certainly not. Nothing could be further from what I actually wrote. – user207421 Aug 10 '20 at 08:11