-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.c
More file actions
162 lines (138 loc) · 3.27 KB
/
Copy pathclient.c
File metadata and controls
162 lines (138 loc) · 3.27 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
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
/*
* client.c - socket program thing
*
*
* Copyright 2016 job <job@function1.nl>
*
* Permission to use, copy, modify, and/or distribute this software for
* any purpose is hereby granted.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
* WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
* AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA
* OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
* TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <string.h>
#define BUFLEN 256
/* prints msg, with error details, and exits */
void ferr(const char* msg)
{
perror(msg);
exit(EXIT_FAILURE);
}
/* gets a pointer to the internet address of a sockaddr* */
void * getinaddr(struct sockaddr *sa)
{
if (sa->sa_family == AF_INET) /* IPv4 */
return &(((struct sockaddr_in*)sa)->sin_addr);
return &(((struct sockaddr_in6*)sa)->sin6_addr); /* IPv6 */
}
int main(int argc, char *argv[])
{
int sock, rv;
size_t msglen, n;
struct addrinfo hints, *servinfo, *p;
char buffer[BUFLEN];
char ipbuffer[INET6_ADDRSTRLEN];
char * sendbuffer = NULL;
if (argc < 3)
{
fprintf(stderr, "Usage: %s <hostname> <port>\n", argv[0]);
exit(EXIT_FAILURE);
}
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC; /*Unspecified(IPv4, IPv6, don't care)*/
hints.ai_socktype = SOCK_STREAM; /* TCP */
/* get server info */
if ((rv = getaddrinfo(argv[1], argv[2], &hints, &servinfo)) != 0)
{
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
return EXIT_FAILURE;
}
/* browse through IP's */
for (p = servinfo; p != NULL; p = p->ai_next)
{
inet_ntop(
p->ai_family,
getinaddr((struct sockaddr *)p->ai_addr),
ipbuffer,
sizeof(ipbuffer)
);
printf("Trying %s...", ipbuffer);
fflush(stdout);
if ((sock = socket(p->ai_family, p->ai_socktype,
p->ai_protocol)) == -1)
{
perror("\tError opening socket");
continue;
}
if (connect(sock, p->ai_addr, p->ai_addrlen) == -1)
{
perror("\tFailed");
close(sock);
continue;
}
/* working socket found */
printf("\tSuccess!\n");
break;
}
if (p == NULL)
{
fprintf(stderr, "All attempts failed\n");
exit(EXIT_FAILURE);
}
printf("Connected to %s! Say something!\n", ipbuffer);
freeaddrinfo(servinfo);
/* TODO: poll()? */
int pid = fork();
if (pid < 0)
{
ferr("Error on fork");
}
else if (pid == 0)
{
/* child */
while (1)
{
n = getline(&sendbuffer, &msglen, stdin);
if (n < 0)
ferr("getline");
n = send(sock, sendbuffer, strlen(sendbuffer), 0);
if (n < 0)
ferr("Error writing socket");
}
}
else
{
/* parent */
while (1)
{
memset(buffer, 0, BUFLEN);
n = recv(sock, buffer, BUFLEN - 1, 0);
if (n < 0)
ferr("Error reading socket");
else if (n == 0)
{
printf("Socket closed by server\n");
close(sock);
return 0;
}
printf("%s", buffer);
}
}
close(sock);
return 0;
}