Startpunkten

This commit is contained in:
2026-03-05 13:35:06 +01:00
commit fd04d44e12
7 changed files with 595 additions and 0 deletions

45
c_app.c Normal file
View File

@@ -0,0 +1,45 @@
/* ***************************************** */
/* Laboration 2 i Datakommunikation 2 */
/* RPC-lib */
/* ***************************************** */
/* c_app.c */
/* Ber användaren mata in vilken typ */
/* av operation denne vill göra */
/* ***************************************** */
/* Christian Ohlsson, di7chro@cse.kau.se */
/* Stefan Sonesson, di7stes@cse.kau.se */
/* ***************************************** */
#include "header.h"
/* ==============================================
menu()
Skriver ut menyn på skärmen
============================================== */
int menu() {
char choice[5];
int ret_val = TRUE;
system("clear");
puts("Welcome to RPC");
puts("================");

259
c_stub.c Normal file
View File

@@ -0,0 +1,259 @@
/* ***************************************** */
/* Laboration 2 i Datakommunikation 2 */
/* RPC-lib */
/* ***************************************** */
/* c_stub.c */
/* Klientapplikation som skickar */
/* kommandon till servern */
/* ***************************************** */
/* Christian Ohlsson, di7chro@cse.kau.se */
/* Stefan Sonesson, di7stes@cse.kau.se */
/* ***************************************** */
#include "header.h"
/* Globala variabler */
int fd=-1, sock; /* fd kommer ihåg om någon fil är öppen */
msg myMsg; /* Meddelandet som funktionerna fyller i */
/* ==============================================
in_connect()
Gör en uppkoppling mot en server
============================================== */
void in_connect() {
char *remhost;
u_short remport;
struct sockaddr_in remote;
struct hostent *h;
/* Skapa en socket att skicka data på */
if((sock = socket(AF_INET, SOCK_STREAM, 0 )) < 0 ) {
perror("client:socket");
exit(1);
}
remport = 8704;
/* Nolla ut structen */
bzero((char *) &remote, sizeof(remote));
remote.sin_family = AF_INET;
/* Fyll i hostent structen */
if((h = gethostbyname(myMsg.machine)) == NULL) {
perror("client:gethostbyname");
exit(1);
}
bcopy((char *)h->h_addr, (char *)&remote.sin_addr, h->h_length);
remote.sin_port = htons(remport);
/* Begär uppkoppling mot servern */
if(connect(sock, (struct sockaddr *)&remote, sizeof(remote)) < 0) {
perror("client:connect");
exit(1);
}
}
/* ==============================================
in_open()
Letar fram server och fil att öppna
============================================== */
int in_open() {
int i,j,k=0;
char this_machine[BUFSIZE], this_path[BUFSIZE], filename[BUFSIZE];
if(fd<0) {
printf("Enter filename: ");
fgets(filename, sizeof(filename), stdin);
for(i = 0; i < BUFSIZE; i++) {
if(filename[i] != '/')
this_machine[i] = filename[i];
else {
this_machine[i] = 0;
break;
}
}
for(; i < BUFSIZE ; i++) {
if(filename[i] != 10) {
this_path[k] = filename[i];
k++;
}
else {
this_path[k] = 0;
break;
}
}
myMsg.op = OP;
if(filename[0] == '/') /* Lokal fil */
strcpy(this_machine, "localhost");
strcpy(myMsg.machine, this_machine);
strcpy(myMsg.path, this_path);
in_connect(myMsg);
}
else {
puts("A file is already open!");
sleep(SLEEPTIME);
return FALSE;
}
return TRUE;
}
/* ==============================================
in_read()
Sätter operatorn till READ
============================================== */
int in_read() {
if(fd > 0)
myMsg.op = RD;
else {
puts("No file is open!");
sleep(SLEEPTIME);
return FALSE;
}
return TRUE;
}
/* ==============================================
in_write()
Frågar hur mycket som skall skrivas och
sätter operatorn
============================================== */
int in_write() {
char buf[BUFSIZE];
int length = 0;
if(fd>0) {
printf("How many bytes do you wish to write: ");
gets(buf);
length = atoi(buf);
printf("Enter text: ");
if((myMsg.data = (char*)malloc(length)) < 0) {
perror("Malloc fel, byt minnesskretsarna, eller skaffa fler\n");
return FALSE;
}
myMsg.length = length;
gets(myMsg.data);
myMsg.op = WR;
}
else {

BIN
c_stub.o Normal file

Binary file not shown.

63
header.h Normal file
View File

@@ -0,0 +1,63 @@
/* ***************************************** */
/* Laboration 2 i Datakommunikation 2 */
/* RPC-lib */
/* ***************************************** */
/* header.h */
/* Innehåller deklarationer och konstanter */
/* ***************************************** */
/* Christian Ohlsson, di7chro@cse.kau.se */
/* Stefan Sonesson, di7stes@cse.kau.se */
/* ***************************************** */
#ifndef __HEADER_H__
#define __HEADER_H__
/* Kommandon som kan skickas */
#define OP 0
#define OPP 1
#define OPN 2
#define RD 3
#define RDP 4
#define RDN 5
#define WR 6
#define WRP 7
#define WRN 8
#define DEL 9
#define DLP 10
#define DLN 11
#define CL 12
#define CLP 13
#define CLN 14
/* Konstanter */
#define BUFSIZE 50/* Buffertstorlek vid inmatning */

15
read_me Normal file
View File

@@ -0,0 +1,15 @@
Readmefil för Laboration i
Datakommunikation II
=================================
RPC-lib
Kompilera genom att köra
makefilen (make)
.SDR filerna i Doc/ katalogen
är skapade med SmartDraw
Labben är provkörd och klar.
================================
Christian.Ohlsson@cs.kau.se

213
s_stub.c Normal file
View File

@@ -0,0 +1,213 @@
/* ***************************************** */
/* Laboration 2 i Datakommunikation 2 */
/* RPC-lib */
/* ***************************************** */
/* s_stub.c */
/* Ansvarar för att operationer utförs */
/* utförs på rätt sätt i servern */
/* ***************************************** */
/* Christian Ohlsson, di7chro@cse.kau.se */
/* Stefan Sonesson, di7stes@cse.kau.se */
/* ***************************************** */
#include "header.h"
/* Globala variabler */
int data_sockfd; /* Den socket som data färdas på */
msg myMsg; /* Meddelandet som funktionerna fyller i */
/* ==============================================
printsin()
Skriver ut information om klienten
============================================== */
printsin(struct sockaddr_in *sin, char *m1, char *m2 ) {
struct hostent *gethostbyaddr(), *h;
printf ("%s %s\n", m1, m2);
printf ("Family %d addr %x port %d\n", sin -> sin_family,
ntohl(sin -> sin_addr.s_addr), ntohs(sin -> sin_port));
h = gethostbyaddr((char *)&sin->sin_addr, sizeof(int), AF_INET);
printf ("Symbolic host name <%s>\n",h->h_name);
}
/* ==============================================
rem_open()
Öppnar en fil och fyller i data såsom
längd och fildeskriptor i strukten.
============================================== */
void rem_open() {
int fd;
struct stat st;
if((fd = open(myMsg.path, O_CREAT|O_RDWR|O_APPEND, PERMS)) < 0) {
myMsg.op = OPN; /* Skicka fel till klient */
return;
}
fstat(fd, &st);
myMsg.length = st.st_size;
myMsg.op = OPP; /* Operationen gick bra */
myMsg.fd = fd;
}
/* ==============================================
rem_read()
Läser data ur en fil tills EOF tecknet nås och
dessa tecken stoppas i en buffer, som slutligen
returneras
============================================== */
char *rem_read() {
char *buf;
int fd;
lseek(myMsg.fd, 0, SEEK_SET);
buf = (char*)malloc(myMsg.length);
if((read(myMsg.fd, buf, myMsg.length)) < 0)
myMsg.op = RDN; /* Skicka fel till klient */
else
myMsg.op = RDP; /* Operationen gick bra */
return buf;
}
/* ==============================================
rem_write()
Skriver tecken till fil
============================================== */
void rem_write() {
if((write(myMsg.fd, myMsg.data, myMsg.length)) < 0)
myMsg.op = WRN; /* Skicka fel till klient */
else
myMsg.op = WRP; /* Operationen gick bra */
}
/* ==============================================
rem_delete()
Tar bort en fil mha unlink, som egentligen
räknar ner antalet länkar till en fil
============================================== */
int rem_delete() {
if((unlink(myMsg.path)) < 0) {
myMsg.op = DLN; /* Skicka fel till klient */
return 0;
}
else {
myMsg.op = DLP; /* Operationen gick bra */
return 1;
}
}
/* ==============================================
rem_close()
Stänger en fil.
Returnerar svar om hur det gick
============================================== */
int rem_close() {
if((close(myMsg.fd)) < 0) {
myMsg.op = CLN; /* Skicka fel till klient */
return 0;
}
else {
myMsg.op = CLP; /* Operationen gick bra */
return 1;

BIN
s_stub.o Normal file

Binary file not shown.