-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathClient.c
More file actions
112 lines (81 loc) · 2.26 KB
/
Client.c
File metadata and controls
112 lines (81 loc) · 2.26 KB
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
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<netinet/in.h>
#define PORT 8000
void send_request(int);
void receive_response(int);
unsigned char snd_data[256];
unsigned char rcv_data[256];
int sock,valread,len;
struct sockaddr_in cliaddr;
int new_socket,i;
int main(int argc,char const*argv[])
{
sock = socket(AF_INET,SOCK_STREAM,0);
sleep(6);
if(sock<0)
{
printf("\n Socket creation error \n");
}
else
printf("\n Socket Created Successfully ");
// memset(&servaddr,'0',sizeof(servaddr));
cliaddr.sin_family = AF_INET; // IPv4
cliaddr.sin_port = htons(PORT);
cliaddr.sin_addr.s_addr = INADDR_ANY;
sleep(2);
if(connect(sock,(struct sockaddr*)&cliaddr,sizeof(cliaddr))<0)
{
printf("\n Connection Failed \n ");
return -1;
}
else
printf("\n Connection Successfull ");
sleep(2);
{
printf("-------------------------------------------------------------\n");
printf("\n Sending: Request to Proxy Server 2");
send_request(sock);
sleep(1);
receive_response(sock);
printf("\n Response Recieved From Proxy Server 2");
printf("-------------------------------------------------------------\n");
sleep(5);
}
return 0;
}
void send_request(int sock) {
snd_data[0] = 0x00; // Transition Identifier High Bit
snd_data[1] = 0x00; // Transition Identifier Low Bit
snd_data[2] = 0x00; // Protocol Identifier High Bit
snd_data[3] = 0x00; // Protocol Identifier Low Bit
snd_data[4] = 0x00; // Length High Bit
snd_data[5] = 0x06; // Length Low Bit
snd_data[6] = 0x01; // Unit Identifier
snd_data[7] = 0x01; // Function Code
snd_data[8] = 0x00; // Starting Address High bit
snd_data[9] = 0x0F; // Starting Address Low bit
snd_data[10] = 0x00; // Count High bit
snd_data[11] = 0x03; // Count Low bit
send(sock, snd_data, 12, 0);
printf("\nsent data [");
for(i = 0; i < 12; i++) {
printf(" %02x ", snd_data[i]);
}
printf("]\n");
}
void receive_response(int sock) {
recv(sock, rcv_data, 256, 0);
int n = rcv_data[5] + 6;
printf("\n Recieved Data [");
for(i = 0; i < n ; i++)
{
printf(" %02x ",rcv_data[i]);
}
printf("]\n");
}