Last active
March 3, 2017 10:10
-
-
Save cristipufu/11639f014b1e3d4dc346d8b0d6f9a97b to your computer and use it in GitHub Desktop.
Simple stop wait transfer protocol
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef LIB | |
#define LIB | |
typedef struct { | |
int type; | |
int len; | |
char payload[1400]; | |
} msg; | |
void init(char* remote,int remote_port); | |
void set_local_port(int port); | |
void set_remote(char* ip, int port); | |
int send_message(const msg* m); | |
int recv_message(msg* r); | |
//msg* receive_message_timeout(int timeout); | |
#endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <string.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <unistd.h> | |
#include <fcntl.h> | |
#include "lib.h" | |
#define HOST "127.0.0.1" | |
#define PORT 10001 | |
#define FR_FILENAME 1 | |
#define FR_DATA 2 | |
#define FR_ACK 3 | |
msg create_message(int type, char *data, int length) { | |
msg m; | |
m.len = length; | |
m.type = type; | |
memset(m.payload, 0, length); | |
memcpy(m.payload, data, length); | |
return m; | |
} | |
void send_ack() { | |
msg t; | |
t = create_message(FR_ACK, NULL, 0); | |
send_message(&t); | |
} | |
int main(int argc,char** argv){ | |
msg r; | |
init(HOST,PORT); | |
int fd; | |
int forever = 1; | |
int ret; | |
while (forever) { | |
if (recv_message(&r)<0){ | |
perror("Receive message"); | |
return -1; | |
} | |
printf("[%s] Got msg with payload: %s %d\n", argv[0], r.payload, r.type); | |
switch(r.type) { | |
case FR_DATA: | |
write(fd, r.payload, r.len); | |
send_ack(); | |
break; | |
case FR_FILENAME: | |
fd = open(strcat(r.payload, ".out"), O_RDWR | O_CREAT | O_APPEND, 0666); | |
send_ack(); | |
break; | |
case FR_ACK: | |
forever = 0; | |
ret = close(fd); | |
if (ret < 0){ | |
perror("Close file error"); | |
} | |
break; | |
default: | |
break; | |
} | |
} | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <string.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <unistd.h> | |
#include <fcntl.h> | |
#include <sys/types.h> | |
#include <sys/stat.h> | |
#include "lib.h" | |
#define HOST "127.0.0.1" | |
#define PORT 10000 | |
#define BUFSIZE 1400 | |
#define FR_FILENAME 1 | |
#define FR_DATA 2 | |
#define FR_ACK 3 | |
msg create_message(int type, char *data, int length) { | |
msg m; | |
m.len = length; | |
m.type = type; | |
memset(m.payload, 0, length); | |
memcpy(m.payload, data, length); | |
return m; | |
} | |
void send_ack() { | |
msg t; | |
t = create_message(FR_ACK, NULL, 0); | |
send_message(&t); | |
} | |
void wait_ack() { | |
msg r; | |
recv_message(&r); | |
} | |
int main(int argc,char** argv){ | |
init(HOST,PORT); | |
msg t; | |
t = create_message(FR_FILENAME, argv[1], strlen(argv[1])); | |
send_message(&t); | |
wait_ack(); | |
int fd; | |
int bytesRead; | |
char buffer[BUFSIZE]; | |
fd = open(argv[1], O_RDONLY); | |
if (fd < 0) perror("file error"); | |
while ((bytesRead = read(fd, buffer, BUFSIZE)) > 0) { | |
t = create_message(FR_DATA, buffer, bytesRead); | |
send_message(&t); | |
wait_ack(); | |
} | |
send_ack(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment