Commit d4978bee authored by Sven Gestegård Robertz's avatar Sven Gestegård Robertz
Browse files

added UDP examples

parent 109448ae
......@@ -54,7 +54,7 @@ int main(int argc, char *argv[])
port = SERVER_PORT;
}
printf("simple_client: connecting to server: %s, port %d\n",server_name, port);
printf("simple_tcp_client: connecting to server: %s, port %d\n",server_name, port);
init(server_name, port);
make_request();
......@@ -67,7 +67,7 @@ static void make_request()
{
char msg[BUFSZ];
#ifdef DEBUG
printf("simple_client: connecting\n");
printf("simple_tcp_client: connecting\n");
#endif
socket_init();
if (connect(sockfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
......@@ -78,7 +78,7 @@ static void make_request()
perror("ERROR reading from motion server");
} else {
msg[res]='\0'; /* ensure msg is null terminated */
printf("simple_client: response: %s\n",msg);
printf("simple_tcp_client: response: %s\n",msg);
}
socket_close();
}
......
......@@ -48,7 +48,7 @@ static int bind_server_socket(int fd, int port){
if(bind(fd, (struct sockaddr*) &addr, sizeof(addr))) return -1;
#ifdef INFO
printf("simple_server: bound fd %d to port %d\n",fd,port);
printf("simple_tcp_server: bound fd %d to port %d\n",fd,port);
#endif
return fd;
......@@ -65,10 +65,10 @@ static int do_serve(int fd)
"BYE.\n";
size_t len = strlen(msg);
printf("simple_server: attempting accept on fd %d\n",fd);
printf("simple_tcp_server: attempting accept on fd %d\n",fd);
if((clientfd = accept(fd, NULL, NULL)) < 0) return -1;
#ifdef INFO
printf("simple_server: writing msg (len=%lu) to clientfd (%d)\n",len,clientfd);
printf("simple_tcp_server: writing msg (len=%lu) to clientfd (%d)\n",len,clientfd);
#endif
#ifdef WRITE_LOOP
......@@ -80,7 +80,7 @@ static int do_serve(int fd)
goto error;
}
#ifdef INFO
printf("simple_server: write returned %d\n",res);
printf("simple_tcp_server: write returned %d\n",res);
#endif
written += res;
} while (written < len);
......@@ -95,7 +95,7 @@ static int do_serve(int fd)
#endif
error:
printf("simple_server: closing clientfd (%d)\n",clientfd);
printf("simple_tcp_server: closing clientfd (%d)\n",clientfd);
return close(clientfd);
}
......@@ -110,7 +110,7 @@ int main()
do_serve(fd);
printf("simple_server: closing socket: %d\n", fd);
printf("simple_tcp_server: closing socket: %d\n", fd);
close(fd);
return 0;
......
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <sys/socket.h>
#include <netinet/ip.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <string.h>
#define BUFSZ 1024
#define SERVER_PORT 5000
/*
* create a datagram socket
*/
int create_socket()
{
int fd = -1;
fd = socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);
return fd;
}
#undef USE_IP_NUMBER
static int init_remote(struct sockaddr_in* remote_end, socklen_t remote_size, const char* server_str, int port )
{
memset((char*) &remote_end, 0, remote_size);
remote_end->sin_family = AF_INET;
remote_end->sin_port = htons(port);
#ifdef USE_IP_NUMBER
if (inet_aton(server_str , &remote_end->sin_addr) == 0)
{
fprintf(stderr, "inet_aton() failed\n");
return -1;
}
#else
{
struct hostent* server;
server = gethostbyname(server_str);
if (server == NULL) {
fprintf(stderr,"ERROR, server name not found\n");
return -1;
} else {
bcopy((char *)server->h_addr, (char *)&remote_end->sin_addr.s_addr, server->h_length);
}
}
#endif
}
/*
* Send a datagram to server and wait for one reply
*/
static int send_and_receive(int fd, const char* server_str, int port)
{
char buf[BUFSZ];
struct sockaddr_in remote_end;
socklen_t remote_size = sizeof(remote_end);
if(port < 0 || port > 65535) {
errno = EINVAL;
return -1;
}
printf("simple_udp_client: sending to %s:%d (fd=%d)\n",server_str,port,fd);
init_remote(&remote_end, remote_size, server_str, port);
const char* msg = "Hello, socket!\n"
"I am a text\n"
"BYE.\n";
ssize_t len = strlen(msg);
len = sendto(fd, msg, len, 0, (struct sockaddr*) &remote_end, remote_size);
if(len == -1){
perror("sendto");
return len;
}
printf("simple_udp_client: waiting for datagram on fd %d\n",fd);
len = recvfrom(fd, buf, BUFSZ-1, 0, (struct sockaddr*) &remote_end, &remote_size);
if(len == -1){
perror("recvfrom");
return len;
}
buf[len] = 0; /* add null termination to string*/
printf("simple_udp_client: received packet from %s:%d, len=%zu\n%s\n",
inet_ntoa(remote_end.sin_addr),
ntohs(remote_end.sin_port),
len,
buf);
return 0;
}
int main(int argc, char *argv[])
{
const char* server_str;
int port;
int fd;
if(argc>=2) {
server_str = argv[1];
} else {
#ifdef USE_IP_NUMBER
server_str = "127.0.0.1";
#else
server_str = "localhost";
#endif
}
if(argc==3) {
port = atoi(argv[2]);
} else {
port = SERVER_PORT;
}
// printf("simple_client: connecting to server: %s, port %d\n",server_str, port);
fd = create_socket();
if(fd < 0){
perror("create_socket");
return 1;
}
printf("calling send_and_receive for %s,%d\n",server_str,port);
send_and_receive(fd, server_str, port);
printf("simple_udp_client: closing socket: %d\n", fd);
close(fd);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <sys/socket.h>
#include <netinet/ip.h>
#include <arpa/inet.h>
#include <string.h>
#define BUFSZ 1024
#define SERVER_PORT 5000
static int bind_socket(int fd, int port);
/*
* create a server socket bound to port
* and listening.
*
* return positive file descriptor
* or negative value on error
*/
int create_socket(int port)
{
int fd = -1;
if(port < 0 || port > 65535) {
errno = EINVAL;
return -1;
}
fd = socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);
if(fd < 0) return fd;
if(bind_socket(fd,port) < 0) return -1;
printf("simple_udp_server: bound socket to port %d\n",port);
return fd;
}
static int bind_socket(int fd, int port){
struct sockaddr_in addr;
// if(setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val))) {
// perror("setsockopt");
// return -1;
// }
/* see man page ip(7) */
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = htonl(INADDR_ANY);
if(bind(fd, (struct sockaddr*) &addr, sizeof(addr))) return -1;
#ifdef INFO
printf("simple_server: bound fd %d to port %d\n",fd,port);
#endif
return fd;
}
/*
* Wait for one datagram and echo it back
*/
static int do_serve(int fd)
{
char buf[BUFSZ];
struct sockaddr_in remote_end;
socklen_t remote_size = sizeof(remote_end);
// const char* msg = "Hello, socket!\n"
// "I am a text\n"
// "BYE.\n";
// size_t len = strlen(msg);
ssize_t len;
printf("simple_server: waiting for datagram on fd %d\n",fd);
len = recvfrom(fd, buf, BUFSZ-1, 0, (struct sockaddr*) &remote_end, &remote_size);
if(len == -1){
perror("recvfrom");
return len;
}
buf[len] = 0; /* add null termination to string*/
printf("simple_server: received packet from %s:%d, len=%zu\n%s\n",
inet_ntoa(remote_end.sin_addr),
ntohs(remote_end.sin_port),
len,
buf);
len = sendto(fd, buf, len, 0, (struct sockaddr*) &remote_end, remote_size);
if(len == -1){
perror("sendto");
return len;
}
return 0;
}
int main(int argc, char *argv[])
{
const char* server_name;
int port;
int fd;
if(argc>=2) {
server_name = argv[2];
} else {
server_name = "localhost";
}
if(argc==3) {
port = atoi(argv[3]);
} else {
port = SERVER_PORT;
}
// printf("simple_client: connecting to server: %s, port %d\n",server_name, port);
fd = create_socket(port);
if(fd < 0){
perror("create_socket");
return 1;
}
do_serve(fd);
printf("simple_server: closing socket: %d\n", fd);
close(fd);
return 0;
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment