Thread: Sending A Concatenated string

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    2

    Sending A Concatenated string

    i created a server to where the Sname along with the mesage is sent over to the client, but when the sname and message is received random symbols are appearing. Can anybody help me and determine why these symbols are appearing. Any help is greatly appreciated

    Code:
    /* chatserver.c */
    
    
    #include <stdlib.h>
    #include <stdio.h>
    #include "cnaiapi.h"
    
    
    #define BUFFSIZE        128
    #define MAX_SNAME                10
    
    
    int recvln(connection, char *, int);
    int readln(char *, int);
    
    
    void concatenate_strings (char *source, int source_len, char *dest, int dest_len)
    {    int i;
        for(i=0; i<dest_len; i++){
            source[source_len + i] = dest[i]; }
        source[source_len + dest_len] = NULL;
    }
    
    
    /*-----------------------------------------------------------------------
     *
     * Program: chatserver
     * Purpose: wait for a connection from a chatclient & allow users to chat
     * Usage:   chatserver <appnum>
     *
     *-----------------------------------------------------------------------
     */
    int
    main(int argc, char *argv[])
    {
        connection    conn;
        int        len;
        char        buff[BUFFSIZE];
          char            sname[MAX_SNAME];
    
    
    
    
        if (argc != 2) {
            (void) fprintf(stderr, "usage: %s <appnum>\n", argv[0]);
            exit(1);
        }
    
    
        
        /* Input Name to use */
    
    
        printf ("Enter your chat name. \n");
        len = readln( sname, MAX_SNAME);
    
    
        /* To add the ">" after the name */
        sname[len -1] ='>';
        sname[len] = NULL;  
        
    
    
        (void) printf("Chat Server Waiting For Connection.\n");
     
        
    
    
        /* wait for a connection from a chatclient */
    
    
        conn = await_contact((appnum) atoi(argv[1]));
        if (conn < 0)
            exit(1);
        
        (void) printf("Chat Connection Established.\n");
        
        /* iterate, reading from the client and the local user */
    
    
        while((len = recvln(conn, buff, BUFFSIZE)) > 0) {
            (void) fflush(stdout);
            (void) write(STDOUT_FILENO, buff, len);
            
            /* send a line to the chatclient */
    
    
            printf (sname);
            (void) fflush(stdout);
            if ((len = readln(buff, BUFFSIZE+MAX_SNAME)) < 1)   // 
                break;
            buff[len - 1] = '\n';
            concatenate_strings (sname, MAX_SNAME, buff, BUFFSIZE); 
            printf (sname);
            (void) send(conn, sname, MAX_SNAME+BUFFSIZE, 0);          
        }
    
    
          /* iteration ends when EOF found on stdin or chat connection */
    
    
        (void) send_eof(conn);
        (void) printf("\nChat Connection Closed.\n\n");
        return 0;
    }

  2. #2
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    I recently read (Deep C Secretes) that you should never confuse "NULL" with "NUL"
    NULL is for a pointer
    NUL is for the end of a string

    I found another reference for this at C Language Gotchas under the section "NULL pointer Gotchas" at the end of that section.
    Fact - Beethoven wrote his first symphony in C

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Code:
    concatenate_strings (sname, MAX_SNAME, buff, BUFFSIZE);
    ...
    void concatenate_strings (char *source, int source_len, char *dest, int dest_len)
    {
        int i;
        for(i=0; i<dest_len; i++){
            source[source_len + i] = dest[i]; }
        source[source_len + dest_len] = NULL;
    }
    Inside concatenate_strings() source will be sname (an array of MAX_SNAME chars), source_len will be MAX_SNAME (which is 10).
    Thus the line
    Code:
    source[source_len + i] = dest[i];
    will write beyond the limit of sname.
    If you want to concatenate strings you need an array of chars which is big enough to hold both strings.

    Bye, Andreas
    Last edited by AndiPersti; 09-30-2012 at 04:00 AM. Reason: clarification

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 15
    Last Post: 11-17-2010, 11:20 PM
  2. Sending a string to another computer
    By guitarist809 in forum Networking/Device Communication
    Replies: 3
    Last Post: 08-22-2007, 11:18 PM
  3. Sending a String to a Window
    By X PaYnE X in forum Windows Programming
    Replies: 5
    Last Post: 03-31-2004, 12:32 PM
  4. Sending a string to C++ from VB 6.
    By VirtualAce in forum C++ Programming
    Replies: 4
    Last Post: 08-21-2001, 02:28 AM