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 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
|
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <time.h>
#include "client.h"
static void init(void)
{
#ifdef WIN32
WSADATA wsa;
int err = WSAStartup(MAKEWORD(2, 2), &wsa);
if(err < 0)
{
puts("WSAStartup failed !");
exit(EXIT_FAILURE);
}
#endif
}
static void end(void)
{
#ifdef WIN32
WSACleanup();
#endif
}
static void app(const char *address, const char *name)
{
SOCKADDR_IN sin = { 0 };
SOCKET sock = init_connection(address, &sin);
char buffer[BUF_SIZE];
strcpy(buffer,"hello i am your server you must obey !");
printf("the sent buffer is %s\n",buffer);
while(1)
{
time_t t = time(0); /* get current time */
sprintf(buffer, "time is %-24.24s", ctime(&t)); /* make readable */
printf("sending: %s\n", buffer);
write_server(sock, &sin, buffer);
sleep(3);
}
end_connection(sock);
}
static int init_connection(const char *address, SOCKADDR_IN *sin)
{
/* UDP so SOCK_DGRAM */
SOCKET sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
struct hostent *hostinfo;
if(sock == INVALID_SOCKET)
{
perror("socket()");
exit(errno);
}
hostinfo = gethostbyname(address);
if (hostinfo == NULL)
{
fprintf (stderr, "Unknown host %s.\n", address);
exit(EXIT_FAILURE);
}
sin->sin_addr.s_addr = htonl(address);//*(IN_ADDR *) hostinfo->h_addr;
sin->sin_port = htons(PORT);
sin->sin_family = AF_INET;
return sock;
}
static void end_connection(int sock)
{
closesocket(sock);
}
static int read_server(SOCKET sock, SOCKADDR_IN *sin, char *buffer)
{
int n = 0;
size_t sinsize = sizeof *sin;
if((n = recvfrom(sock, buffer, BUF_SIZE - 1, 0, (SOCKADDR *) sin, &sinsize)) < 0)
{
perror("recvfrom()");
exit(errno);
}
buffer[n] = 0;
return n;
}
static void write_server(SOCKET sock, SOCKADDR_IN *sin, const char *buffer)
{
if(sendto(sock, buffer, strlen(buffer), 0, (SOCKADDR *) sin, sizeof *sin) < 0)
{
perror("sendto()");
exit(errno);
}
}
int main(int argc, char **argv)
{
if(argc < 2)
{
printf("Usage : %s [address] [pseudo]\n", argv[0]);
return EXIT_FAILURE;
}
init();
printf("port emission sender = %d; adresse broadcast=%s",PORT, argv[1]);
app(argv[1], argv[2]);
end();
return EXIT_SUCCESS;
}
Puis un programme "receiver" qui est sensé lire le text envoyé :
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <time.h>
#include "client.h"
static void init(void)
{
#ifdef WIN32
printf("init in");
WSADATA wsa;
int err = WSAStartup(MAKEWORD(2, 2), &wsa);
if(err < 0)
{
puts("WSAStartup failed !");
exit(EXIT_FAILURE);
}
#endif
}
static void end(void)
{
#ifdef WIN32
WSACleanup();
#endif
}
static void app(const char *address, const char *name) {
SOCKADDR_IN sin = {0
};
struct ip_mreq mreq;
SOCKET sock = init_connection(address, &sin, &mreq);
char buffer[BUF_SIZE];
/* send our name */
//write_server(sock, &sin, name);
while (1) {
/* set the timer to 1.5 second */
struct timeval timeout;
timeout.tv_sec = 1; /* 1 s */
timeout.tv_usec = 5 * 100 * 1000; /* 500 ms */
fd_set rdfs;
FD_ZERO(&rdfs);
FD_SET(sock, &rdfs);
//FD_ISSET(sock, &rdfs);
int err = select(FD_SETSIZE, &rdfs, NULL, NULL, &timeout);
switch (err) {
case 0:
/* timeout */
break;
case -1 :
/* error */
perror("error select !!!\n");
printf("errno=%d\n",errno);
exit(1);
break;
default:
/* stream event */
/* - data has been received */
if (FD_ISSET(sock, &rdfs)) {
int n = read_server(sock, &sin, buffer);
printf("valeur de n=%d",n);
/* server down */
if (n == 0) {
printf("Server disconnected !\n");
break;
}
puts(buffer);
}
}
}
end_connection(sock);
}
static int init_connection(const char *address, SOCKADDR_IN *sin, struct ip_mreq *mreq)
{
/* UDP so SOCK_DGRAM */
SOCKET sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
struct hostent *hostinfo;
u_int yes=1; /*** MODIFICATION TO ORIGINAL */
//SOCKADDR_IN addr;
if(sock == INVALID_SOCKET)
{
perror("socket()");
exit(errno);
}
hostinfo = gethostbyname(address);
if (hostinfo == NULL)
{
fprintf (stderr, "Unknown host %s.\n", address);
exit(EXIT_FAILURE);
}
/**** MODIFICATION TO ORIGINAL */
/* allow multiple sockets to use the same PORT number */
if (setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,(char*)&yes,sizeof(yes)) < 0) {
perror("Reusing ADDR failed");
exit(1);
}
/*** END OF MODIFICATION TO ORIGINAL */
/* memset(&addr, 0, sizeof(addr));
addr.sin_addr.s_addr = htonl(INADDR_ANY);//*(IN_ADDR *) hostinfo->h_addr;
addr.sin_port = htons(PORT);
addr.sin_family = AF_INET;
*/
sin->sin_addr.s_addr = htonl(INADDR_ANY);//*(IN_ADDR *) hostinfo->h_addr;
sin->sin_port = htons(PORT);
sin->sin_family = AF_INET;
int error=bind(sock,(struct sockaddr *) sin,sizeof(*sin));
printf("error=%d",error);
if (error < 0) {
perror("bind");
exit(1);
}
/* use setsockopt() to request that the kernel join a multicast group */
mreq->imr_multiaddr.s_addr=inet_addr(address);
mreq->imr_interface.s_addr=htonl(INADDR_ANY);
error=setsockopt(sock,IPPROTO_IP,IP_ADD_MEMBERSHIP,(char*) mreq,sizeof(*mreq));
if (error < 0) {
perror("setsockopt2");
exit(1);
}
return sock;
}
static void end_connection(int sock)
{
closesocket(sock);
}
static int read_server(SOCKET sock, SOCKADDR_IN *sin, char *buffer)
{
int n = 0;
size_t sinsize = sizeof *sin;
if((n = recvfrom(sock, buffer, BUF_SIZE - 1, 0, (SOCKADDR *) sin, &sinsize)) < 0)
{
perror("recvfrom()");
exit(errno);
}
buffer[n] = 0;
return n;
}
static void write_server(SOCKET sock, SOCKADDR_IN *sin, const char *buffer)
{
if(sendto(sock, buffer, strlen(buffer), 0, (SOCKADDR *) sin, sizeof *sin) < 0)
{
perror("sendto()");
exit(errno);
}
}
int main_1(int argc, char **argv)
{
if(argc < 2)
{
printf("Usage : %s [address] [pseudo]\n", argv[0]);
return EXIT_FAILURE;
}
init();
printf("port ecoute receiver = %d; adresse broadcast=%s",PORT, argv[1]);
app(argv[1], argv[2]);
end();
return EXIT_SUCCESS;
} |
Partager