Hello i\'m trying to make a code. I\'m using C right now. But i don\'t know how
ID: 3768903 • Letter: H
Question
Hello i'm trying to make a code. I'm using C right now. But i don't know how to attach this code excatly.
Here is my assignment said.
- Name: sensor-service-st.c
- Listen on the specified TCP network port for incoming connections
- On each accepted connection, read blocks of 37 bytes at a time
and write the information corresponding to a sensor reading to sensor.dat,
closing the connection when the client is done (when read()
returns 0 or -1)
- The server is single threaded, so when a connection is accepted no other
connections will be serviced until the communication with the current
client is done
And here is my code
// compile: gcc -std=c99 prog.c -o prog
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdint.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <errno.h>
#include <strings.h>
#include <string.h>
#include <pthread.h>
#include <semaphore.h>
#include <sys/time.h>
pthread_t thr, thr2;
pthread_ thrs[5];
pthread_ *thd = NULL;
pthread_mutex_t mut;
// Quit on error
void errs(char *msg)
{
fprintf(stderr,"ERROR: %s ", msg);
exit(1);
}
// Quit on error showing errno
void err(char *msg)
{
char *errstr = strerror(errno);
fprintf(stderr,"ERROR: %s errno:%d strerror:%s ", msg, errno, errstr);
exit(1);
}
int ct = 0;
void* worker(void *p)
{
pthread_t t;
char *str = (char*)p;
t = pthread_self();
for(size_t i = 0; i < 1*1000*1000; i++)
{
//sem_wait(&sem);
// do semaphore limited group stuff here
//ct++;
//sem_post(&sem);
pthread_mutex_lock(&mut);
// do critical stuff here
ct++;
pthread_mutex_unlock(&mut);
}
printf("Thread id: %lu ", (unsigned long)t);
printf("tid %lu param: %s ", (unsigned long)t, str);
for(int i = 0; i < 0*5; i++)
{
printf("tid %lu count: %d ", (unsigned long)t, i);
sleep(1);
}
return NULL;
}
// Convert little endian to big endian
uint32_t flip32(uint32_t i)
{
uint32_t r = 0;
uint32_t r1 = 0, r2 = 0, r3 = 0, r4 = 0;
r1 = (0xFF000000 & i) >> 24;
r2 = (0x00FF0000 & i) >> 8;
r3 = (0x0000FF00 & i) << 8;
r4 = (0x000000FF & i) << 24;
//r = r1 + r2 + r3 + r4;
r = r1 | r2 | r3 | r4;
return r;
}
double flip_double(double d)
{
double r;
char *dp = (char*)&d;
char *rp = (char*)&r;
rp[0] = dp[7];
rp[1] = dp[6];
rp[2] = dp[5];
rp[3] = dp[4];
rp[4] = dp[3];
rp[5] = dp[2];
rp[6] = dp[1];
rp[7] = dp[0];
return r;
}
int main()
{
int port = 43443;
int sock = -1;
int cli_sock = -1;
struct sockaddr_in addr;
struct sockaddr_in cli_addr;
socklen_t cli_addr_len = 0;
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0) err("Cannot create server socket");
memset(&addr, 0, sizeof(addr)); // pointer add - 0 - sizeof(add) times
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl(INADDR_ANY); // accept any
addr.sin_port = htons(port); // host to network short
if(bind(sock, (struct sockaddr*)&addr, sizeof(addr)) < 0) // socket - pointer add bind
err("Cannot bind server socket");
if(listen(sock, 10) < 0)
err("Cannot listen");
int acceptMore = 1;
while(acceptMore) // while loop for many users
{
soil_data_packed_t s;
int buf_len = sizeof(s);
void* buf = &s;
void* buf_pos = buf;
int buf_remain = buf_len;
int bytesRead;
cli_addr_len = sizeof(cli_addr);
cli_sock = accept(sock, (struct sockaddr *)&cli_addr, &cli_addr_len);
printf("Client accepted ");
if (cli_sock < 0) err("Cannot accept client socket");
while(buf_remain > 0)
{
bytesRead = read(cli_sock, buf_pos, buf_remain);
if(bytesRead < 0) err("Client read failed");
printf("Client read bytesRead=%d ", bytesRead);
buf_pos = buf_pos + bytesRead;
buf_remain = buf_remain - bytesRead;
}
// By now, have 37 bytes read from client into s
printf("soil_moist_raw=%d ", s.soil_moist_raw);
printf("timestamp=%ld ", s.timestamp);
printf("%d ", s);
int f;
f = open("sensor.dat", O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR); // O_WRONLY
if(f != -1)
{
printf("File opened for write ");
int i;
{
int numWritten;
soil_data_packed_t s;
numWritten = write(f,&s, sizeof(s));
printf("%d ", numWritten);
}
close(f);
}
else
{
printf("Cannot open file ");
}
}
if(bytesRead < 0) err("Client read failed");
printf("Client done ");
shutdown(cli_sock, SHUT_RDWR);
printf("Client shutdown ");
close(cli_sock);
printf("Client close ");
}
return 0;
}
And here is my code that i have to attach to upside one.
pthread_mutex_init(&mut, NULL);
sem_init(&sem, 0, 4);
soil_data_packed_t sd;
sd.soil_moist_raw = 200;
//printf("Hello ");
printf("sizeof(sd.soil_moist_raw) = %zu ", sizeof(sd.soil_moist_raw));
printf("sizeof(sd.timestamp) = %zu ", sizeof(sd.timestamp));
printf("sizeof(sd) = %zu ", sizeof(sd));
printf("Before thread ");
char *s = "Hello";
//pthread_create(&thr, NULL, worker, s);
//pthread_create(&thr2, NULL, worker, s);
//pthread_join(thr, NULL);
//pthread_join(thr2, NULL);
/*
for(int i = 0; i < 5; i++)
{
pthread_create(&thrs[i], NULL, worker, s);
}
for(int i = 0; i < 5; i++)
{
pthread_join(thrs[i], NULL);
}
*/
pthread_mutex_lock(&mut);
// do critical stuff here
pthread_mutex_unlock(&mut);
sem_wait(&sem);
// do semaphore limited group stuff here
sem_post(&sem);
int num_threads = 50;
thd = malloc(num_threads * sizeof(pthread_t));
for(int i = 0; i < num_threads; i++)
{
pthread_create(&thd[i], NULL, worker, s);
}
for(int i = 0; i < num_threads; i++)
{
pthread_join(thd[i], NULL);
}
free(thd);
printf("ct = %d ", ct);
I don't know where do i have to put that one.
Explanation / Answer
********* I highlighted the code ************************
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdint.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <errno.h>
#include <strings.h>
#include <string.h>
#include <pthread.h>
#include <semaphore.h>
#include <sys/time.h>
pthread_t thr, thr2;
pthread_ thrs[5];
pthread_ *thd = NULL;
pthread_mutex_t mut;
// Quit on error
void errs(char *msg)
{
fprintf(stderr,"ERROR: %sn", msg);
exit(1);
}
// Quit on error showing errno
void err(char *msg)
{
char *errstr = strerror(errno);
fprintf(stderr,"ERROR: %s errno:%d strerror:%sn", msg, errno, errstr);
exit(1);
}
int ct = 0;
void* worker(void *p)
{
pthread_t t;
char *str = (char*)p;
t = pthread_self();
for(size_t i = 0; i < 1*1000*1000; i++)
{
//sem_wait(&sem);
// do semaphore limited group stuff here
//ct++;
//sem_post(&sem);
pthread_mutex_lock(&mut);
// do critical stuff here
ct++;
pthread_mutex_unlock(&mut);
}
printf("Thread id: %lun", (unsigned long)t);
printf("tid %lu param: %sn", (unsigned long)t, str);
for(int i = 0; i < 0*5; i++)
{
printf("tid %lu count: %dn", (unsigned long)t, i);
sleep(1);
}
return NULL;
}
// Convert little endian to big endian
uint32_t flip32(uint32_t i)
{
uint32_t r = 0;
uint32_t r1 = 0, r2 = 0, r3 = 0, r4 = 0;
r1 = (0xFF000000 & i) >> 24;
r2 = (0x00FF0000 & i) >> 8;
r3 = (0x0000FF00 & i) << 8;
r4 = (0x000000FF & i) << 24;
//r = r1 + r2 + r3 + r4;
r = r1 | r2 | r3 | r4;
return r;
}
double flip_double(double d)
{
double r;
char *dp = (char*)&d;
char *rp = (char*)&r;
rp[0] = dp[7];
rp[1] = dp[6];
rp[2] = dp[5];
rp[3] = dp[4];
rp[4] = dp[3];
rp[5] = dp[2];
rp[6] = dp[1];
rp[7] = dp[0];
return r;
}
int main()
{
int port = 43443;
int sock = -1;
int cli_sock = -1;
struct sockaddr_in addr;
struct sockaddr_in cli_addr;
socklen_t cli_addr_len = 0;
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0) err("Cannot create server socket");
memset(&addr, 0, sizeof(addr)); // pointer add - 0 - sizeof(add) times
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl(INADDR_ANY); // accept any
addr.sin_port = htons(port); // host to network short
if(bind(sock, (struct sockaddr*)&addr, sizeof(addr)) < 0) // socket - pointer add bind
err("Cannot bind server socket");
if(listen(sock, 10) < 0)
err("Cannot listen");
int acceptMore = 1;
while(acceptMore) // while loop for many users
{
soil_data_packed_t s;
int buf_len = sizeof(s);
void* buf = &s;
void* buf_pos = buf;
int buf_remain = buf_len;
int bytesRead;
cli_addr_len = sizeof(cli_addr);
cli_sock = accept(sock, (struct sockaddr *)&cli_addr, &cli_addr_len);
printf("Client acceptedn");
if (cli_sock < 0) err("Cannot accept client socket");
while(buf_remain > 0)
{
bytesRead = read(cli_sock, buf_pos, buf_remain);
if(bytesRead < 0) err("Client read failed");
printf("Client read bytesRead=%dn", bytesRead);
buf_pos = buf_pos + bytesRead;
buf_remain = buf_remain - bytesRead;
}
// By now, have 37 bytes read from client into s
printf("soil_moist_raw=%dn", s.soil_moist_raw);
printf("timestamp=%ldn", s.timestamp);
printf("%dn", s);
int f;
f = open("sensor.dat", O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR); // O_WRONLY
if(f != -1)
{
printf("File opened for writen");
int i;
{
int numWritten;
soil_data_packed_t s;
numWritten = write(f,&s, sizeof(s));
printf("%dn", numWritten);
}
close(f);
}
else
{
printf("Cannot open filen");
}
pthread_mutex_init(&mut, NULL);
sem_init(&sem, 0, 4);
soil_data_packed_t sd;
sd.soil_moist_raw = 200;
//printf("Hello ");
printf("sizeof(sd.soil_moist_raw) = %zu ", sizeof(sd.soil_moist_raw));
printf("sizeof(sd.timestamp) = %zu ", sizeof(sd.timestamp));
printf("sizeof(sd) = %zu ", sizeof(sd));
printf("Before thread ");
char *s = "Hello";
//pthread_create(&thr, NULL, worker, s);
//pthread_create(&thr2, NULL, worker, s);
//pthread_join(thr, NULL);
//pthread_join(thr2, NULL);
/*
for(int i = 0; i < 5; i++)
{
pthread_create(&thrs[i], NULL, worker, s);
}
for(int i = 0; i < 5; i++)
{
pthread_join(thrs[i], NULL);
}
*/
pthread_mutex_lock(&mut);
// do critical stuff here
pthread_mutex_unlock(&mut);
sem_wait(&sem);
// do semaphore limited group stuff here
sem_post(&sem);
int num_threads = 50;
thd = malloc(num_threads * sizeof(pthread_t));
for(int i = 0; i < num_threads; i++)
{
pthread_create(&thd[i], NULL, worker, s);
}
for(int i = 0; i < num_threads; i++)
{
pthread_join(thd[i], NULL);
}
free(thd);
printf("ct = %d ", ct);
}
if(bytesRead < 0) err("Client read failed");
printf("Client donen");
shutdown(cli_sock, SHUT_RDWR);
printf("Client shutdownn");
close(cli_sock);
printf("Client closen");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.