quick and dirty draft implementation of server-side UNIX socket handler
This commit is contained in:
parent
7feb436240
commit
f5d1239d28
@ -1,7 +1,103 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#include <linux/un.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <pthread.h>
|
||||||
|
|
||||||
|
#include "yasnd-log.h"
|
||||||
|
|
||||||
|
const char sock_path[]="./demo_socket";
|
||||||
|
pthread_t server_thread_handle; // server socket thread handle
|
||||||
|
int socket_fd; // server socket descriptor
|
||||||
|
|
||||||
|
void* connection_handler(void* param)
|
||||||
|
{
|
||||||
|
// Get connection descriptor
|
||||||
|
int connection_fd=*(int*)param;
|
||||||
|
|
||||||
|
int nbytes;
|
||||||
|
char buffer[256];
|
||||||
|
char tmpbuf[512];
|
||||||
|
|
||||||
|
nbytes = read(connection_fd, buffer, 256);
|
||||||
|
buffer[nbytes] = 0;
|
||||||
|
|
||||||
|
sprintf(tmpbuf,"MESSAGE FROM CLIENT: %s\n", buffer);
|
||||||
|
log_event(tmpbuf);
|
||||||
|
|
||||||
|
close(connection_fd);
|
||||||
|
|
||||||
|
pthread_exit(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void* server_socket(void* param)
|
||||||
|
{
|
||||||
|
struct sockaddr_un address;
|
||||||
|
int connection_fd;
|
||||||
|
socklen_t address_length;
|
||||||
|
|
||||||
|
pthread_t child_handle; // connection thread handle
|
||||||
|
|
||||||
|
socket_fd = socket(PF_UNIX, SOCK_STREAM, 0);
|
||||||
|
if(socket_fd < 0)
|
||||||
|
{
|
||||||
|
log_event("socket creation failed");
|
||||||
|
return (void*)1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Remove old socket file
|
||||||
|
unlink(sock_path);
|
||||||
|
|
||||||
|
// start with a clean address structure
|
||||||
|
memset(&address, 0, sizeof(struct sockaddr_un));
|
||||||
|
|
||||||
|
address.sun_family = AF_UNIX;
|
||||||
|
snprintf(address.sun_path, UNIX_PATH_MAX, sock_path);
|
||||||
|
|
||||||
|
if(bind(socket_fd,
|
||||||
|
(struct sockaddr *) &address,
|
||||||
|
sizeof(struct sockaddr_un)) != 0)
|
||||||
|
{
|
||||||
|
log_event("bind() call failed");
|
||||||
|
return (void*)1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(listen(socket_fd, 5) != 0)
|
||||||
|
{
|
||||||
|
log_event("listen() call failed");
|
||||||
|
return (void*)1;
|
||||||
|
}
|
||||||
|
|
||||||
|
while((connection_fd = accept(socket_fd, (struct sockaddr *) &address, &address_length)) > -1)
|
||||||
|
{
|
||||||
|
if(pthread_create(&child_handle,NULL,connection_handler,&connection_fd) != 0)
|
||||||
|
{
|
||||||
|
log_event("Client connection thread creation failed");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
pthread_detach(child_handle);
|
||||||
|
}
|
||||||
|
|
||||||
|
return (void*)0;
|
||||||
|
}
|
||||||
|
|
||||||
int sock_init()
|
int sock_init()
|
||||||
{
|
{
|
||||||
|
if(pthread_create(&server_thread_handle,NULL,server_socket,NULL) != 0)
|
||||||
|
{
|
||||||
|
log_event("Server socket thread creation failed");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void sock_close()
|
void sock_close()
|
||||||
{
|
{
|
||||||
|
// Terminate server socket handling thread
|
||||||
|
pthread_cancel(server_thread_handle);
|
||||||
|
// Close server socket
|
||||||
|
close(socket_fd);
|
||||||
|
// Remove unneeded socket file
|
||||||
|
unlink(sock_path);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user