Concurrent Multithreading Server (1 server, 3 clients) program using sqlite3 in C

Ditutup Dipasang Dec 8, 2015 Dibayar saat pengiriman
Ditutup Dibayar saat pengiriman

Part 1

Design and implement a concurrent server (concurrent server using fork) for the main store. The server is up and ready to service the incoming request from the branch office via Internet. The request from the branch office's client program is a simple SQL query (for example, "select * from artist;") to be run and to send its result back to the branch store's client program. The server will keep the log file (a simple file) of each incoming request (transaction) from the client and its result being sent back to the client, with the timestamp. For part 1, make it work for 1 server and 1 client.

Part 2

Design and implement a client program for the branch store to connect the server of the main store, to send a SQL query and receive the result from the server, and output it to a file (or to the console, and to log both the request and result with timestamp). The client will send a few simple SQL query requests, one at a time. For example,

select * from artist;

select * from cd;

select * from track;

Bonus - Part 3 (1 concurrent server with threads, and 3 clients)

For each client connection, the server will make a thread to service each client (each session) at a time. Since the log file should be shared among potentially many servicing threads of the server, you need to provide a mutually-exclusive file access (lock/unlock using mutex) for the file which is open once by the server initially when it is started. When the log file is locked or unlocked by a thread, each thread should generate a message to the console. Create a test scenario (or to allow each client connected enough time) to test the concurrency and synchronization.

NOTE **[url removed, login to view] records are already populated with 3 tables (artist, cd, track)**

// sample programs for your use (socket programs)
// this is a sample client code to use

#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <arpa/inet.h>

#define MAXLINE 4096 /*max text line length*/
#define SERV_PORT 3000 /* server port – you need to change this */

int main(int argc, char **argv) // the argument is the server's IP address
{
int sockfd;
struct sockaddr_in servaddr;
char sendline[MAXLINE], recvline[MAXLINE];

//basic check of the arguments
//additional checks can be inserted
if (argc !=2) {
perror("Usage: TCPClient <IP address of the server.");
exit(1);
}

//Create a socket for the client
//If sockfd<0 there was an error in the creation of the socket
if ((sockfd = socket (AF_INET, SOCK_STREAM, 0)) <0) {
perror("Problem in creating the socket");
exit(2);
}

//Creation of the socket
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr= inet_addr(argv[1]);
servaddr.sin_port = htons(SERV_PORT); //convert to big-endian order

//Connection of the client to the socket
if (connect(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr))<0) {
perror("Problem in connecting to the server");
exit(3);
}

while (fgets(sendline, MAXLINE, stdin) != NULL) {

send(sockfd, sendline, strlen(sendline), 0);

if (recv(sockfd, recvline, MAXLINE,0) == 0){
//error: server terminated prematurely
perror("The server terminated prematurely");
exit(4);
}

// you need to update the code below to do the required work by client
printf("%s", "String received from the server: ");
fputs(recvline, stdout);

}

exit(0);
}




// Sample concurrent server code using fork (process)

#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <unistd.h>

#define MAXLINE 4096 /*max text line length*/
#define SERV_PORT 3000 /* server port – you need to change this */
#define LISTENQ 8 /*maximum number of client connections*/

int main (int argc, char **argv)
{
int listenfd, connfd, n;
pid_t childpid;
socklen_t clilen;
char buf[MAXLINE];
struct sockaddr_in cliaddr, servaddr;

//Create a socket for the soclet
//If sockfd<0 there was an error in the creation of the socket
if ((listenfd = socket (AF_INET, SOCK_STREAM, 0)) <0) {
perror("Problem in creating the socket");
exit(2);
}

//preparation of the socket address
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(SERV_PORT);

//bind the socket
bind (listenfd, (struct sockaddr *) &servaddr, sizeof(servaddr));

//listen to the socket by creating a connection queue, then wait for clients
listen (listenfd, LISTENQ);
printf("%s
","Server running...waiting for connections.");

for ( ; ; ) {
clilen = sizeof(cliaddr);
//accept a connection
connfd = accept (listenfd, (struct sockaddr *) &cliaddr, &clilen);
printf("%s
","Received request...");

if ( (childpid = fork ()) == 0 ) { //if it’s 0, it’s child process
printf ("%s
","Child created for dealing with client requests");
//close listening socket
close (listenfd);

while ( (n = recv(connfd, buf, MAXLINE,0)) > 0) {
printf("%s","String received from and resent to the client:");
puts(buf);
// place your code to do the server part here and send the result.
send(connfd, buf, n, 0);
}
if (n < 0)
printf("%s
", "Read error");
exit(0);
}
close(connfd);
}
}





/* Sample thread program with mutex. It will create 4 threads running
concurrently. Each thread will use mutex to regulate so that only
one thread will do the work with a critical data after lock until unlock.
*/

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>

/* Define globally accessible variables and a mutex */
#define NUMTHRDS 4

pthread_t callThd[NUMTHRDS];
pthread_mutex_t mutexsum; // mutex for lock and unlock by threads


void *dotprod(void *arg)
{

/* Lock a mutex to work exclusively, and unlock it when it is done. */

pthread_mutex_lock (&mutexsum);

/* only one thread is allowed now – add code here */

pthread_mutex_unlock (&mutexsum);
pthread_exit((void*) 0);
}

int main (int argc, char *argv[])
{
long i;
double *a, *b;
void *status;
pthread_attr_t attr;

pthread_mutex_init(&mutexsum, NULL);

/* Create threads to perform the dotproduct */
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);

for(i=0;i<NUMTHRDS;i++)
{
pthread_create(&callThd[i], &attr, dotprod, (void *)i);

}

pthread_attr_destroy(&attr);

/* And wait on the other threads */

for(i=0;i<NUMTHRDS;i++) {
pthread_join(callThd[i], &status);
}

/* After joining, print out the results and cleanup */

pthread_mutex_destroy(&mutexsum);
pthread_exit(NULL);

}

Pemrograman C Pemrograman C++ SQLite

ID Proyek: #9074688

Tentang proyek

8 proposal Proyek online Aktif Jan 14, 2016

8 freelancer rata-rata menawar $113 untuk pekerjaan ini

super2lao

A proposal has not yet been provided

$252 USD dalam 3 hari
(76 Ulasan)
7.1
hbxfnzwpf

I am very proficient in c, c++. I have 16 years c++ developing experience now, and I have worked for 6 years. My work is online game developing, and mainly focus on server side, the language is c++ under linux. I used Lebih banyak

$200 USD dalam 3 hari
(84 Ulasan)
6.5
aleemakhtar1

hi I have done multi threading server using fork before. also have experience in sqlite database. would like to do your task. regards team excel

$30 USD dalam 3 hari
(16 Ulasan)
4.6
mohammedabotalat

Hi, i am c/c++ programmer I worked with network socket programming and threading and IPC and I also linux/Unix system admin and I know python and shell script

$111 USD dalam 4 hari
(3 Ulasan)
1.7
dieutech2

Dear Prospect Hiring Manager. Thank you for giving me a chance to bid on your project. i am a serious bidder here and i have already worked on a similar project before and can deliver as u have mentioned I have c Lebih banyak

$25 USD dalam 1 hari
(0 Ulasan)
0.0
poonaminfo

I am interested in this project. I can do this project within 1 day. But I need 3 days. 1 day for testing and and another day for rework if any issue found in testing. I believe in quality work.

$111 USD dalam 1 hari
(0 Ulasan)
0.0
Onlance

A proposal has not yet been provided

$35 USD dalam 4 hari
(0 Ulasan)
0.0