Hey everyone,
i'm very new to C programming, and i don't have enough knowledge of how to threads, i'm having the following program, i think i need to use threads in it, but i'm not really sure, what's ur opinion?
this is a very simple http server, which loads up some html pages:
here's the main function:
Code:
int main(int argc, char *argv[]) {
int sock;
int conn;
pid_t pid;
struct sockaddr_in servaddr;
/* Create socket */
sock = socket(AF_INET, SOCK_STREAM, 0);
/* Populate socket address structure */
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(SERVER_PORT);
/* Assign socket address to socket */
bind(sock, (struct sockaddr *) &servaddr, sizeof(servaddr));
/* Make socket a listening socket */
listen(sock, LISTENQ);
/* Loop infinitely to accept and service connections */
while ( 1 ) {
/* Wait for connection */
if ( (conn = accept(sock, NULL, NULL)) < 0 )
perror("Error on accept");
/* Fork child process to service connection */
if ( (pid = fork()) == 0 ) {
/* This is now the forked child process, so
close listening socket and service request */
if ( close(sock) < 0 )
perror("Error on close in child");
Service_Request(conn);
/* Close connected socket and exit */
if ( close(conn) < 0 )
perror("Error on close");
exit(EXIT_SUCCESS);
}
/* If we get here, we are still in the parent process,
so close the connected socket, clean up child processes,
and go back to accept a new connection. */
if ( close(conn) < 0 )
perror("Error on close in parent");
waitpid(-1, NULL, WNOHANG);
}
return EXIT_FAILURE; /* We shouldn't get here */
}
and this is a simple bluetooth server, which has a send and receive function,
Code:
//rfcomm Server
#include <stdio.h>
#include <unistd.h>
#include <sys/socket.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/rfcomm.h>
#include <stdlib.h>
#include "server.h"
char buffer[1024] = { 0 };
int bt_recv()
{
while(1)
{
struct sockaddr_rc BASE_addr = { 0 }, CORE_addr = { 0 };
//variable deceleration
int sock;
int conn;
int bytes_read;
socklen_t opt = sizeof(CORE_addr);
// Socket Allocation
sock = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
// Socket Binding
BASE_addr.rc_family = AF_BLUETOOTH;
BASE_addr.rc_bdaddr = *BDADDR_ANY;
BASE_addr.rc_channel = (uint8_t) 1;
bind(sock, (struct sockaddr *)&BASE_addr, sizeof(BASE_addr));
// Socket Listening
listen(sock, 1);
// Socket Accpeting
conn = accept(sock, (struct sockaddr *)&CORE_addr, &opt);
ba2str( &CORE_addr.rc_bdaddr, buffer );
fprintf(stderr, "accepted connection from %s\n", buffer);
memset(buffer, 0, sizeof(buffer));
// read data after connection is established
bytes_read = read(conn, buffer, sizeof(buffer));
if( bytes_read > 0 ) {
printf("Received Command: %s \n", buffer);
printf("Command Logged!\n");
logger();
}
// Socket Close
close(conn);
close(sock);
return 0;
}
}
//Logging Functions
int logger()
{
FILE *file;
file = fopen("file.txt","a+"); // apend file (add text to a file or create a file if it does not exist.
//writes the "buffer" to the txt file
fprintf(file,"# Command = %s\n", buffer );
fclose(file);
return 0;
}
//send function?
int bt_send(const char *COMMAND)
{
struct sockaddr_rc addr = { 0 };
int sock; //as in socket
int conn;
//since Core's MAC address is always fixed, its hard coded.
char mac_address[18] = "00:03:7A:AA:92:DE"; //Core's MAC Address - Change if needed
//00:1F:5C:E4:7F:34 E66
//00:19:7E:F9:3B:92 Roy PC
//00:03:7A:AA:92:DE Toshiba
// Scoket Allocation
sock = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
// set the connection parameters (who to connect to)
addr.rc_family = AF_BLUETOOTH;
addr.rc_channel = (uint8_t) 1;
str2ba( mac_address, &addr.rc_bdaddr );
// connect to Core
conn = connect(sock, (struct sockaddr *)&addr, sizeof(addr));
// send message - Write a message on the socket (sock)
//payload should be applied here
if( conn == 0 ) {
conn = write(sock, COMMAND, 6);
}
//error handling
if( conn < 0 ) perror("Error On Sending Message");
//Close Socket after sending
close(sock);
return 0;
}
i need to have this bt_recv function running at the same time that i run the http server. i believe i need to use a thread for recv function, so both http and recv could process parallely. can anyone help me with this? an example code would be really nice
thanks in advance