Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
#include "message.h"
#include <cstring>
#include <arpa/inet.h>
#include <errno.h>
#include <netdb.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
using namespace std;
int main(int argc, char **argv)
{
int reuse= 1;
int server, client;
int port = atoi(argv[1]);
struct sockaddr_in server_addr,client_addr;
socklen_t clientlen = sizeof(client_addr);
memset(&server_addr,0,sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = (port);
server_addr.sin_addr.s_addr = INADDR_ANY;
server = socket(PF_INET,SOCK_STREAM,0);
setsockopt(server, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse));
bind(server,(const struct sockaddr *)&server_addr,sizeof(server_addr));
listen(server,SOMAXCONN);
streampos size;
char * memblock;
client = accept(server,(struct sockaddr *)&client_addr,&clientlen);
msg_t returns;
memset(&returns,0,BUF_SZ+100);
recv(client,&returns,BUF_SZ+50,0);
if(returns.msg_type == MSG_TYPE_FINISH)
{
return 0;
}
if(returns.msg_type != 1)
{
cout << "not a get" << endl;
//continue;
}
ifstream file (reinterpret_cast<const char*>(returns.payload) , ios::in|ios::binary|ios::ate);
if (file.is_open())
{
size = file.tellg();
memblock = new char [size];
file.seekg (0, ios::beg);
file.read (memblock, size);
file.close();
}
else
{
cout << "Unable to open file";
return 1;
}
//server: RX <msg_type> <cur_seq> <max_seq> <payload_len>
for(int x = 0, x1 = 0; x1 <= (size/BUF_SZ); x+=BUF_SZ, x1++)
{
msg_t aa;
memset(&aa,0,BUF_SZ+100);
aa.msg_type = MSG_TYPE_GET_RESP;
aa.cur_seq = x1;
aa.max_seq = (size/BUF_SZ);
cout << "server: RX " << aa.cur_seq << " "<< aa.max_seq << " "<< aa.payload_len << endl;
for(int y = 0; y < BUF_SZ; y++)
aa.payload[y] = memblock[y + (x)];
aa.payload_len = BUF_SZ;
send(client, &aa, BUF_SZ+50, 0);
msg_t returnse2;
recv(client,&returnse2,BUF_SZ+50,0);
if(returnse2.msg_type != 4)
{
cout << "NO ACK!" << endl;
return 1;
}
}
cout << "DONE!" << endl;
msg_t ack;
ack.msg_type = MSG_TYPE_FINISH;
send(client, &ack, sizeof(ack), 0);
close(client);
close(server);
cout << "DONE!2" << endl;
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.