Below are the two files from our project that we need to modify so the user enters a name that will appear before their typed text. We must prepend the name followed by a greater-than sign to each line of the text before sending the text across the network or to the receiver. I am having issues with sending the user input name to the other file. Can anyone help? I was going to try global variables but that didn't work either ( see header file below).
Below is the client codeCode:/* chatserver.c */ #include <stdlib.h> #include <stdio.h> #include "cnaiapi.h" #define BUFFSIZE 256 #define MAX_SNAME 15 #define INPUT_PROMPT " >" #define RECEIVED_PROMPT ">" int recvln(connection, char *, int); int readln(char *, int); /*----------------------------------------------------------------------- * * 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); } //(void)printf("Enter your chat name followed by ">". \n"); //scanf("%s", &sname); /* Input Name to use */ printf ("Enter your chat name. \n"); scanf("%s", sname); //len = recvln( sname, MAX_SNAME); (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) { //printf (cname); /// To Test Global Variable ( Didn't work!! :( (void) printf(RECEIVED_PROMPT); //len = readln(buff 2, BUFFSIZE+MAX_CNAME); /// tRY RUNNING IT WITH THese LINE TO SEE WHAT HAPPENS!! //printf (len); (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)) < 1) break; buff[len - 1] = '\n'; (void) send(conn, buff, len, 0); } /* iteration ends when EOF found on stdin or chat connection */ (void) send_eof(conn); (void) printf("\nChat Connection Closed.\n\n"); return 0; }
Below is the header file where I tried declaring my global variables.Code:/* chatclient.c */ #include <stdlib.h> #include <stdio.h> #include "cnaiapi.h" #define BUFFSIZE 256 #define MAX_CNAME 15 #define INPUT_PROMPT " > " #define RECEIVED_PROMPT "> " int recvln(connection, char *, int); int readln(char *, int); /*----------------------------------------------------------------------- * * Program: chatclient * Purpose: contact a chatserver and allow users to chat * Usage: chatclient <compname> <appnum> * *----------------------------------------------------------------------- */ int main(int argc, char *argv[]) { computer comp; connection conn; char buff[BUFFSIZE]; int len; char cname[MAX_CNAME]; if (argc != 3) { (void) fprintf(stderr, "usage: %s <compname> <appnum>\n", argv[0]); exit(1); } // printf("Enter your chat name followed by ">". \n"); // scanf("%c", &cname); /* convert the compname to binary form comp */ comp = cname_to_comp(argv[1]); if (comp == -1) exit(1); /* Input Name to use */ printf ("Enter your chat name. \n"); scanf("%s", cname); //len = recvln( cname, MAX_CNAME); /* make a connection to the chatserver */ conn = make_contact(comp, (appnum) atoi(argv[2])); if (conn < 0) exit(1); (void) printf("Chat Connection Established.\n"); printf (cname); (void) printf(INPUT_PROMPT); (void) fflush(stdout); /* iterate, reading from local user and then from chatserver */ while((len = readln(buff, BUFFSIZE)) > 0) { buff[len - 1] = '\n'; (void) send(conn, buff, len, 0); /* receive and print a line from the chatserver */ if ((len = recvln( conn, buff, BUFFSIZE)) < 1) break; (void) printf(RECEIVED_PROMPT); (void) fflush(stdout); (void) write(STDOUT_FILENO, buff, len); (void) printf(INPUT_PROMPT); (void) fflush(stdout); } /* iteration ends when stdin or the connection indicates EOF */ (void) printf("\nChat Connection Closed.\n"); (void) send_eof(conn); exit(0); }
Any help is appriciated. Thanks!!!!Code:/* cnaiapi.h */ #ifndef _CNAIAPI_H_ #define _CNAIAPI_H_ #define LINUX #if defined(LINUX) || defined(SOLARIS) #include <sys/socket.h> #include <netinet/in.h> #include <netdb.h> #include <pthread.h> #include <unistd.h> #endif /* defined(LINUX) || defined(SOLARIS) */ #if defined(WIN32) #include <winsock2.h> #include <cnaiapi_win32.h> #endif /* defined(WIN32) */ #include <sys/types.h> typedef short appnum; typedef int computer; typedef int connection; extern char sname; // Added these global variables extern char cname; // Added these global variables struct port2sock { short port; int sock; }; #define P2S_SIZE 64 /* number of entries in port to socket map table */ #define LISTEN_Q_LEN 5 appnum appname_to_appnum(char *appname); computer cname_to_comp(char *cname); connection await_contact(appnum a); connection make_contact(computer c, appnum a); int send_eof(connection c); void cnaiapi_init(void); #if defined(LINUX) || defined(SOLARIS) extern pthread_mutex_t await_contact_mutex, cname_mutex, appname_mutex; #elif defined(WIN32) extern HANDLE await_contact_mutex, cname_mutex, appname_mutex; #endif #endif /* !defined(_CNAIAPI_H_) */



LinkBack URL
About LinkBacks


