Thread: Using Concatenate strings to communicate to a client

  1. #1
    Registered User
    Join Date
    Sep 2012

    Post Using Concatenate strings to communicate to a client

    I got the server to send name of chat user along with message ex: sname> "message" but for some reason im getting weird symbols after sname> is received... Can someone see where i can improve this 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>
    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]);
        /* 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)
        (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)   
            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
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    My answer is over there -> Sending A Concatenated string - Dev Shed

    Stop with the spamming already!
    Sending A Concatenated string
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Concatenate two strings.
    By ModeSix in forum C Programming
    Replies: 21
    Last Post: 04-26-2011, 10:12 AM
  2. Replies: 2
    Last Post: 03-30-2009, 12:25 AM
  3. concatenate two strings! K&R problem
    By karanmitra in forum Linux Programming
    Replies: 2
    Last Post: 08-09-2005, 10:44 AM
  4. Concatenate chars and std::strings.
    By Hulag in forum C++ Programming
    Replies: 3
    Last Post: 06-29-2005, 08:20 AM
  5. can't get MFC to communicate with itself
    By drb2k2 in forum C++ Programming
    Replies: 2
    Last Post: 04-04-2003, 04:06 PM