-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpiggy_type.c
113 lines (100 loc) · 2.44 KB
/
piggy_type.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <stdlib.h>
#include <stdio.h>
//For ncurses functions
#include <locale.h>
#include <curses.h>
#include "populate.h"
#define QLEN 15 /* size of request queue */
void update_win(int i);
void head(int right_con, struct sockaddr_in ad, WINDOW* sw[])
{
if(connect(right_con, (struct sockaddr *)&ad, sizeof(ad)) < 0) {
nocbreak();
endwin();
perror("connect");
exit(1);
}
wprintw(sw[6],"Status: right_connection established.\n");
update_win(6);
wmove(sw[4],0,10);
update_win(4);
}
void middle(int left_con, int right_con,
struct sockaddr_in lad, struct sockaddr_in rad, WINDOW* sw[])
{
int rc; int flag = 1;
/*Behaves like a head and connects to the tail*/
if(connect(right_con, (struct sockaddr *)&rad, sizeof(rad)) < 0) {
nocbreak();
endwin();
perror("Connect");
exit(1);
} else {
wprintw(sw[6],"Status: Right connection established.\n");
wprintw(sw[6],"Waiting for left connection...\n");
update_win(6);
wmove(sw[4],0,10);
update_win(4);
}
/*Eliminate "Address already in use "error message."*/
if(setsockopt(left_con,SOL_SOCKET,SO_REUSEADDR,&flag,sizeof(int))==-1){
nocbreak();
endwin();
perror("setsockopt");
exit(1);
}
// bind the connection
if(bind(left_con, (struct sockaddr *)&lad, sizeof(lad)) < 0) {
nocbreak();
endwin();
perror("Bind");
exit(1);
} else {
wprintw(sw[6],"Status: Left connection ready.");
update_win(6);
wmove(sw[4],0,10);
update_win(4);
wprintw(sw[6],"\n");
}
//make connection passive and recv incoming connecitons
rc = listen(left_con, QLEN);
if(rc < 0) {
nocbreak();
endwin();
perror("listen() failed\n");
exit(1);
}
}
void tail(int left_con, struct sockaddr_in ad, WINDOW* sw[])
{
int rc; int flag = 1;
if(setsockopt(left_con,SOL_SOCKET,SO_REUSEADDR,&flag,sizeof(int))==-1){
nocbreak();
endwin();
perror("setsockopt");
exit(1);
}
//bind the connection
if(bind(left_con, (struct sockaddr *)&ad, sizeof(ad)) < 0) {
nocbreak();
endwin();
perror("Bind");
exit(1);
} else {
wprintw(sw[6],"Status: Server Ready.\n");
update_win(6);
wmove(sw[4],0,10);
update_win(4);
}
//make connection passive and recv incoming connecitons
rc = listen(left_con, QLEN);
if(rc < 0) {
nocbreak();
endwin();
perror("listen() failed\n");
exit(1);
}
}