-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclientbejref.cpp
177 lines (164 loc) · 4.46 KB
/
clientbejref.cpp
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
#include <iostream>
#include <typeinfo>
#include "mysockbej.h"
using namespace std;
class Client{
private :
int a,flag,status,tcpFlag;
int yes,listenflag;
char s[INET6_ADDRSTRLEN],message[MAXDATASIZE];
int sockfd,newfd;
const char *local;
struct sigaction sa;
const char *port;
struct addrinfo *p,*serverInfo, hints;
public :
void display(int tcpFlag)
{
//bool display(struct addrinfo *pointer)
if(tcpFlag == 1)
{
cout << "\n addrinfo strucutre has the following details TCP " ;
cout << "\n ai_family" << serverInfo->ai_family;
cout << "\n ai_socktype" << serverInfo->ai_socktype;
cout << "\n ai_addr : " << (struct sockaddr *) (serverInfo->ai_addr)->sa_data;
cout << "\n ai_addrlen: " << serverInfo->ai_addrlen;
}//end tcpFlag == 1
else if(tcpFlag == 2)
{
cout << "\n addrinfo strucutre has the following details UDP " ;
cout << "\n ai_family" << serverInfo->ai_family;
cout << "\n ai_socktype" << serverInfo->ai_socktype;
cout << "\n ai_addr : " << (struct sockaddr *) (serverInfo->ai_addr)->sa_data;
cout << "\n ai_addrlen:" << serverInfo->ai_addrlen << endl;
}// end tcpFlag == 2
}// end display
Client(char *myport,int tcpFlag)
{
//Default for both TCP and UDP ports
memset(&hints, 0, sizeof (hints));
port=myport;
hints.ai_flags = AI_PASSIVE;
hints.ai_family = AF_UNSPEC;
local="127.0.0.1";
yes=1;
if(tcpFlag == 1)
{
cout << " Input myportTCP \n" << myport;
cout << " Assigned portTCP \n" << port;
hints.ai_socktype = SOCK_STREAM;
}//end tcpFlag
else if(tcpFlag == 2)
{
cout << " Input myportUDP \n" << myport;
cout << " Assigned portUDP \n" << port;
hints.ai_socktype = SOCK_DGRAM;
}
}//end Constructor Client
void testDNS(int tcpFlag)
{
if(tcpFlag == 1)
{
if ((status = getaddrinfo(local, port, &hints, &serverInfo)) != 0) {
fprintf(stderr, "TCP DNS getaddrinfo error: %s\n", gai_strerror(status));
exit(1);
}
display(1);
}//end tcpFlag==1
else if(tcpFlag == 2)
{
if ((status = getaddrinfo(local, port, &hints, &serverInfo)) != 0) {
fprintf(stderr, "UDP DNS getaddrinfo error: %s\n", gai_strerror(status));
exit(1);
}
display(2);
}//end tcpFlag==2
}//end DNS
int clientConnect(int tcpFlag)
{
if(tcpFlag == 1)
{
for(p = serverInfo; p != NULL; p = p->ai_next)
{
sockfd = socket(p->ai_family, p->ai_socktype,p->ai_protocol);
if (sockfd == -1)
{
perror("client: socket");
continue;
}
if (connect(sockfd, p->ai_addr, p->ai_addrlen) == -1)
{
close(sockfd);
perror("client: connect ");
//cout << sockfd ;
continue;
}
cout << " Inside TCP clientConnect getting address and connected" <<endl;
break;
}// end for
int sendBytes;
//sendBytes=sendTCP();
//return sendBytes;
}//end tcpFlag == 1
else if(tcpFlag == 2)
{
for(p = serverInfo; p != NULL; p = p->ai_next)
{
sockfd = socket(p->ai_family, p->ai_socktype,p->ai_protocol);
if (sockfd == -1)
{
perror("client: socket");
continue;
}
if (connect(sockfd, p->ai_addr, p->ai_addrlen) == -1)
{
close(sockfd);
perror("client: connect");
continue;
}
cout << " Inside UDP clientConnect getting address and connected" <<endl;
break;
}// end for
return 2;
}// end else tcpFlag == 2
}// end clientConnect
/*int sendTCP()
{
int numbytes;
numbytes=send(sockfd,message,sizeof(message), 0);
cout << " TCP Client send " << numbytes << endl;
return numbytes;
}
void setMessage(char *ptr)
{
strcpy(message,ptr);
}*/
void *get_in_addr(struct sockaddr *sa)
{
if (sa->sa_family == AF_INET) {
return &(((struct sockaddr_in*)sa)->sin_addr);
}
return &(((struct sockaddr_in6*)sa)->sin6_addr);
}
};
int main()
{
char tcp[MAXDATASIZE],udp[MAXDATASIZE];
int tcpCheck, udpCheck;
char message[] = "Sri Saibaba";
cout << "Enter TCP port number";
cin >> tcp;
//cout << "Enter UDP port number";
//cin >> udp;
Client tcpClient(tcp,1);
Client udpClient(udp,2);
tcpClient.testDNS(1);
//udpClient.testDNS(2);
//tcpClient.setMessage(message);
if ((tcpClient.clientConnect(1)) < 0)
cout << " TCP Client Connect not happening" << endl;
//if ((udpClient.clientConnect(2)) < 0)
//cout << " UDP Client Connect not happening" << endl;
//tcpClient.display(1);
//udpClient.display(2);
}