regarding:
if(n<0){
printf("Buffer: %s", buffer);
error("Reading Failed");
}
if(n<0){ printf("Buffer: %s", buffer); error("Reading Failed"); }
the call to printf() will change the value in errno. The result is the call to perror() in function: error() will print the wrong message.
<.>
regarding:
void* job_read(void * p){
int* socketp = (int*)p;
int newsockfd = (*socketp);
void* job_read(void * p){ int* socketp = (int*)p; int newsockfd = (*socketp);
the variable socketp is not used elsewhere in this function. Suggest:
void* job_read(void * p){
int newsockfd = *(int*)p;
Also, the correct type for a socket is sock_t, not int
<.>
regarding:
#define MAX_CONNECTIONS 128
#define MAX_CONNECTIONS 128
It would be advisable to check your OS to assure it will allow 128 simultaneous connections. Many 'user' OS versions will not allow anywhere near that many simultaneous connections.
<.>
regarding:
m = write(socket_ids[i], name, strlen(name));
n = write(socket_ids[i], buffer, strlen(buffer));
if(n < 0 | m < 0){
error("Writing failed");
}
m = write(socket_ids[i], name, strlen(name)); n = write(socket_ids[i], buffer, strlen(buffer)); if(n < 0 | m < 0){ error("Writing failed"); }
If the first call to write() fails, it will have set errno indicating the reason the system thinks the error occurred. However, the second call to write() will overlay the value in errno, so the call to error()/perror() will output the wrong message.
suggest:
if( (m = write(socket_ids[i], name, strlen(name)) ) < 0 )
{
error( "write of user name failed" );
}
if( (n = write(socket_ids[i], buffer, strlen(buffer)) ) < 0)
{
error("Writing of user message failed");
}