-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathio-models-nonblocking.c
More file actions
164 lines (136 loc) · 4.07 KB
/
Copy pathio-models-nonblocking.c
File metadata and controls
164 lines (136 loc) · 4.07 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
163
164
/*
** io-models-blocking.c
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <ctype.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
// Enable if you want debugging to be printed, see examble below.
// Alternative, pass CFLAGS=-DDEBUG to make, make CFLAGS=-DDEBUG
#define DEBUG
#define MAXDATA 65000
// get sockaddr, IPv4 or IPv6:
void *get_in_addr(struct sockaddr *sa)
{
if (sa->sa_family == AF_INET) {
return &(((struct sockaddr_in*)sa)->sin_addr);
}
if (sa->sa_family == AF_INET6) {
return &(((struct sockaddr_in6*)sa)->sin6_addr);
}
printf("Unknown FAMILY!!!!\n");
return(0);
}
int main(int argc, char *argv[])
{
int sockfd, numbytes;
struct addrinfo hints, *servinfo, *p;
int rv;
char buffer[MAXDATA];
char s[INET6_ADDRSTRLEN];
if (argc != 2) {
fprintf(stderr,"usage: client hostname:port\n");
exit(1);
}
/*
Read first input, assumes <ip>:<port> syntax, convert into one string (Desthost) and one integer (port).
Atm, works only on dotted notation, i.e. IPv4 and DNS. IPv6 does not work if its using ':'.
*/
char delim[]=":";
char *Desthost=strtok(argv[1],delim);
char *Destport=strtok(NULL,delim);
// *Desthost now points to a sting holding whatever came before the delimiter, ':'.
// *Dstport points to whatever string came after the delimiter.
/* Do magic */
int DestportNum=atoi(Destport);
#ifdef DEBUG
printf("Host %s, and port %d.\n",Desthost,DestportNum);
#endif
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
if ((rv = getaddrinfo(Desthost, Destport, &hints, &servinfo)) != 0) {
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
return 1;
}
// loop through all the results and connect to the first we can
for(p = servinfo; p != NULL; p = p->ai_next) {
if ((sockfd = socket(p->ai_family, p->ai_socktype,
p->ai_protocol)) == -1) {
perror("client: socket");
continue;
}
if (connect(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
close(sockfd);
perror("client: connect");
continue;
}
printf("Connection successfull.\n");
break;
}
/* Why are we here??? p == NULL, Looped no match (socket &| connect)..
Success?? */
if (p == NULL) {
fprintf(stderr, "client: failed to socket||connect\n");
/* Clear servinfo */
freeaddrinfo(servinfo); // all done with this structure
return 2;
}
const char *q=inet_ntop(p->ai_family, get_in_addr((struct sockaddr *)p->ai_addr),s, sizeof s);
if ( q == NULL ) {
fprintf(stderr, "problem inet_ntop\n");
freeaddrinfo(servinfo); // all done with this structure
return 2;
}
/* q ok or not */
printf("client: connected to %s:%s\n", s,Destport);
freeaddrinfo(servinfo); // all done with this structure
int totalBytes=0;
char outBuffer[60];
char outASCIIBuffer[60];
char outASCIIBuffer2[80];
while(1){
memset(&buffer, 0, MAXDATA);
memset(&outASCIIBuffer, 0, 60);
memset(&outASCIIBuffer2, 0, 80);
memset(&outBuffer, 0, 60);
numbytes = recv(sockfd, buffer, MAXDATA-1, MSG_DONTWAIT);
if(numbytes == -1 ){
if (errno == EWOULDBLOCK ) {
printf("EWOULDBLOCK.\n");
usleep(500000);
continue;
}
}
if (numbytes == 0) {
printf("Server closed.\n");
break;
}
totalBytes+=numbytes;
memcpy(&outBuffer,&buffer,20);
printf("client (%d/%d) : received \n",numbytes, totalBytes);
for(int i=0; i<10;i++){
if( isprint(outBuffer[i])) {
printf("%c ",outBuffer[i]);
} else {
printf("- ");
}
if(i==0){
sprintf(outASCIIBuffer2,"0x%02x",outBuffer[i]);
} else {
sprintf(outASCIIBuffer2,"%s 0x%02x",outASCIIBuffer,outBuffer[i]);
}
}
printf("\n%s \n",outASCIIBuffer);
/* sleep(2); */
}
close(sockfd);
return 0;
}