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

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 {