------------------------------------- Programming suggestions for Lab #02 ------------------------------------- -Ian! D. Allen - idallen@idallen.ca Here are some comments that will help you fix your code to work correctly and earn full marks in future assignments. You can't have a working Lab #3 until you have a working Lab #2. You have to live with your own code. This note file is full of wrong examples, not good code. - missing comments on the code you added - if you write code - comment it (see notes file programming_style.txt) - if you use raw code from other people - comment it - future assignments: no comments, no marks - also comment what the code does, not just what the assignment wants: /* The code was modified for the purposes of this assignment */ - use the exact file names given in the lab exercise - if the lab says to use a particular set of names to the cstsubmit command, you have to use those names or else my marking-assist scripts won't find your files for marking - if you copy code from other people, make sure it works: - sendall.c has an uninitialized variable error if passed a zero length - gcc warnings would have told you this - read your gcc warnings and fix them - several people have broken linked list implementations - use valgrind - get EOF handling right in your client - see notes file: eof_handling.txt - don't print a line when you get EOF - there is nothing there - watch out for lack of \0 on buffers n = read(sockfd,buffer,255); /* read from socket */ if (n < 0) { kill(chid,SIGTERM); /* kill child to prevent zombie */ error("ERROR reading from socket"); return 0; /* return on read error */ } printf("\n%s\n",buffer); - don't write useless comments (see notes file programming_style.txt) // bind // listen int sendall(int s, char *buf, int *len); /* declare prototype */ chid = fork(); /* split to two processes */ n = read(sockfd,buffer,255); /* read from socket */ - don't use magic numbers; #define them or use sizeof() - related numbers should relate in your code - make it hard for a change in size to break your code char buffer[256]; bzero(buffer,256); n = read(sockfd,buffer,255); /* read from socket */ - don't use "else" after return/exit - and use argv[0] for program name, not compiled-in name if(argc != 2) error("Usage: ./chat [port]\n"); else port = atoi(argv[1]); - don't write garbage bytes to your clients - if you read nbytes, only write nbytes nbytes = read(..., buf, ... if (sendall(newfd, buf, sizeof(buf)) == -1) ... - don't overflow your buffers; use snprintf() not sprintf() char buf[256]; char buf2[256]; nbytes = sprintf(buf2,"%s:%d: %.*s", ips[i], i, nbytes, buf); - don't overflow or underflow your buffers - don't index with a bad number (-1) or go beyond (nbytes) nbytes = read(..., buf, sizeof(buf) buf[nbytes] = '\0'; - if you use command line arguments, check for the right number - make sure the number of arguments exactly matches what you expect - don't simply check for argc >= 2 - check that argc == 2 - never ignore user command line input - no inconsistent indentation style - correct indentation is essential; unreadable code won't be marked - Unix tab stops are every 8 spaces; your submitted code must match - "man expand"