-
Notifications
You must be signed in to change notification settings - Fork 0
/
simplest.c
212 lines (189 loc) · 4.47 KB
/
simplest.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<arpa/inet.h>
typedef enum { frame_arrival } event_type;
typedef enum { data } frame_kind;
char current_frame[8];
char str[256],info[4];
event_type current_event;
char *ip="111.11.111.111";
int port=2900;
int server_sock,client_sock;
struct sockaddr_in server_addr,client_addr;
socklen_t addr_size;
int b,r,w;
void waitForEvent(event_type *event)
{
*event=current_event;
}
void deliverData(int seqnum)
{
str[(seqnum-1)*4]=info[0];
str[(seqnum-1)*4+1]=info[1];
str[(seqnum-1)*4+2]=info[2];
str[(seqnum-1)*4+3]=info[3];
}
void getData(int seqnum)
{
info[0]=str[(seqnum-1)*4];
info[1]=str[(seqnum-1)*4+1];
info[2]=str[(seqnum-1)*4+2];
info[3]=str[(seqnum-1)*4+3];
}
void extractData()
{
info[0]=current_frame[3];
info[1]=current_frame[4];
info[2]=current_frame[5];
info[3]=current_frame[6];
}
void makeFrame(frame_kind kind,int seqnum,int acknum)
{
bzero(current_frame,8);
current_frame[0]=(kind==data)?'D':'0';
current_frame[1]=(char)(seqnum+48);
current_frame[2]=(char)(acknum+48);
current_frame[3]=info[0];
current_frame[4]=info[1];
current_frame[5]=info[2];
current_frame[6]=info[3];
current_frame[7]='0';
}
int sendFrame()
{
w=write(client_sock,current_frame,strlen(current_frame));
if(w!=-1)
return 0;
return 1;
}
int receiveFrame()
{
bzero(current_frame,8);
r=read(client_sock,current_frame,sizeof(current_frame));
if(r!=-1)
return 0;
return 1;
}
int main()
{
printf(">>> enter local ip : ");
ip=malloc(20*sizeof(char));
scanf("%s",ip);
int mode=0,i=0;
char ch;
printf("[+] start.\n>>> enter (1) to run as sender (2) to run as receiver (0) to exit : ");
scanf("%d",&mode);
switch(mode)
{
case 1:
printf("[+] sender side activated.\n");
printf(">>> enter data : ");
fflush(stdin);
scanf(" %[^\n]s",str);
printf("[+] data to be sent : %s\n",str);
int p;
if(strlen(str)%4!=0)
{
int x=strlen(str);
for(p=0;p<4-x%4;p++)
str[x+p]='\0';
}
client_sock=socket(AF_INET,SOCK_STREAM,0);
if(client_sock<0)
{
printf("[-] client socket failed.\n");
exit(1);
}
printf("[+] client socket created.\n");
memset(&client_addr,'\0',sizeof(client_addr));
client_addr.sin_family=AF_INET;
client_addr.sin_port=port;
client_addr.sin_addr.s_addr=inet_addr(ip);
connect(client_sock,(struct sockaddr*)&client_addr,sizeof(client_addr));
printf("[+] connected to server.\n");
int len=strlen(str);
for(p=0;p<4;p++)
str[len+p]='\0';
int sf;
for(i=0;i<=len/4;i++)
{
getData(i+1);
makeFrame(data,i+1,0);
sf=sendFrame();
if(sf==1)
printf("[-] frame %d not sent [ data = '%c%c%c%c' ].\n",i+1,info[0],info[1],info[2],info[3]);
else
printf("[+] frame %d sent [ data = '%c%c%c%c' ].\n",i+1,info[0],info[1],info[2],info[3]);
}
close(client_sock);
printf("[+] disconnected from server.\n");
break;
case 2:
printf("[+] receiver side activated.\n");
server_sock=socket(AF_INET,SOCK_STREAM,0);
if(server_sock<0)
{
printf("[-] server socket failed.\n");
exit(1);
}
printf("[+] server socket created.\n");
memset(&server_addr,'\0',sizeof(server_addr));
server_addr.sin_family=AF_INET;
server_addr.sin_port=port;
server_addr.sin_addr.s_addr=inet_addr(ip);
b=bind(server_sock,(struct sockaddr*)&server_addr,sizeof(server_addr));
if(b<0)
{
printf("[-] bind to port failed.\n");
exit(1);
}
printf("[+] bind to port successful.\n");
listen(server_sock,5);
printf("[+] listening for connection...\n");
addr_size=sizeof(client_addr);
client_sock=accept(server_sock,(struct sockaddr*)&client_addr,&addr_size);
printf("[+] client connected.\n");
int rf;
do
{
event_type event;
waitForEvent(&event);
rf=receiveFrame();
if(rf==1)
{
printf("[-] frame %d not received.\n",i+1);
continue;
}
extractData();
printf("[+] frame %d received [ data = '",i+1);
int q;
for(q=0;q<4;q++)
if(info[q]!='\0')
printf("%c",info[q]);
printf("' ].\n");
ch=info[0];
deliverData(i+1);
i++;
}
while(ch!='\0');
close(client_sock);
printf("[+] client disconnected.\n");
close(server_sock);
printf("[+] server disconnected.\n");
int y;
for(y=0;y<strlen(str);y++)
if(str[y]=='\0')
break;
str[y]='\0';
printf("[+] data received : %s\n",str);
break;
case 0:
break;
default:
printf("[-] invalid choice.\n");
}
printf("[+] exit.\n");
return 0;
}