#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    4
    Rep Power
    0

    Sending A Concatenated string


    I got it to work, by sending the sname>"message" to the client but for some weird reason symbols are appearing after it is received, can anyone see where these are coming from??? :eek:
    Code:
    /* chatserver.c */
    
    #include <stdlib.h>
    #include <stdio.h>
    #include "cnaiapi.h"
    
    #define BUFFSIZE		128
    #define MAX_SNAME                10
    //#define INPUT_PROMPT		" "
    //#define RECEIVED_PROMPT		">" 
    
    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) printf(INPUT_PROMPT);
    		(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
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,363
    Rep Power
    1870
    > /* To add the ">" after the name */
    > sname[len -1] ='>';
    > sname[len] = NULL;
    If your user fills the buffer with a very long name, then this overflows the buffer.
    Also, there is a semantic difference between nul - a character written as '\0', and NULL - a pointer to nothing.

    > printf (sname);
    If your user types a name with a % character in it, which can be very bad

    > if ((len = readln(buff, BUFFSIZE+MAX_SNAME)) < 1) //
    > break;
    > buff[len - 1] = '\n'; //!! another buffer overrun
    > concatenate_strings (sname, MAX_SNAME, buff, BUFFSIZE);
    > printf (sname);
    > (void) send(conn, sname, MAX_SNAME+BUFFSIZE, 0);
    All those X+Y buffer sizes are wrong - it's a buffer overflow.

    If you must use concatenate_strings, then you need a larger buffer to copy into.
    char x[XLEN];
    char y[YLEN];
    char both[XLEN+YLEN];
    strcpy(both,x);
    strcat(both,y);


    Finally, try sending just len characters, rather than the whole buffer with it's trailing junk characters.

    Though it would seem a lot easier just to call send twice
    send(conn,sname,strlen(sname));
    send(conn,buff,strlen(buff));

    Edit:
    cross-posted
    Last edited by salem; September 30th, 2012 at 02:28 AM.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    4
    Rep Power
    0
    Thanks so much. This really helped!!

IMN logo majestic logo threadwatch logo seochat tools logo