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

