testserver.c 1.77 KB
Newer Older
1
2
3
4
5
6
7
8
9
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h> 

Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
10
#include "jg.h"
11
12
13
14
15
16
17
18
19
#include "dec.h"
#include "enc.h"

void error(const char *msg)
{
    perror(msg);
    exit(0);
}

Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
20
void do_labcomm(int sockfd, jg_foo *v)
21
22
{
    void *enc = enc_init(sockfd);
Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
23
24
25
26
    int i;
    for(i=0; i<10;i++) {
        enc_run(enc, v);
    }
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
    enc_cleanup(enc);
}

int main(int argc, char *argv[])
{
    int sockfd, portno, n;
    int newsockfd, clilen;
    struct sockaddr_in serv_addr, cli_addr;
    struct hostent *server;

    char buffer[256];
    if (argc < 2) {
       fprintf(stderr,"usage %s port\n", argv[0]);
       exit(0);
    }
    portno = atoi(argv[1]);
    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd < 0) 
        error("ERROR opening socket");

Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
47
48
49
50
51
    int so_reuseaddr = 1; //TRUE;
    if(setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR, &so_reuseaddr, sizeof so_reuseaddr)) {
        error("ERROR setting socket options");
    }

52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
    bzero((char *) &serv_addr, sizeof(serv_addr));

    serv_addr.sin_addr.s_addr = INADDR_ANY;
    serv_addr.sin_port = htons(portno);

    if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
        perror("ERROR on binding");
        exit(1);
    }

    listen(sockfd,2);
    clilen = sizeof(cli_addr);

   /* Accept actual connection from the client */
    newsockfd = accept(sockfd, (struct sockaddr *)&cli_addr, &clilen);
    if (newsockfd < 0) {
        perror("ERROR on accept");
       exit(1);
    }
Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
71
72
73
74
75
76
77
78
79
80
81
    
    jg_foo v;

    v.b = 17.17;
    v.c = 1742;
    v.d = 4217;
    v.e = "hello";
    v.f = 17;
    v.g = 42;
    v.h = 2;
    v.i = 42.42;
82

Sven Gestegård Robertz's avatar
Sven Gestegård Robertz committed
83
    do_labcomm(newsockfd, &v);
84
85
86
87
88

    close(newsockfd);
    close(sockfd);
    return 0;
}