-
Notifications
You must be signed in to change notification settings - Fork 1
/
TCPIPUtils.c
executable file
·177 lines (148 loc) · 4.43 KB
/
TCPIPUtils.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
/*
* TCPIPUtils.c
* FastCTD
*
* Created by Mai on 2/4/05.
* Copyright 2005 __MyCompanyName__. All rights reserved.
*
*/
#include "TCPIPUtils.h"
#define PORT_NUMBER 2342
int writeString (int fd, const void *buffer, size_t length)
{
int result;
unsigned char byte;
if (length > 4095) {
fprintf (stderr, "truncating message to 255 bytes\n");
length = 4095;
}
byte = (unsigned char)length;
result = write (fd, &byte, 1);
if (result <= 0) {
goto bailout;
}
do {
result = write (fd, buffer, length);
if (result <= 0) {
goto bailout;
}
length -= result;
buffer += result;
} while (length > 0);
bailout:
return (result);
} // writeString
// opentcpClient: open TCP connection for client to input hostname:
// open socket, connect (no binding and listenning)
int opentcpClient(char hname[])
{
int result, fd = -1;
struct sockaddr_in serverAddress;
struct hostent *hostInfo;
hostInfo = gethostbyname(hname);
if (hostInfo == NULL) {
fprintf (stderr, "could not gethostbyname for '%s'\n", hname);
fprintf (stderr, " error: %d / %s\n", h_errno, hstrerror(h_errno));
return -1;
}
serverAddress.sin_len = sizeof (struct sockaddr_in);
serverAddress.sin_family = AF_INET;
serverAddress.sin_port = htons (PORT_NUMBER);
serverAddress.sin_addr = *((struct in_addr *)(hostInfo->h_addr));
memset (&(serverAddress.sin_zero), 0, sizeof(serverAddress.sin_zero));
result = socket (AF_INET, SOCK_STREAM, 0);
if (result == -1) {
fprintf (stderr, "could not make a socket. error: %d / %s\n",
errno, strerror(errno));
close (fd);
return -1;
}
fd = result;
// no need to bind() or listen()
result = connect (fd, (struct sockaddr *)&serverAddress,
sizeof(serverAddress));
if (result == -1) {
fprintf (stderr, "could not connect. error: %d / %s\n",
errno, strerror(errno));
close (fd);
return -1;
}
result = fd;
return (result);
}
// opentcp: for server(TCP socket)
// create socket, setoptions for socket and binding
int opentcpServer(int port) {
int error;
int one = 1;
struct sockaddr_in server;
int sock;
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
fprintf (stderr, "could not make a socket. error: %d / %s\n",
errno, strerror(errno));
return -1;
}
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) {
fprintf (stderr, "couldn't setsockopt to reuseaddr. %d / %s\n",
errno, strerror(errno));
close(sock);
return -1;
}
if (port > 0) {
server.sin_family = AF_INET;
server.sin_addr.s_addr = htonl(INADDR_ANY);
server.sin_port = htons((short)port);
if (bind(sock, (struct sockaddr *)&server, sizeof(server)) == -1) {
fprintf (stderr, "could not bind socket. error: %d / %s\n",
errno, strerror(errno));
error = errno;
close(sock);
errno = error;
return -1;
}
}
return sock;
}
// startListening: for server (TCP socket)
// create socket, setoptions for socket, binding and listenning
int ServerStartListening (int port) // for server
{
struct sockaddr_in address;
int fd = -1, success = 0, val;
int result;
int yes = 1;
result = socket (AF_INET, SOCK_STREAM, 0);
if (result == -1) {
fprintf (stderr, "could not make a socket. error: %d / %s\n",
errno, strerror(errno));
goto bailout;
}
fd = result;
// bind to an address and port
memset (&address, 0, sizeof(address));
address.sin_family = AF_INET;
address.sin_addr.s_addr = htonl (INADDR_ANY);
address.sin_port = htons ((short)port);
address.sin_len = sizeof (struct sockaddr_in);
result = bind (fd, (struct sockaddr *)&address, sizeof(address));
if (result == -1) {
fprintf (stderr, "could not bind socket. error: %d / %s\n",
errno, strerror(errno));
goto bailout;
}
// allow 1024 queue
result = listen (fd, 1024);
if (result == -1) {
fprintf (stderr, "listen failed. error: %d / %s\n",
errno, strerror(errno));
goto bailout;
}
success = 1;
bailout:
if (!success) {
close (fd);
fd = -1;
}
return (fd);
} // SererStartListening