Question

C programming help! /* Your challenge is to format the following code in a readable manner, anwsering the questions in...

C programming help!

/* Your challenge is to format the following code in a readable manner, anwsering the questions in the comments.
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <time.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <strings.h>
#define BUFFERT 512
#define BACKLOG 1
int create_server_socket (int port);
struct sockaddr_in sock_serv,sock_clt;
int main(int argc, char** argv){
    int sfd, fd;
    unsigned int length = sizeof(struct sockaddr_in);
    long int n, m, count = 0;
    unsigned int nsid;
    ushort clt_port;
    char buffer[BUFFERT], filename[256];
    char dst[INET_ADDRSTRLEN];
        time_t intps;
        struct tm* tmi;


    /* (A) repalce the following line with suitable getopt() functionality */
    if(argc!=2) {perror("Usage ./a.out <num_port> <file2send>\n");exit(3);}


    int l;
    int yes=1;


    /* The following function call should use; Internet version 4 protocols with a sequenced, reliable, two-way connection based byte streams */
        sfd = socket( /* Q) What goes here?  */ , /* Q) What goes here? */ , 0);
    /* See; man socket */


        if (sfd == -1) {perror("socket fail");return EXIT_SUCCESS;}
    if(setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR,&yes,sizeof(int)) == -1 ) {perror("setsockopt erreur");exit(5);}
        l = sizeof(struct sockaddr_in);
        bzero(&sock_serv,l);

    /* The following assignment should use; Internet version 4 protocols constant */
        sock_serv.sin_family=/* Q) What goes here? */;
    /* See; man socket */

        sock_serv.sin_port = htons(atoi(argv[1]));

    /* Set the IP address to allow connections on any network interface on the server */
        sock_serv.sin_addr.s_addr = htonl(/* Q) What goes here? */);
    /* See; man 4 ip */
        
    if (bind(sfd,(struct sockaddr*)&sock_serv,l) == -1) {perror("bind fail"); exit(EXIT_FAILURE);}
    bzero(buffer,BUFFERT);
    listen(sfd,BACKLOG);

    /* accept() takes "struct sockaddr_in" and returns non-negative integer that is a descriptor for the accepted socket.
     * The missing arguement in the line below will need to have the *address* of the variable required, as the value is updated when the socket is update. eg, &variable
     */
    nsid = accept(sfd, (struct sockaddr*)&sock_clt, /* Q) What goes here?  */);
    /* See; man 2 accept */


    if (nsid == -1) {perror("accept fail"); return EXIT_FAILURE;} 
    else {
        if(inet_ntop(AF_INET,&sock_clt.sin_addr,dst,INET_ADDRSTRLEN)==NULL) {perror("erreur socket");exit (4);}
        clt_port=ntohs(sock_clt.sin_port);
        printf("connection : %s:%d\n",dst,clt_port);
        bzero(filename,256);
        intps = time(NULL);
        tmi = localtime(&intps);
        bzero(filename,256);
        sprintf(filename,"saved-file.%d.%d.%d.%d.%d.%d", tmi->tm_mday, tmi->tm_mon+1, 1900+tmi->tm_year, tmi->tm_hour, tmi->tm_min, tmi->tm_sec);
        printf("Creating the copied output file : %s\n", filename);
        if ((fd=open(filename,O_CREAT|O_WRONLY,0600))==-1) {perror("open fail"); exit (3);}
        bzero(buffer,BUFFERT);
        n=recv(nsid,buffer,BUFFERT,0);
        while(n) {
            if(n==-1) {perror("recv fail");exit(5);}
            if((m=write(fd,buffer,n))==-1) {perror("write fail");exit (6);}
            count=count+m;
            bzero(buffer,BUFFERT);
            n=recv(nsid,buffer,BUFFERT,0);
        }
        close(sfd);
        close(fd);
    }
    close(nsid);
    printf("End of transmission with %s.%d\n",dst,clt_port);

    /* Select the appropriate variable in the following function call argument */
    printf("Number of octets received : %ld \n", /* Q) What goes here? */);    
    return EXIT_SUCCESS;
}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

The readable format is shown below with answers of questions given

#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <time.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <strings.h>
#define BUFFERT 512
#define BACKLOG 1

int create_server_socket (int port);
struct sockaddr_in sock_serv,sock_clt;

int main(int argc, char** argv)
{
int sfd, fd;
unsigned int length = sizeof(struct sockaddr_in);
long int n, m, count = 0;
unsigned int nsid;
ushort clt_port;
char buffer[BUFFERT];
char filename[256];
char dst[INET_ADDRSTRLEN];
time_t intps;
struct tm* tmi;


/* (A) repalce the following line with suitable getopt() functionality */
if(argc!=2)
{
perror("Usage ./a.out <num_port> <file2send>\n");
exit(3);
}

int l;
int yes=1;


/* The following function call should use; Internet version 4 protocols with a sequenced, reliable, two-way connection based byte streams */
sfd = socket( /* Q) What goes here?-A) PF-INET*/ , /* Q) What goes here?-A) SOCK_STREAM */ , 0);
/* See; man socket */


if (sfd == -1)
{
perror("socket fail");
return EXIT_SUCCESS;
}
  
if(setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR,&yes,sizeof(int)) == -1 )
{
perror("setsockopt erreur");
exit(5);
}
l = sizeof(struct sockaddr_in);
bzero(&sock_serv,l);

/* The following assignment should use; Internet version 4 protocols constant */
sock_serv.sin_family=/* Q) What goes here?-A) PF-INET //for ipv4 // but for constant use AF-INET*/;
/* See; man socket */

sock_serv.sin_port = htons(atoi(argv[1]));

/* Set the IP address to allow connections on any network interface on the server */
sock_serv.sin_addr.s_addr = htonl(/* Q) What goes here? -A)(((((192 << 8) | 43) << 8) | 244) << 8) | 18 //to convert long*/);
/* See; man 4 ip */
  
if (bind(sfd,(struct sockaddr*)&sock_serv,l) == -1)
{
perror("bind fail"); exit(EXIT_FAILURE);
}
bzero(buffer,BUFFERT);
listen(sfd,BACKLOG);

/* accept() takes "struct sockaddr_in" and returns non-negative integer that is a descriptor for the accepted socket.
* The missing arguement in the line below will need to have the *address* of the variable required, as the value is updated when the socket is update. eg, &variable
*/
nsid = accept(sfd, (struct sockaddr*)&sock_clt, /* Q) What goes here?-A) socketlen_t *addrlen */);
/* See; man 2 accept */


if (nsid == -1)
{
perror("accept fail"); return EXIT_FAILURE;
}
else
{
if(inet_ntop(AF_INET,&sock_clt.sin_addr,dst,INET_ADDRSTRLEN)==NULL)
{
perror("erreur socket");exit (4);
}
clt_port=ntohs(sock_clt.sin_port);
printf("connection : %s:%d\n",dst,clt_port);
bzero(filename,256);
intps = time(NULL);
tmi = localtime(&intps);
bzero(filename,256);
sprintf(filename,"saved-file.%d.%d.%d.%d.%d.%d", tmi->tm_mday, tmi->tm_mon+1, 1900+tmi->tm_year, tmi->tm_hour, tmi->tm_min, tmi->tm_sec);
printf("Creating the copied output file : %s\n", filename);
if ((fd=open(filename,O_CREAT|O_WRONLY,0600))==-1)
{
perror("open fail"); exit (3);
}
bzero(buffer,BUFFERT);
n=recv(nsid,buffer,BUFFERT,0);
while(n)
{
if(n==-1)
{
perror("recv fail");
exit(5);
}
if((m=write(fd,buffer,n))==-1)
{
perror("write fail");
exit (6);
}
count=count+m;
bzero(buffer,BUFFERT);
n=recv(nsid,buffer,BUFFERT,0);
}
close(sfd);
close(fd);
}
close(nsid);
printf("End of transmission with %s.%d\n",dst,clt_port);

/* Select the appropriate variable in the following function call argument */
printf("Number of sockets received : %ld \n", /* Q) What goes here? -A)nsid // number of accepted sockets*/);
return EXIT_SUCCESS;
}

Add a comment
Know the answer?
Add Answer to:
C programming help! /* Your challenge is to format the following code in a readable manner, anwsering the questions in...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Modify the client server system program given below so that instead of sendto() and recvfrom(), you...

    Modify the client server system program given below so that instead of sendto() and recvfrom(), you use connect() and un-addresssed write() and read() calls. //Server.c #include #include #include #include #include #include #include #include #include #include # define PortNo 4567 # define BUFFER 1024 int main(int argc, char ** argv) { int ssd; int n; socklen_t len; char msg[BUFFER]; char clientmsg[BUFFER]; struct sockaddr_in server; struct sockaddr_in client; int max_iterations = 0; int count = 0, totalChar = 0, i = 0;...

  • Run the code in Linux and provide the screenshot of the output and input #include <signal.h>...

    Run the code in Linux and provide the screenshot of the output and input #include <signal.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <arpa/inet.h> #include <sys/types.h> #include <sys/socket.h> static void cleanup(); static void docleanup(int signum); static const char *SERVER_ADDR = "127.0.0.1"; static const int SERVER_PORT = 61234; static int cfd = -1; int main(int argc, char *argv[]) { struct sockaddr_in saddr; char buf[128]; int bufsize = 128, bytesread; struct sigaction sigact; printf("client starts running ...\n"); atexit(cleanup); sigact.sa_handler =...

  • For Unix in a C/C++ environment echoServer and echoClient are provided below In this lab, we will...

    For Unix in a C/C++ environment echoServer and echoClient are provided below In this lab, we will modifiy echoServer.c and echoClient.c programs (for the server's port# not fixed). (1) Modify echoServer.c program to take one argument (a port number) to be used for its listening port when it starts. (2) Modify echoClient.c program to take two arguments (server's IP address and Port number) to be used for its connection. (3) Find a port free for the server using netstat (see...

  • IN UNIX, MODIFY CODE, PROVIDE SCREENSHOTS FOR GOOD RATING: T1. Modify Client.c program to accept two...

    IN UNIX, MODIFY CODE, PROVIDE SCREENSHOTS FOR GOOD RATING: T1. Modify Client.c program to accept two arguments (IP add & port no. of the concurrent Server with thread - conServThread.c). Similarly, modify the Server (conServThread.c) program to accept an argument which is the port number of the server to bind and listen to. Try these two updated programs (server and client) with a port number (e.g., hhmm6) with current time where hh is hours in 24-hour format and mm is...

  • /* myloggerd.c * Source file for thread-lab * Creates a server to log messages sent from...

    /* myloggerd.c * Source file for thread-lab * Creates a server to log messages sent from various connections * in real time. * * Student: */ #include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <unistd.h> #include <fcntl.h> #include <sys/socket.h> #include <sys/un.h> #include <stdlib.h> #include <pthread.h> #include "message-lib.h" // forward declarations int usage( char name[] ); // a function to be executed by each thread void * recv_log_msgs( void * arg ); // globals int log_fd; // opened by main() but accessible...

  • Assume I don't understand C++ Can someone explain this program to me Line by Line? Basically...

    Assume I don't understand C++ Can someone explain this program to me Line by Line? Basically what each line actually does? whats the function? whats the point? Don't tell me what the program does as a whole, I need to understand what each line does in this program. #include #include #include #include #include #define SERVER_PORT 5432 #define MAX_LINE 256 int main(int argc, char * argv[]) {    FILE *fp;    struct hostent *hp;    struct sockaddr_in sin;    char *host;...

  • I need help fixing this code in C. It keeps giving me an error saying error:...

    I need help fixing this code in C. It keeps giving me an error saying error: ‘O_RDWD’ undeclared (first use in this function) if ((fd = open(fifo, O_RDWD)) < 0) note: each undeclared identifier is reported only once for each function it appears in. Please fix so it compiles properly. //************************************************************************************************************************************************** #include <fcntl.h> #include <stdio.h> #include <errno.h> #include<stdlib.h> #include <string.h> #include<unistd.h> #include <sys/stat.h> #include <sys/types.h> #define MSGSIZE 63 char *fifo = "fifo"; int main(int argc, char **argv){    int fd;...

  • Directions: use only the signal mechanism system calls don’t use ( wait() or pipe() )in this...

    Directions: use only the signal mechanism system calls don’t use ( wait() or pipe() )in this problem. You can still read/write from/to a file. You must use ( kill() and pause() ) system calls. rewrite code below to do kill and pause system calls #include <fcntl.h> #include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/wait.h> // Function ptototypes int readX(); void writeX(int); int main() { int pid; // pid: used to keep track of the child process int x...

  • GIVEN CODE- FILL IN THE BLANK! #include <fcntl.h> #include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <string.h>...

    GIVEN CODE- FILL IN THE BLANK! #include <fcntl.h> #include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/wait.h> // Function ptototypes int readX(); void writeX(int); int main() /// chi read x ---> divi ---> write x into file ---> par read x --> sub--> write x into file---> chi read x-->etc {    int pid;           // pid: used to keep track of the child process    int x = 19530;       // x: our value as integer   ...

  • In this activity, you will complete the RSS Client that connects to a UNL RSS feed,...

    In this activity, you will complete the RSS Client that connects to a UNL RSS feed, processes the XML data and outputs the results to the standard output. Most of the client has been completed for you. You just Lab Handout: Structures 4 need to complete the design and implementation of a C structure that models the essential parts of an RSS item. Your structure will need to support an RSS item’s title, link, description, and publication date. As a...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT