Multipleclient Server: quitfunction - c

I have some problems with my Server/Client.
If multiple people connect to my server, and one of them quit the connection to the server, all other, which are also connected to the server, lose their connection to.
void *connection_handler(void *);
extern int check_err_buffer(int sock);
extern error_t *error_buffer;
extern invalid_user_input_t invalid_user_input;
regex_t regex;
int main(int argc, char *argv[]) {
//init KVS
init();
int socket_desc, client_sock, c, *new_sock;
struct sockaddr_in server, client;
//Create socket
socket_desc = socket(AF_INET, SOCK_STREAM | O_NONBLOCK, 0);
int opts;
opts = fcntl(socket_desc,F_GETFL);
opts = opts & (~O_NONBLOCK);
fcntl(socket_desc,F_SETFL,opts);
if (socket_desc == -1) {
printf("Could not create socket");
}
puts("Socket created");
//Prepare the sockaddr_in structure
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons(8503);
//Bind
if (bind(socket_desc, (struct sockaddr *) &server, sizeof(server)) < 0) {
//print the error message
perror("bind failed. Error");
return 1;
}
puts("bind done");
//Listen
listen(socket_desc, 3);
//Accept and incoming connection
puts("Waiting for incoming connections...");
c = sizeof(struct sockaddr_in);//Accept and incoming connection
puts("Waiting for incoming connections...");
c = sizeof(struct sockaddr_in);
while (1) {
puts("waiting for client...");
client_sock = accept(socket_desc, (struct sockaddr *) &client, (socklen_t * ) & c);
puts("Connection accepted");
pthread_t sniffer_thread;
new_sock = malloc(1);
*new_sock = client_sock;
pthread_create(&sniffer_thread, NULL, connection_handler, (void *) new_sock);
//Now join the thread , so that we dont terminate before the thread
//pthread_join(sniffer_thread, NULL);
puts("Handler assigned");
}
if (client_sock < 0) {
perror("accept failed");
return 1;
}
return 0;
}
In the main function I accept the different connection from the clients.
I have more functions, which I didn't put in, because they are not so relevant for this problem.
In the second part I will handle some cases. I look if the client type the letter 'q', if it's true, I will close the connection with close().
But in all cases, it close the server, and all connections are lost.
/*
* This will handle connection for each client
* */
void *connection_handler(void *socket_desc) {
//Get the socket descriptor
int sock = *(int *) socket_desc;
char *message, client_message[3000];
puts("sending welcome msg to client...");
write(sock, message, 3000);
int reti;
//compile a regexp that checks for valid user input
regcomp(&regex, "([dg] [0-9]+)|([v] .+)|(p [0-9]+ .+)|q", REG_EXTENDED);
//Receive a message from client
char *input[3];
while(1){
//wait for some client input
recv(sock, client_message, 2000, 0);
//check the input with the regexp
reti = regexec(&regex, client_message, 0, NULL, REG_EXTENDED);
if (!reti) {
//tokenize the client input with \n as a delim char
char *token = strtok(client_message, " ");
int i = 0;
while (token) {
input[i] = token;
token = strtok(NULL, " ");
i++;
}
//quit operation
if (strcmp(input[0], "q") == 0) {
//if a client quits, the server dies. TODO: fix
puts("quitting");
//free(socket_desc);
close(socket_desc);
return 0;
}
//user input does not match regexp, try again..
} else if (reti == REG_NOMATCH) {
error_buffer = invalid_user_input;
check_err_buffer(sock);
usleep(100);
write(sock, " ", 2);
}
}
//Free the socket pointer
free(socket_desc);
return 0;
}
On the client side i test if message[0] == 'q' and I close the socket.
I would be very happy if i will get some help or hints to solve this problem. Thank you

As here
new_sock = malloc(1);
the code allocates 1 byte only, this following line
*new_sock = client_sock;
writes to invalid memory, as an int is assigned, and sizeof int bytes are written to where new_sock points. By definition sizeof int is at least 2, probably more, With this undefined behaviour is invoked. Anything may happen from then on.
To fix this do
new_sock = malloc(sizeof *new_sock);
instead.
Inspecting the return value of functions is debugging for free. close(socket_desc); is nonsense and definitely would return -1 and set errno to EBADFD.
Replace
close(socket_desc);
by
if (-1 == close(socket_desc))
{
perror("close() falied");
}
to see how the code as it stand fails.

Related

How can I see HTTP Request in socket C

I'm trying to see the HTTP request to my socket server. When I request from browser this server, naturally, response is invalid because there is no HTTP response here. But I want to see this HTTP request on my terminal screen. How can I do this, any idea?
It is simply multithreded socket server code:
void *connection_handler(void *socket_desc)
{
//Get the socket descriptor
int sock = *((int*)socket_desc);
char *message = "Greetings! I am your connection handler\n";;
//Send some messages to the client
write(sock , message , strlen(message));
message = "Its my duty to communicate with you";
write(sock , message , strlen(message));
free(socket_desc); //Free the socket pointer
return 0;
}
int main(int argc, char *argv[])
{
int socket_desc, new_socket, c, *new_sock;
struct sockaddr_in server, client;
char *message;
socket_desc = socket(AF_INET , SOCK_STREAM , 0);
if (socket_desc == -1)
{
puts("Could not create socket");
return 1;
}
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons(8888);
if(bind(socket_desc, (struct sockaddr *)&server, sizeof(server)) < 0)
{
puts("Binding failed");
return 1;
}
listen(socket_desc, 3);
puts("Waiting for incoming connections...");
c = sizeof(struct sockaddr_in);
while((new_socket =
accept(socket_desc, (struct sockaddr *)&client, (socklen_t*)&c)) )
{
puts("Connection accepted");
message = "Hello Client, I will assign a handler for you\n";
write(new_socket, message, strlen(message));
pthread_t sniffer_thread;
new_sock = malloc(1); // a memory allocator
*new_sock = new_socket;
if(pthread_create(&sniffer_thread, NULL, connection_handler,
(void*)new_sock) < 0)
{
puts("Could not create thread");
return 1;
}
puts("Handler assigned");
}
return 0;
}

Sending data from server to client socket programming

I have just started learning socket programming.
Currently the server and the client are on the same workstation and everything seems to work. The server is running in c programming and my client is an android program. I managed to have a one way connection, sending data from client to server. I would like to send some string back to client.
Please advice me.
void *connection_handler(void *);
int main(int argc , char *argv[])
{
int socket_desc , client_sock , c;
struct sockaddr_in server , client;
//Create socket
socket_desc = socket(AF_INET , SOCK_STREAM , 0);
if (socket_desc == -1)
{
printf("Could not create socket");
}
puts("Socket created");
//Prepare the sockaddr_in structure
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons( 7800 );
//Bind
if( bind(socket_desc,(struct sockaddr *)&server , sizeof(server)) < 0)
{
//print the error message
perror("bind failed. Error");
return 1;
}
puts("bind done");
//Listen
listen(socket_desc , 3);
//Accept and incoming connection
//puts("Waiting for incoming connections...");
//c = sizeof(struct sockaddr_in);
//Accept and incoming connection
puts("Waiting for incoming connections...");
c = sizeof(struct sockaddr_in);
pthread_t thread_id;
while( (client_sock = accept(socket_desc, (struct sockaddr *)&client, (socklen_t*)&c)) )
{
puts("Connection accepted");
if( pthread_create( &thread_id , NULL , connection_handler , (void*) &client_sock) < 0)
{
perror("could not create thread");
return 1;
}
//Now join the thread , so that we dont terminate before the thread
//pthread_join( thread_id , NULL);
puts("Handler assigned");
}
if (client_sock < 0)
{
perror("accept failed");
return 1;
}
return 0;
}
/*
* This will handle connection for each client
* */
void *connection_handler(void *socket_desc)
{
//Get the socket descriptor
int sock = *(int*)socket_desc;
int read_size;
char *message , client_message[2000];
//Send some messages to the client
message = "Greetings! I am your connection handler\n";
write(sock , message , strlen(message));
message = "Now type something and i shall repeat what you type \n";
write(sock , message , strlen(message));
//Receive a message from client
while( (read_size = recv(sock , client_message , 2000 , 0)) > 0 )
{
//end of string marker
client_message[read_size] = '\0';
printf("%s\n", client_message);
//Send the message back to client
write(sock , client_message , strlen(client_message));
//clear the message buffer
memset(client_message, 0, 2000);
}
if(read_size == 0)
{
//puts("Client disconnected");
//fflush(stdout);
}
else if(read_size == -1)
{
perror("recv failed");
}
return 0;
}
There are different ways to transmit data to another socket and it doesn't matter if it's either a server or a client.
write
As you did in your code, you can use the system call:
ssize_t write(int fd, const void *buf, size_t count);
It writes from the buf to the file related to the file descriptor fd. The return value will tell you if the message was sent correctly.
send, sendto, sendmsg
This is the second way you can send data to a socket. The only difference between write and send is the argument flags:
ssize_t send(int sockfd, const void *buf, size_t len, int flags);
but if you set flag to 0, write and send will work in the same way.
sendto and sendmsg are quite different from send because they have more arguments. You can get all information from man pages available online or in Linux.
http://man7.org/linux/man-pages/man2/send.2.html
while( (client_sock = accept(socket_desc, (struct sockaddr *)&client, (socklen_t*)&c)) )
is wrong; you probably want to check >= 0 or (even better) express this in another way.
The lone
write(sock , message , strlen(message));
is not good:
you should check the error code
it will be very difficult for the receiver to split the result.
write() will kill your program with SIGPIPE when the other end has been closed
I suggest something like
bool send_data(int fd, void const *data, size_t len)
{
while (len > 0) {
ssize_t l = send(fd, data, len, MSG_NOSIGNAL);
if (l > 0) {
data += l;
len -= l;
} else if (l == 0) {
fprintf(stderr, "this is ugly and can not happen...\n");
break;
} else if (errno == EINTR) {
continue;
} else {
perror("send()");
break;
}
}
return len == 0;
}
and for string datatypes
bool send_string(int fd, char const *str)
{
size_t l = strlen(str);
uint32_t l_be = htobe32(l);
if ((uint32_t)(l) != l)
return false;
return (send_data(fd, &l_be, sizeof l_be) &&
send_data(fd, str, l));
}
For receiving, you can implement recv_data() similarly to send_data() above. Depending on your memory allocation strategy, you can implement either
bool recv_string(int fd, char *str, size_t max_len, size_t *len)
{
uint32_t l_be;
size_t l;
if (!recv_data(fd, &l_be, sizeof l_be))
return false;
l = be32toh(l_be);
if (l >= max_len)
return false;
if (!recv_data(fd, str, l))
return false;
str[l] = '\0';
if (len)
*len = l;
return true;
}
or write something which malloc() memory for the received data after reading the length.
Now, you can do on one side:
if (!send_string(fd, "foo") ||
!send_string(fd, "bar") ||
!recv_string(fd, resp, sizeof resp, NULL))
error();
and on the other one
if (!recv_string(fd, req0, sizeof req0, NULL) ||
!recv_string(fd, req1, sizeof req1, NULL) ||
!send_string(fd, handle_request(req0, req1)))
error();

Linux C: segfault error 4 in libmysqlclient.so.18.0.0

I'm trying to implement a multithreaded tcp server with the following code.
int main(int argc , char *argv[])
{
int socket_desc , client_sock , c , *new_sock;
struct sockaddr_in server , client;
//Create socket
socket_desc = socket(AF_INET , SOCK_STREAM , 0);
if (socket_desc == -1)
{
printf("Could not create socket");
}
puts("Socket created");
//Prepare the sockaddr_in structure
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons( 8888 );
//Bind
if( bind(socket_desc,(struct sockaddr *)&server , sizeof(server)) < 0)
{
//print the error message
perror("bind failed. Error");
return 1;
}
puts("bind done");
//Listen
listen(socket_desc , 3);
//Accept and incoming connection
puts("Waiting for incoming connections...");
c = sizeof(struct sockaddr_in);
while( (client_sock = accept(socket_desc, (struct sockaddr *)&client, (socklen_t*)&c)) )
{
puts("Connection accepted");
pthread_t sniffer_thread;
new_sock = malloc(1);
*new_sock = client_sock;
if( pthread_create( &sniffer_thread , NULL , connection_handler , (void*) new_sock) < 0)
{
perror("could not create thread");
return 1;
}
//Now join the thread , so that we dont terminate before the thread
//pthread_join( sniffer_thread , NULL);
puts("Handler assigned");
}
if (client_sock < 0)
{
perror("accept failed");
return 1;
}
return 0;
}
/*
* This will handle connection for each client
* */
void *connection_handler(void *socket_desc)
{
//Get the socket descriptor
int newsockfd = *(int*)socket_desc;
int read_size;
char *message , buffer[2000];
int n;
struct auth details;
char* reply;
char cmd[100] = {0};
//Receive a message from client
while( (read_size = recv(newsockfd , buffer , 2000 , 0)) > 0 )
{
printf("Incoming data: %s", buffer);
if(parseData(cmd, buffer, "/", "-")) /* If received data is compliant */
{
details = authenticate(cmd);
if (details.verified) // If said user can execute this command.
{
reply = execute(cmd); // Execute.
n = write(newsockfd, reply, strlen(reply));
if (n < 0) { error("ERROR writing to socket"); }
free(reply);
reply = NULL;
}
else // Authentication failed.
{
n = write(newsockfd, details.error_msg, strlen(details.error_msg));
if (n < 0) { error("ERROR writing to socket"); }
else{
if(debug) printf("sent: %s\n", details.error_msg);
}
}
}
}
if(read_size == 0)
{
puts("Client disconnected");
fflush(stdout);
}
else if(read_size == -1)
{
perror("recv failed");
}
//Free the socket pointer
free(socket_desc);
return 0;
}
The authenticate function is defined in an implementation file:
struct auth authenticate(const char* const command)
{
struct auth data;
char* db_pwd;
char* perm;
char* username = "testUser";
char query[200] = {0};
data.verified = FALSE;
data.error_msg = "";
printf("in auth\n");
MYSQL_ROW row;
snprintf(query, 200, "SELECT password FROM userlist WHERE user='%s'", username);
if ((row = mysql_fetch_row(run_query(query)))) // if user is on list
{
db_pwd = row[0];
if(!strcmp(db_pwd, password))
{
if (!(strcmp(command, "test")))
{
data.verified = TRUE;
}
}
}
return data;
}
I have a working single threaded version of this program(main) with the same implementation file, the segmentation fault occurs only with the multithreaded version.
Am I doing something wrong in my code?
How should I proceed with debugging?
Any ideas would be appreciated.
Read documentation of MySQL C API, notably ยง 23.8.12 C API Threaded Function Descriptions It internally uses a socket connection to the mysqld server process, so you have to serialize all MySQL functions.
So, define your global mutex, and protect all the functions doing mysql calls with that mutex (from a request to fetching all the rows of the reply). Better yet, design your application so that only one single thread (usually the main one) is using mysql functions.
Compile with all warnings and debug info (gcc -Wall -g). Use the debugger (gdb) and perhaps valgrind
If unfamiliar with mutexes, you might want to read a pthread tutorial

UDP multi-client chat server

I have a multi-client chat server and for some reason only the first client is being added. I used a tutorial to help get me started. I have included my code below. When I try and add another client it doesnt appear to be added. If I add one client I get a response from the server like I want but only the first message I enter then after that it stops sending correctly.
Server Code:
int main(void)
{
struct sockaddr_in my_addr, cli_addr[10],cli_temp;
int sockfd;
socklen_t slen[10],slen_temp;
slen_temp = sizeof(cli_temp);
char buf[BUFLEN];
int clients = 0;
int client_port[10];
if ((sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1)
{
printf("test\n");
err("socket");
}else{
printf("Server : Socket() successful\n");
}
bzero(&my_addr, sizeof(my_addr));
my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(PORT);
my_addr.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(sockfd, (struct sockaddr* ) &my_addr, sizeof(my_addr))==-1)
{
err("bind");
}else{
printf("Server : bind() successful\n");
}
int num_clients = 0;
while(1)
{
//receive
printf("Receiving...\n");
if (recvfrom(sockfd, buf, BUFLEN, 0, (struct sockaddr*)&cli_temp, &slen_temp)==-1)
err("recvfrom()");
if (clients <= 10) {
cli_addr[clients] = cli_temp;
client_port[clients] = ntohs(cli_addr[clients].sin_port);
clients++;
printf("Client added\n");
//printf("%d",clients);
int i;
for(i=0;sizeof(clients);i++) {
sendto(sockfd, buf, BUFLEN, 0, (struct sockaddr*)&cli_addr[i], sizeof(cli_addr[i]));
}
}
}
close(sockfd);
return 0;
}
I have included the client code as well in case it helps.
void err(char *s)
{
perror(s);
exit(1);
}
sig_atomic_t child_exit_status;
void clean_up_child_process (int signal_number)
{
/* Clean up the child process. */
int status;
wait (&status);
/* Store its exit status in a global variable. */
child_exit_status = status;
}
int main(int argc, char** argv)
{
struct sockaddr_in serv_addr;
int sockfd, slen=sizeof(serv_addr);
char buf[BUFLEN];
struct sigaction sigchld_action;
memset (&sigchld_action, 0, sizeof (sigchld_action));
sigchld_action.sa_handler = &clean_up_child_process;
sigaction (SIGCHLD, &sigchld_action, NULL);
int pid,ppid;
if(argc != 2)
{
printf("Usage : %s <Server-IP>\n",argv[0]);
exit(0);
}
if ((sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1)
err("socket");
bzero(&serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT);
if (inet_aton(argv[1], &serv_addr.sin_addr)==0)
{
fprintf(stderr, "inet_aton() failed\n");
exit(1);
}
pid = fork();
if (pid<0) {
err("Fork Error");
}else if (pid==0) {
//child process will receive from server
while (1) {
bzero(buf,BUFLEN);
//printf("Attempting to READ to socket %d: ",sockfd);
fflush(stdout);
//recvfrom here
if (recvfrom(sockfd, buf, BUFLEN, 0, (struct sockaddr*)&serv_addr, &slen)==-1)
err("recvfrom()");
printf("The message from the server is: %s \n",buf);
if (strcmp(buf,"bye\n") == 0) {
ppid = getppid();
kill(ppid, SIGUSR2);
break;
}
}
}else {
//parent will send to server
while(1){
printf("Please enter the message to send: ");
bzero(buf,BUFLEN);
fgets(buf,BUFLEN,stdin);
printf("Attempting to write to socket %d: ",sockfd);
fflush(stdout);
//send to here
if (sendto(sockfd, buf, BUFLEN, 0, (struct sockaddr*)&serv_addr, slen)==-1)
{
err("sendto()");
}
}
}
close(sockfd);
return 0;
}
Several problems jump out at me. First, every time you receive a message it will consider that to be a new client. Instead of just incrementing the clients variable for a message, you'll need to scan through the array to see if the source address is already present. Second, sizeof(clients) will return a static value (probably 4) depending on how many bytes an int occupies on your machine. That loop should be for( int i = 0; i < clients; i++ ).
You also have a variable named num_clients which is not used. Is that supposed to be there for something and maybe is causing some confusion?
Finally, instead of using the magic value 10 all over the place, use #define MAX_CONNECTIONS 10 and then replace all those numbers with MAX_CONNECTIONS. It's a lot easier to read and change later.

C, socket programming: Connecting multiple clients to server using select()

I'm trying to make a server that can be connected to by multiple clients. Here's my code so far:
Client:
int main(int argc, char **argv) {
struct sockaddr_in servaddr;
int sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sock == -1) perror("Socket");
bzero((void *) &servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(6782);
servaddr.sin_addr.s_addr = inet_addr(<server_ip_address>);
if (-1 == connect(sock, (struct sockaddr *)&servaddr, sizeof(servaddr)))
perror("Connect");
while(1) {
char message[6];
fgets(message, 6, stdin);
message[5] = '\0';
send(sock, message, 6, 0);
}
close(sock);
}
Server:
int main(int argc, char **argv) {
fd_set fds, readfds;
int i, clientaddrlen;
int clientsock[2], rc, numsocks = 0, maxsocks = 2;
int serversock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (serversock == -1) perror("Socket");
struct sockaddr_in serveraddr, clientaddr;
bzero(&serveraddr, sizeof(struct sockaddr_in));
serveraddr.sin_family = AF_INET;
serveraddr.sin_addr.s_addr = htonl(INADDR_ANY);
serveraddr.sin_port = htons(6782);
if (-1 == bind(serversock, (struct sockaddr *)&serveraddr,
sizeof(struct sockaddr_in)))
perror("Bind");
if (-1 == listen(serversock, SOMAXCONN))
perror("Listen");
FD_ZERO(&fds);
FD_SET(serversock, &fds);
while(1) {
readfds = fds;
rc = select(FD_SETSIZE, &readfds, NULL, NULL, NULL);
if (rc == -1) {
perror("Select");
break;
}
for (i = 0; i < FD_SETSIZE; i++) {
if (FD_ISSET(i, &readfds)) {
if (i == serversock) {
if (numsocks < maxsocks) {
clientsock[numsocks] = accept(serversock,
(struct sockaddr *) &clientaddr,
(socklen_t *)&clientaddrlen);
if (clientsock[numsocks] == -1) perror("Accept");
FD_SET(clientsock[numsocks], &fds);
numsocks++;
} else {
printf("Ran out of socket space.\n");
}
} else {
int messageLength = 5;
char message[messageLength+1];
int in, index = 0, limit = messageLength+1;
while ((in = recv(clientsock[i], &message[index], limit, 0)) > 0) {
index += in;
limit -= in;
}
printf("%d\n", index);
printf("%s\n", message);
}
}
}
}
close(serversock);
return 0;
}
As soon as a client connects and sends its first message, the server just runs in an infinite loop, and spits out garbage from the message array. recv doesn't seem to receive anything. Can anyone see where i go wrong?
Two issues in your code:
You should do recv(i, ...) instead of recv(clientsock[i], ...)
After that you do not check if recv() failed, and therefore printf() prints out the uninitialised buffer message, hence the garbage in the output
You need to check for limit <= 0 in your read loop, before you call read.
In the while loop for the server, change the code to do recv(i) instead of recv(clientsocks[i]). I have implemented this code and it works with this change.
I replaced the else with the below and it works
} else {
/* int messageLength = 5;
char message[messageLength+1];
int in, index = 0, limit = messageLength+1;
memset ( &message[index] , 0, sizeof ( message [index] ) );
while ((in = recv(i, &message[index], limit, 0)) > 0) {
index += in;
limit -= in;
}
printf("%d\n", index);
printf("%s\n", message);
*/
bzero(buf, sizeof(buf));
if ((rval = read(i, buf, 1024)) < 0)
perror("reading stream message");
else if (rval == 0)
printf("Ending connection\n");
else
printf("-->%s\n", buf);
}
1) It is a good practice to use PF_INET(protocol family) rather than
AF_INET(address family) during the Socket creation .
2) within the while(1) loop
each time it is advisable to make your readfds empty by using FD_ZERO(&readfds).
in the recv() call you should use i rather than clientsocks[i]
you have to check return value of recv is negative(which indicating error in reading) if that is the case you do not have to print the message.
during printing the message make sure the stdout/server is ready for writing anything to it which you can do it by using writefds (3rd argument of select).

Resources