#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <pthread.h>
#include <errno.h>
#define MAX_CLIENTS 100
#define BUFFER_SZ 2048
#define NAME_LEN 32
volatile sig_atomic_t flag = 0;
int sockfd = 0;
char name[NAME_LEN];
void str_overwrite_stdout(){
printf("\r%s", "> ");
fflush(stdout);
}
void str_trim_lf(char* arr, int length){
for(int i = 0; i < length; i ) {
if(arr[i] == '\n'){
arr[i] = '\0';
break;
}
}
}
void catch_ctrl_c_and_exit(){
flag = 1;
}
void recv_msg_handler(){
char message[BUFFER_SZ] = {};
while(1){
int receive = recv(sockfd, message, BUFFER_SZ, 0);
if(receive > 0) {
printf("%s", message);
str_overwrite_stdout();
} else if (receive == 0) {
break;
}
bzero(message, BUFFER_SZ);
}
}
void send_msg_handler(){
char buffer[BUFFER_SZ] = {};
char message[BUFFER_SZ NAME_LEN] = {};
while(1) {
str_overwrite_stdout();
fgets(buffer, BUFFER_SZ, stdin);
str_trim_lf(buffer, BUFFER_SZ);
if(strcmp(buffer, "exit") == 0) {
break;
} else {
sprintf(message, "%s: %s\n", name, buffer);
send(sockfd, message, strlen(message), 0);
}
bzero(buffer, BUFFER_SZ);
bzero(message, BUFFER_SZ NAME_LEN);
}
catch_ctrl_c_and_exit(2);
}
int main(int argc, char **argv) {
if(argc!=2) {
printf("usage: %s <port> \n", argv[0]);
return EXIT_FAILURE;
}
char *ip = "127.0.0.1";
int port = atoi(argv[1]);
signal(SIGINT, catch_ctrl_c_and_exit);
printf("Enter name: ");
fgets(name, NAME_LEN, stdin);
str_trim_lf(name, strlen(name));
if(strlen(name) > NAME_LEN - 1 || strlen(name) < 2){
printf("Enter your name correctly!\n");
return EXIT_FAILURE;
}
struct sockaddr_in server_addr;
//socket settings
sockfd = socket(AF_INET, SOCK_STREAM, 0);
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = inet_addr(ip);
server_addr.sin_port = htons(port);
//connect to server
int err = connect(sockfd,(struct sockaddr*)&server_addr, sizeof(server_addr));
if (err == -1){
printf("ERROR: UNABLE TO CONNECT");
return EXIT_FAILURE;
}
//send name
send(sockfd, name, NAME_LEN, 0);
printf("CHATROOM PROGRAM STARTED\n\n\n");
pthread_t send_msg_thread;
if(pthread_create(&send_msg_thread, NULL, (void*)send_msg_handler, NULL) != 0){
printf("ERROR: PTHREAD ERR\n");
return EXIT_FAILURE;
}
pthread_t recv_msg_thread;
if(pthread_create(&recv_msg_thread, NULL, (void*)recv_msg_handler, NULL) != 0){
printf("ERROR: PTHREAD ERR\n");
return EXIT_FAILURE;
}
while(1){
if(flag){
printf("\nGoodbye\n");
break;
}
}
close(sockfd);
return EXIT_SUCCESS;
}
This is the code for my client.c program. I am currently working on a Chatroom project with a server.c and a client.c. It is supposed to consist of a multithreaded server and a client. I cannot figure out why I am getting these errors when compiling. Any idea why it would be saying they are unused?
client.c: In function ‘send_msg_handler’:
client.c:59:3: warning: ignoring return value of ‘fgets’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
59 | fgets(buffer, BUFFER_SZ, stdin);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
client.c: In function ‘main’:
client.c:87:2: warning: ignoring return value of ‘fgets’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
87 | fgets(name, NAME_LEN, stdin);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
From this image
CodePudding user response:
Any idea why it would be saying they are unused?
That would be because they are unused.
The warnings (not errors) are about the return value of the fgets
calls, which you are altogether ignoring. fgets
returns a value of type char *
, and that return value carries information about the success or failure of the call. Specifically, it returns a null pointer if an error occurs or if the end of the file is reached without any more characters being read.
If you do not recognize and handle such situations then your program is likely to exhibit poor behavior when they occur (and they will). So, for example,
FLAWED:
fgets(name, NAME_LEN, stdin);
BETTER:
if (fgets(name, NAME_LEN, stdin) == NULL) {
// handle EOF / error ...
}
CodePudding user response:
I cannot figure out why I am getting these errors when compiling.
You aren't. As it says, they're warnings, not errors.
Any idea why it would be saying they are unused?
Because they are unused. You ignore the return value of fgets
. That's almost never the right thing to do because you have no idea whether the operation succeeded or failed.