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
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; }



LinkBack URL
About LinkBacks


