Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hello i have a several question about make Single thread.. - sensor-service-st.c

ID: 3767495 • Letter: H

Question

Hello i have a several question about make Single thread..

- 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
- sensor-client-st.c
- Connect to the specified TCP network port on localhost (0300c-tiger server).
- Send 3 random sensor data blocks of 37 bytes each.
- Close the connection
- Client is single threaded
- General notes
- Only required target is x86 64-bit platform on 0300c-tiger
- Store data in little endian format (x86 native)
- Transmit data in little endian format (x86 native)

this is requirement of making code. I made code like this

service one

/ 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>

// 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);
}

// 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 = 0, sock = -1, cli_sock = -1;
struct sockaddr_in addr, cli_addr;
socklen_t cli_addr_len = 0;
port = 43999;
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0) err("Cannot create server socket");
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl(INADDR_ANY);
addr.sin_port = htons(port);
if(bind(sock, (struct sockaddr*)&addr, sizeof(addr)) < 0)
err("Cannot bind server socket");
if(listen(sock, 10) < 0)
err("Cannot listen");
int acceptMore = 1;
while(acceptMore)
{
int buf_len = 1024;
char* buf[1024];
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");
bytesRead = read(cli_sock, buf, buf_len);
while(bytesRead > 0)
{
printf("Client read bytesRead=%d ", bytesRead);
bytesRead = read(cli_sock, buf, buf_len);
}
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;
}

client one

/ compile: gcc -std=c99 -DENDIAN_LITTLE 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 <netdb.h>
#include <errno.h>
#include <strings.h>
#include <string.h>

// 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);
}

// 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 argc, char *argv[])
{
int port = 0, sock = -1;
int bytesWritten;
struct sockaddr_in srv_addr;
struct hostent *srv_he = NULL;
char *srv_name = NULL;
int buf_len = 1024;
char buf[1024];
char *msg = NULL;
int msg_len = 0;

msg = "Hello";
msg_len = strlen(msg) + 1;
srv_name = "localhost";
port = 43999;
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0) err("Cannot create socket");
srv_he = gethostbyname(srv_name);
if (srv_he == NULL) err("Cannot get server host by name");
memset(&srv_addr, 0, sizeof(srv_addr));
srv_addr.sin_family = AF_INET;
memcpy(&srv_addr.sin_addr.s_addr, srv_he->h_addr_list[0], srv_he->h_length);
srv_addr.sin_port = htons(port);
if(connect(sock,(struct sockaddr*)&srv_addr,sizeof(srv_addr)) < 0)
err("Cannot connect");

strcpy(buf, msg);
void* buf_pos = buf;
int buf_remain = msg_len;
while(buf_remain > 0)
{
bytesWritten = write(sock, buf_pos, buf_remain);
if(bytesWritten < 0) err("Server write failed");
printf("Client write bytesWritten=%d ", bytesWritten);
buf_pos = buf_pos + bytesWritten;
buf_remain = buf_remain - bytesWritten;
}
printf("Client done ");
shutdown(sock, SHUT_RDWR);
close(sock);
return 0;
}

Explanation / Answer


//code fro client
#include <stdio.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include <string.h>
int main(void)
{
struct sockaddr_in ser_address;
int socket_fd, mbytes;
char buffer[256];
socket_fd = socket(PF_UNIX, SOCK_STREAM, 0);
if(socket_fd < 0)
{
printf("socket() connection is failed ");
return 1;
}
  
memset(&address, 0, sizeof(struct sockaddr_in)); //here start with clean address
address.sun_family = AF_UNIX;
snprintf(address.sun_path, UNIX_PATH_MAX, "./exam_socket");
if(connect(socket_fd,
(struct sockaddr *) &address,
sizeof(struct sockaddr_in)) != 0)
{
printf("the connect() is failed ");
return 1;
}
mbytes = snprintf(buffer, 256, "hello from a client");
write(socket_fd, buffer, mbytes);
nbytes = read(socket_fd, buffer, 256);
buffer[mbytes] = 0;
printf("MESSAGE FROM SERVER: %s ", buffer);
close(socket_fd);
return 0;
}


//code for server

#include <stdio.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
int connection_handler(int connection_fd)
{
int mbytes;
char buffer[256];
nbytes = read(connection_fd, buffer, 256);
buffer[mbytes] = 0;
printf("MESSAGE FROM CLIENT: %s ", buffer);
mbytes = snprintf(buffer, 256, "hello from the server");
write(connection_fd, buffer, mbytes);
close(connection_fd);
return 0;
}
int main(void)
{
struct sockaddr_in ser_address;
int socket_fd, connection_fd;
socklen_t address_length;
pid_t child;
socket_fd = socket(PF_UNIX, SOCK_STREAM, 0);
if(socket_fd < 0)
{
printf("socket() failed ");
return 1;
}
unlink("./demo_socket");
  
memset(&address, 0, sizeof(struct sockaddr_in));
address.sun_family = AF_UNIX;
snprintf(address.sun_path, UNIX_PATH_MAX, "./demo_socket");
if(bind(socket_fd,
(struct sockaddr *) &address,
sizeof(struct sockaddr_in)) != 0)
{
printf("bind() failed ");
return 1;
}
if(listen(socket_fd, 5) != 0)
{
printf("listen() failed ");
return 1;
}
while((connection_fd = accept(socket_fd,
(struct sockaddr *) &address,
&address_length)) > -1)
{
child = fork();
if(child == 0)
{
i
return connection_handler(connection_fd);
}
i
close(connection_fd);
}
close(socket_fd);
unlink("./demo_socket");
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote