Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   I need your help with an programm (http://www.go4expert.com/forums/i-help-programm-t25839/)

Epicbeard 19May2011 20:41

I need your help with an programm
 
Hi guys i'm new here and i really need your help. I'm a student in information technology in the 4th semester and i've a big problem. Our main programming language changed from Java to C and i only had a few lectures about C and now we've a homework in Operating System till Monday thats driven me crazy. We're working in teams with two people and my partner's guess is as good as mine.
I've startet with a part of the program from my last homework that is a part of the new on. I don't want that anybody here programs this program for me, i only want to have a few ideas or tips how i can solve this homework.

Here is the task

Objective:
In this project we would like to learn how to create and manage files by use of a client/server stream socket programming (using TCP/IP sockets).The goal of this project is to give you a better understanding on process synchronization and file access protection.
Project definition:
Write a program to cover the following characteristics:
1. Store separate students records including name, StudentID, Date of birth, n course marks (Course1, …Coursen) on the server side.
2. We should have m groups each contains several students (m >= 3).
3. Groups are built based on different studied program (ITTI, INFORMATIK, …)
4. Each group’s records are stored in a separate file.
5. The top student within a group is selected based on the Highest Grade Point Average (GPA).
6. There are at least three groups, three students per group and three course grades per student.
7. Student records should be accessible.
8. The program output should display the top student within a group and among all available groups.

The project implementation should employ one of the following options:
a) Process Synchronization: Concurrent file access issues by different clients should be considered. By using a suitable Synchronization mechanism, managing read and write priority and deadlock problems on critical sections need to be considered.


Code:


#include<stdio.h>
#include<conio.h>
#include<process.h>
#include<string.h>
typedef struct
{
int usn;
char name[25];
int marks1;
int marks2;
int marks3;
}Student;
int search_record(int key_usn,FILE *fp)
{
Student st;
for(;;)
{
fscanf(fp,"%d %s %d %d %d",&st.usn, st.name, &st.marks1, &st.marks2,
&st.marks3);
if (feof(fp))break;
if (key_usn==st.usn)return 1;
}
return 0;
}
 
 
void display_records(FILE *fp,int pass)
{
Student st;
printf("USN\tName\t\t\t Marks1\tMarks2\tMarks3\n");
for(;;)
{
fscanf(fp,"%d %s %d %d %d" ,&st.usn, st.name, &st.marks1, &st.marks2, &st.marks3);
if(feof(fp))break;
if(pass==st.usn)
{
printf("%-5d %-10s",st.usn,st.name);
printf("\t\t");
printf(" %-5d %-5d %-5d",st.marks1,st.marks2,st.marks3);
printf("\n");
}
else
printf("%-5d %-10s\t\t %-5d %-5d %5d\n", st.usn, st.name, st.marks1,
st.marks2, st.marks3);
}
}
void append_record(FILE *fp)
{
Student st;
printf("USN : ");
scanf("%d",&st.usn);
printf("Name : ");
scanf("%s",st.name);
printf("Marks1 : ");
scanf("%d",&st.marks1);
printf("Marks2 : ");
scanf("%d",&st.marks2);
printf("Marks3 : ");
scanf("%d",&st.marks3);
fprintf(fp,"%d %s %d %d %d\n",st.usn,st.name,st.marks1,st.marks2,st.marks3);
 
 
}
int main(void)
{
Student st;
char fname[12];
FILE *fp;
int key_usn;
int key = 0;
int flag;
int(choice);

puts("Name der Datei: ");
scanf("%s",fname);
for(;;)
{
puts("\n\n1.Datensatz einfuegen\n2.Datensatz suchen\n3.Datensatz anzeigen\n4.Exit\n\n\nAuswahl:");
scanf("%d",&choice);
switch(choice)
{
case 1:
fp = fopen(fname,"a+");
if(fp==NULL)
{
puts("Öffnen fehlgeschlagen");
break;
}
append_record(fp);
fclose(fp);
break;
case 2:
fp = fopen(fname,"r");
if(fp==NULL)
{
puts("Öffnen fehlgeschlagen");
break;
}
puts("Eingabe der USN: ");
scanf("%d",&key_usn);
flag = search_record(key_usn,fp);
if (flag==0)
puts("Suche fehlgeschlagen\n");
else
{
   
puts("Suche fehlgeschlagen\n");
fp = fopen(fname,"r");
if(fp==NULL)
{
puts("Öffnen fehlgeschlagen");
break;
}
display_records(fp,key_usn);
}
fclose(fp);
break;
case 3:
fp = fopen(fname,"r");
if(fp==NULL)
{
puts("Öffnen fehlgeschlagen");
break;
}
display_records(fp,key);
fclose(fp);
break;
default:
 
return 0;
 

}
}
}

Client
Code:

    /*** clientprog.c ****/

    /*** a stream socket client demo ***/

    #include <stdio.h>

    #include <stdlib.h>

    #include <unistd.h>

    #include <errno.h>

    #include <string.h>

    #include <netdb.h>

    #include <sys/types.h>

    #include <netinet/in.h>

    #include <sys/socket.h>

   

    // the port client will be connecting to

    #define PORT 3490

    // max number of bytes we can get at once

    #define MAXDATASIZE 300

   

    int main(int argc, char *argv[])

    {

    int sockfd, numbytes;

    char buf[MAXDATASIZE];

    struct hostent *he;

    // connector’s address information

    struct sockaddr_in their_addr;

   

    // if no command line argument supplied

    if(argc != 2)

    {

        fprintf(stderr, "Client-Usage: %s the_client_hostname\n", argv[0]);

        // just exit

        exit(1);

    }

   

    // get the host info

    if((he=gethostbyname(argv[1])) == NULL)

    {

        perror("gethostbyname()");

        exit(1);

    }

    else

        printf("Client-The remote host is: %s\n", argv[1]);

   

    if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)

    {

        perror("socket()");

        exit(1);

    }

    else

        printf("Client-The socket() sockfd is OK...\n");

   

    // host byte order

    their_addr.sin_family = AF_INET;

    // short, network byte order

    printf("Server-Using %s and port %d...\n", argv[1], PORT);

    their_addr.sin_port = htons(PORT);

    their_addr.sin_addr = *((struct in_addr *)he->h_addr);

    // zero the rest of the struct

    memset(&(their_addr.sin_zero), '\0', 8);

   

    if(connect(sockfd, (struct sockaddr *)&their_addr, sizeof(struct sockaddr)) == -1)

    {

        perror("connect()");

        exit(1);

    }

    else

        printf("Client-The connect() is OK...\n");

   

    if((numbytes = recv(sockfd, buf, MAXDATASIZE-1, 0)) == -1)

    {

        perror("recv()");

        exit(1);

    }

    else

        printf("Client-The recv() is OK...\n");

   

    buf[numbytes] = '\0';

    printf("Client-Received: %s", buf);

   

    printf("Client-Closing sockfd\n");

    close(sockfd);

    return 0;

    }

server
Code:

    /* serverprog.c - a stream socket server demo */

    #include <stdio.h>

    #include <stdlib.h>

    #include <unistd.h>

    #include <errno.h>

    #include <string.h>

    #include <sys/types.h>

    #include <sys/socket.h>

    #include <netinet/in.h>

    #include <arpa/inet.h>

    #include <sys/wait.h>

    #include <signal.h>

   

    /* the port users will be connecting to */

    #define MYPORT 3490

    /* how many pending connections queue will hold */

    #define BACKLOG 10

   

    void sigchld_handler(int s)

    {

        while(wait(NULL) > 0);

    }

   

    int main(int argc, char *argv[ ])

    {

    /* listen on sock_fd, new connection on new_fd */

    int sockfd, new_fd;

    /* my address information */

    struct sockaddr_in my_addr;

    /* connector’s address information */

    struct sockaddr_in their_addr;

    int sin_size;

    struct sigaction sa;

    int yes = 1;

   

    if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)

    {

        perror("Server-socket() error lol!");

        exit(1);

    }

    else

    printf("Server-socket() sockfd is OK...\n");

   

    if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1)

    {

        perror("Server-setsockopt() error lol!");

        exit(1);

    }

    else

        printf("Server-setsockopt is OK...\n");

   

    /* host byte order */

    my_addr.sin_family = AF_INET;

    /* short, network byte order */

    my_addr.sin_port = htons(MYPORT);

    /* automatically fill with my IP */

    my_addr.sin_addr.s_addr = INADDR_ANY;

   

    printf("Server-Using %s and port %d...\n", inet_ntoa(my_addr.sin_addr), MYPORT);

   

    /* zero the rest of the struct */

    memset(&(my_addr.sin_zero), '\0', 8);

   

    if(bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1)

    {

        perror("Server-bind() error");

        exit(1);

    }

    else

        printf("Server-bind() is OK...\n");

   

    if(listen(sockfd, BACKLOG) == -1)

    {

        perror("Server-listen() error");

        exit(1);

    }

    printf("Server-listen() is OK...Listening...\n");

   

    /* clean all the dead processes */

    sa.sa_handler = sigchld_handler;

    sigemptyset(&sa.sa_mask);

    sa.sa_flags = SA_RESTART;

   

    if(sigaction(SIGCHLD, &sa, NULL) == -1)

    {

        perror("Server-sigaction() error");

        exit(1);

    }

    else

        printf("Server-sigaction() is OK...\n");

   

    /* accept() loop */

    while(1)

    {

    sin_size = sizeof(struct sockaddr_in);

    if((new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size)) == -1)

    {

        perror("Server-accept() error");

        continue;

    }

    else

        printf("Server-accept() is OK...\n");

    printf("Server-new socket, new_fd is OK...\n");

    printf("Server: Got connection from %s\n", inet_ntoa(their_addr.sin_addr));

   

    /* this is the child process */

    if(!fork())

    {

        /* child doesn’t need the listener */

      close(sockfd);

     

      if(send(new_fd, "Hello Operating System's student!\n", 37, 0) == -1)

            perror("Server-send() error lol!");

      close(new_fd);

      exit(0);

    }

    else

        printf("Server-send is OK...!\n");

   

    /* parent doesn’t need this*/

    close(new_fd);

    printf("Server-new socket, new_fd closed successfully...\n");

    }

    return 0;

    }

The first problem i have is how can i integrate program 1 into the client/server?

xpi0t0s 20May2011 12:01

Re: I need your help with an programm
 
Why do you think you need to do that? The project doesn't call for the client and server to be merged and given the project definition ("...by use of a client/server...") it would seem you need a separate client and server.

But anyway, it's easy enough if you understand the code. Just copy the functions you want from the client to the server (or vice versa) then modify any functions that need modifying in order to call those new functions in the right place. Obviously you can't have duplicate functions, so for instance if there are two foo() functions then you'll need to work out (a) if they are different functions with the same name, in which case just rename the function; (b) if they are identical, in which case delete one, or (c) if they implement similar functionality but slightly differently, in which case you will need to merge the logic. In any event you'll need to understand *fully* the existing code.

My suggestions are (a) that you focus on what the project asks for without adding in your own extra tasks (maybe do them later for extra bonus marks, but if you just do the project on its own, well, you should get 10/10); (b) take an overview of the project requirements but when it comes to implementation make sure you write just a few lines at a time before compiling it and making sure it works correctly.

Epicbeard 20May2011 14:58

Re: I need your help with an programm
 
Thx for the advise, my english is probably to bad to understand me completly.
The first problem i have is how can i store files on the server site (server.c)
In the client file i need a function that creates a file for the students.
So far i'm creating a file with:
Code:

void append_record(FILE *fp)

Epicbeard 21May2011 22:30

Re: I need your help with an programm
 
So far its going really good, but i have one question:
I've a file on the server site with a few records each has 7 variables which function can i use to print these records on the client site. I can't use printf() for this and recv() isn't the best funktion for this?

xpi0t0s 21May2011 23:31

Re: I need your help with an programm
 
There won't be a single function that does that but you have just named the two I would expect you to use. Use recv to pull back the data from the server into temporary variables, and printf to print the contents of those variables to the screen.


All times are GMT +5.5. The time now is 07:42.