first working net solution
This commit is contained in:
parent
a79fcb4cbb
commit
b4d175c882
10
Makefile
10
Makefile
|
@ -1,17 +1,17 @@
|
|||
|
||||
|
||||
all: bin/test bin/main
|
||||
all: bin/netsl
|
||||
|
||||
|
||||
#bin/net: net.c
|
||||
# mkdir -pv bin
|
||||
# gcc -o bin/net net.c
|
||||
|
||||
bin/test: test.c
|
||||
mkdir -pv bin
|
||||
gcc -o bin/test test.c
|
||||
#bin/test: test.c
|
||||
# mkdir -pv bin
|
||||
# gcc -o bin/test test.c
|
||||
|
||||
bin/main: main.c net.c
|
||||
bin/netsl: misc.h main.c net.c
|
||||
mkdir -pv bin
|
||||
gcc -o bin/main main.c
|
||||
|
||||
|
|
5
main.c
5
main.c
|
@ -57,6 +57,10 @@ int parseArgs(struct prog_info *pinfo, int argc, char **argv) {
|
|||
return 0;
|
||||
}
|
||||
|
||||
void callback(long tst) {
|
||||
printf("in callback, tst=%ld\n", tst);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
|
||||
int ret;
|
||||
|
@ -69,6 +73,7 @@ int main(int argc, char **argv) {
|
|||
} else {
|
||||
printf("running in CLIENT mode, using client number %d\n", prog_info.client_num);
|
||||
// ...
|
||||
ret = run_client(&prog_info, callback);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
44
net.c
44
net.c
|
@ -28,8 +28,9 @@ void *get_in_addr(struct sockaddr *sa)
|
|||
}
|
||||
|
||||
int run_server(const struct prog_info *pinfo) {
|
||||
struct addrinfo hints, *servinfo;
|
||||
struct addrinfo hints, *servinfo, *p;
|
||||
int ret;
|
||||
int sockfd;
|
||||
|
||||
char portbuf[6];
|
||||
memset(&hints, 0, sizeof(hints));
|
||||
|
@ -38,19 +39,52 @@ int run_server(const struct prog_info *pinfo) {
|
|||
|
||||
sprintf(portbuf, "%d", pinfo->port);
|
||||
if ((ret=getaddrinfo("0.0.0.0", portbuf, &hints, &servinfo)) != 0) {
|
||||
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
|
||||
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(ret));
|
||||
return 1;
|
||||
}
|
||||
// ...
|
||||
|
||||
for(p = servinfo; p != NULL; p = p->ai_next) {
|
||||
if ((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) {
|
||||
perror("talker: socket");
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
if (p == NULL) {
|
||||
fprintf(stderr, "talker: failed to bind socket\n");
|
||||
return 2;
|
||||
}
|
||||
|
||||
struct timespec tim;
|
||||
tim.tv_sec = 0;
|
||||
tim.tv_nsec = 500000000;
|
||||
|
||||
for (;;) {
|
||||
printf("sending...\n");
|
||||
int numbytes;
|
||||
if ((numbytes = sendto(sockfd, "bla", 3, 0, p->ai_addr, p->ai_addrlen)) == -1) {
|
||||
perror("error sending");
|
||||
return -44;
|
||||
}
|
||||
nanosleep(&tim, NULL);
|
||||
}
|
||||
|
||||
freeaddrinfo(servinfo);
|
||||
|
||||
close(sockfd);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int run_client(const struct prog_info *pinfo) {
|
||||
int run_client(const struct prog_info *pinfo, void (*framecallback)(long) ) {
|
||||
|
||||
struct addrinfo hints, *servinfo;
|
||||
char portbuf[6];
|
||||
|
||||
//framecallback(123);
|
||||
|
||||
memset(&hints, 0, sizeof(hints));
|
||||
hints.ai_family = AF_INET; // IPv4
|
||||
hints.ai_socktype = SOCK_DGRAM; // UDP
|
||||
|
@ -111,7 +145,11 @@ int run_client(const struct prog_info *pinfo) {
|
|||
printf("listener: packet is %d bytes long\n", numbytes);
|
||||
buf[numbytes] = '\0';
|
||||
printf("listener: packet contains \"%s\"\n", buf);
|
||||
|
||||
framecallback(123);
|
||||
|
||||
} while (strncmp(buf, "exit", 10000));
|
||||
|
||||
|
||||
|
||||
close(sockfd);
|
||||
|
|
67
test.c
67
test.c
|
@ -1,67 +0,0 @@
|
|||
/*
|
||||
** talker.c -- a datagram "client" demo
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <netdb.h>
|
||||
|
||||
#define SERVERPORT "4711" // the port users will be connecting to
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int sockfd;
|
||||
struct addrinfo hints, *servinfo, *p;
|
||||
int rv;
|
||||
int numbytes;
|
||||
|
||||
if (argc != 3) {
|
||||
fprintf(stderr,"usage: talker hostname message\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
memset(&hints, 0, sizeof hints);
|
||||
hints.ai_family = AF_UNSPEC;
|
||||
hints.ai_socktype = SOCK_DGRAM;
|
||||
|
||||
if ((rv = getaddrinfo(argv[1], SERVERPORT, &hints, &servinfo)) != 0) {
|
||||
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
|
||||
return 1;
|
||||
}
|
||||
|
||||
// loop through all the results and make a socket
|
||||
for(p = servinfo; p != NULL; p = p->ai_next) {
|
||||
if ((sockfd = socket(p->ai_family, p->ai_socktype,
|
||||
p->ai_protocol)) == -1) {
|
||||
perror("talker: socket");
|
||||
continue;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
if (p == NULL) {
|
||||
fprintf(stderr, "talker: failed to bind socket\n");
|
||||
return 2;
|
||||
}
|
||||
|
||||
if ((numbytes = sendto(sockfd, argv[2], strlen(argv[2]), 0,
|
||||
p->ai_addr, p->ai_addrlen)) == -1) {
|
||||
perror("talker: sendto");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
freeaddrinfo(servinfo);
|
||||
|
||||
printf("talker: sent %d bytes to %s\n", numbytes, argv[1]);
|
||||
close(sockfd);
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue
Block a user