-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxyclient.c
More file actions
64 lines (54 loc) · 1.7 KB
/
proxyclient.c
File metadata and controls
64 lines (54 loc) · 1.7 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
/* Proxy server */
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <stdio.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <unistd.h>
void error(char *msg) {
perror(msg);
exit(1);
}
int main(int argc, char**argv) {
const int socketfd = socket(AF_INET, SOCK_STREAM, 0);
if (socketfd < 0)
error("ERROR on opening socket");
const int optval = 1;
if (setsockopt(socketfd, SOL_SOCKET, SO_REUSEADDR,
(const void *)&optval, sizeof(int)) < 0)
error("ERROR on setting reuseaddr");
if (setsockopt(socketfd, IPPROTO_TCP, TCP_NODELAY,
(const void*)&optval, sizeof(int)) < 0)
error("ERROR on setting nodelay");
if (setsockopt(socketfd, SOL_SOCKET, SO_KEEPALIVE,
(const void*)&optval, sizeof(int)) < 0)
error("ERROR on setting keepalive");
// const int port = 1433; // default port for ms sql server
const int port = 5433;
struct sockaddr_in servaddr;
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(port);
if (connect(socketfd, (struct sockaddr *)&servaddr,sizeof(servaddr)) < 0)
error("ERROR on connecting");
char buf[1024] = "hello world";
const int n = write(socketfd, buf, strlen(buf));
if (n < 0)
error("ERROR on writing to socket");
else
printf("write %d bytes\n", n);
bzero(buf, 1024);
const int m = read(socketfd, buf, 1024);
if (m < 0)
error("ERROR on reading from socket");
else
printf("read from socket: %s, bytes: %d\n", buf, m);
close(socketfd);
return 0;
}