blablalaberblaieduiranet
This commit is contained in:
parent
2d1cd95f90
commit
7856a47f69
2
Makefile
2
Makefile
|
@ -13,7 +13,7 @@ all: bin/netsl
|
||||||
|
|
||||||
bin/netsl: misc.h main.c net.c
|
bin/netsl: misc.h main.c net.c
|
||||||
mkdir -pv bin
|
mkdir -pv bin
|
||||||
gcc -o bin/main main.c
|
gcc -o bin/netsl main.c
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -rf bin
|
rm -rf bin
|
||||||
|
|
3
msg.c
3
msg.c
|
@ -44,8 +44,11 @@ void serialize_int(int x, Buffer * b) {
|
||||||
|
|
||||||
memcpy(((char *)b->data) + b->next, &x, sizeof(int));
|
memcpy(((char *)b->data) + b->next, &x, sizeof(int));
|
||||||
b->next += sizeof(int);
|
b->next += sizeof(int);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void struct
|
||||||
|
|
||||||
//serialize_string
|
//serialize_string
|
||||||
//serialize_message
|
//serialize_message
|
||||||
|
|
||||||
|
|
25
net.c
25
net.c
|
@ -38,7 +38,7 @@ int run_server(const struct prog_info *pinfo) {
|
||||||
hints.ai_socktype = SOCK_DGRAM;
|
hints.ai_socktype = SOCK_DGRAM;
|
||||||
|
|
||||||
sprintf(portbuf, "%d", pinfo->port);
|
sprintf(portbuf, "%d", pinfo->port);
|
||||||
if ((ret=getaddrinfo("0.0.0.0", portbuf, &hints, &servinfo)) != 0) {
|
if ((ret=getaddrinfo("255.255.255.255", portbuf, &hints, &servinfo)) != 0) {
|
||||||
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(ret));
|
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(ret));
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
@ -61,6 +61,12 @@ int run_server(const struct prog_info *pinfo) {
|
||||||
tim.tv_sec = 0;
|
tim.tv_sec = 0;
|
||||||
tim.tv_nsec = 500000000;
|
tim.tv_nsec = 500000000;
|
||||||
|
|
||||||
|
int broadcastPermission = 1;
|
||||||
|
if (setsockopt(sockfd, SOL_SOCKET, SO_BROADCAST, (void *) &broadcastPermission,sizeof(broadcastPermission)) < 0){
|
||||||
|
fprintf(stderr, "setsockopt error");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
for (;;) {
|
for (;;) {
|
||||||
printf("sending...\n");
|
printf("sending...\n");
|
||||||
int numbytes;
|
int numbytes;
|
||||||
|
@ -83,8 +89,6 @@ int run_client(const struct prog_info *pinfo, void (*framecallback)(long) ) {
|
||||||
struct addrinfo hints, *servinfo;
|
struct addrinfo hints, *servinfo;
|
||||||
char portbuf[6];
|
char portbuf[6];
|
||||||
|
|
||||||
//framecallback(123);
|
|
||||||
|
|
||||||
memset(&hints, 0, sizeof(hints));
|
memset(&hints, 0, sizeof(hints));
|
||||||
hints.ai_family = AF_INET; // IPv4
|
hints.ai_family = AF_INET; // IPv4
|
||||||
hints.ai_socktype = SOCK_DGRAM; // UDP
|
hints.ai_socktype = SOCK_DGRAM; // UDP
|
||||||
|
@ -104,13 +108,6 @@ int run_client(const struct prog_info *pinfo, void (*framecallback)(long) ) {
|
||||||
int sockfd;
|
int sockfd;
|
||||||
char s[INET6_ADDRSTRLEN];
|
char s[INET6_ADDRSTRLEN];
|
||||||
for (info = servinfo; info != NULL; info = info->ai_next) {
|
for (info = servinfo; info != NULL; info = info->ai_next) {
|
||||||
/* if (info->ai_addr) {
|
|
||||||
struct sockaddr_in* p = (struct sockaddr_in *)info->ai_addr;
|
|
||||||
inet_ntop(p->sin_family, &p->sin_addr, s, INET6_ADDRSTRLEN);
|
|
||||||
printf(" %d: %s\n", info->ai_addr->sa_family, s);
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
if ((sockfd = socket(info->ai_family, info->ai_socktype, info->ai_protocol)) == -1) {
|
if ((sockfd = socket(info->ai_family, info->ai_socktype, info->ai_protocol)) == -1) {
|
||||||
perror("sock");
|
perror("sock");
|
||||||
continue;
|
continue;
|
||||||
|
@ -141,17 +138,15 @@ int run_client(const struct prog_info *pinfo, void (*framecallback)(long) ) {
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("listener: got packet from %s\n", inet_ntop(their_addr.ss_family, get_in_addr((struct sockaddr *)&their_addr), s, sizeof s));
|
//printf("listener: got packet from %s\n", inet_ntop(their_addr.ss_family, get_in_addr((struct sockaddr *)&their_addr), s, sizeof s));
|
||||||
printf("listener: packet is %d bytes long\n", numbytes);
|
//printf("listener: packet is %d bytes long\n", numbytes);
|
||||||
buf[numbytes] = '\0';
|
buf[numbytes] = '\0';
|
||||||
printf("listener: packet contains \"%s\"\n", buf);
|
//printf("listener: packet contains \"%s\"\n", buf);
|
||||||
|
|
||||||
framecallback(123);
|
framecallback(123);
|
||||||
|
|
||||||
} while (strncmp(buf, "exit", 10000));
|
} while (strncmp(buf, "exit", 10000));
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
close(sockfd);
|
close(sockfd);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
Loading…
Reference in New Issue
Block a user