UDP Server and Client
From Go Embedded! Knowledgebase
Most Embedded Systems today are connected to rest of the world through wires or one wirelessly. Understanding foundations of Networking Programming is essential to working with networking for embedded systems. Here is a simple program that shows an echo client/server. To compile these programs you will need C/C++ compiler on MS Windows and Windows Sockets header files and libraries.
_Contents |
[edit] Introduction to socket programming
- Socket Programming model was introducted in BSD UNIX Operating System. This model is implemented in other operating systems like Windows, Linux, Solaris etc. It is based on the principle that everything is a file. Socket model consists of simple set of functions (API).
- Socket is a communication endpoint. First, client and server must create a communication endpoint.
SOCKET socket(int domain, int type, int protocol);
- In client-server programming, two communication methods are commonly used.
- Connection Oriended - In this case, client and server maintain a permanent connection for entire time period in which they exchange messages with each other. For connection oriented sockets, use
SOCK_STREAMintypeargument (Stream socket) - Connectionless - In this case, client and server exchange messages one by one. No permanent connection is established between them for time period in which these messages are exchanged. For Connectionless sockets, use
SOCK_DGRAMintypeargument (Datagram socket)
- Connection Oriended - In this case, client and server maintain a permanent connection for entire time period in which they exchange messages with each other. For connection oriented sockets, use
-
protocolparameter specifies which Transport Layer Protocol that will be used with this scoket. Most commonly used values are:-
IPPROTO_TCPfor stream oriented sockets. This tells the operating system know that we need to establish a permanent connection with other endpoint of this socket. It further informs the OS that TCP protcol be used for communication between client and server. -
IPPROTO_UDPfor connectionless sockets. This tells the operating system know that we don't want to establish a connection with other endpoint of this socket. It further informs the OS that UDP (Universal Datagram Protocol) be used to communicate between client and server.
-
[edit] Header file: common.h
#define WIN32_LEAN_AND_MEAN #include <stdio.h> #include <tchar.h> #include <winsock2.h> #include <ws2tcpip.h> #define MAXLINE 4096 /* Max. text line length */ #define SERV_PORT 8773 typedef int socklen_t;
[edit] Server: udpserver.c
#include "common.h"
extern void dg_echo(SOCKET sockfd, struct sockaddr *pcliaddr, socklen_t clilen);
int _tmain(int argc, _TCHAR* argv[])
{
SOCKET sockfd;
WSADATA wsa;
struct sockaddr_in servaddr, cliaddr;
WSAStartup ( MAKEWORD(2, 0), &wsa);
sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(SERV_PORT);
bind(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr));
dg_echo(sockfd, (struct sockaddr *) &cliaddr, sizeof(cliaddr));
}
void
dg_echo(SOCKET sockfd, struct sockaddr *pcliaddr, socklen_t clilen)
{
int n;
socklen_t len;
char mesg[MAXLINE];
for ( ; ; ) {
len = clilen;
n = recvfrom(sockfd, mesg, MAXLINE, 0, pcliaddr, &len);
if (n == SOCKET_ERROR)
{
fprintf(stderr, "Error %ld in recvfrom\n", WSAGetLastError());
break;
}
sendto(sockfd, mesg, n, 0, pcliaddr, len);
}
}
[edit] Client: udpclient.c
#include "common.h"
extern void dg_cli(FILE *fp, SOCKET sockfd, const sockaddr *pservaddr, socklen_t servlen);
int _tmain ( int argc , _TCHAR * argv[] )
{
SOCKET sockfd ;
WSADATA wsa ;
struct sockaddr_in servaddr ;
WSAStartup ( MAKEWORD(2, 0), &wsa);
if (argc != 2) {
_tprintf(_T("usage: udpcli <IPaddress>\n"));
_exit ( 0 ) ;
}
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(SERV_PORT);
servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
dg_cli(stdin, sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr));
exit(0);
}
void
dg_cli(FILE *fp, SOCKET sockfd, const sockaddr *pservaddr, socklen_t servlen)
{
int n;
char sendline[MAXLINE], recvline[MAXLINE + 1];
while (fgets(sendline, MAXLINE, fp) != NULL) {
sendto(sockfd, sendline, (int)strlen(sendline), 0, pservaddr, servlen);
n = recvfrom(sockfd, recvline, MAXLINE, 0, NULL, NULL);
if (n == SOCKET_ERROR) {
fprintf(stderr, "Error %ld in recvfrom\n", WSAGetLastError());
break;
}
recvline[n] = 0; /* null terminate */
fputs(recvline, stdout);
}
}
[edit] External Links
- Windows Socket Programming
- TCP [RFC 793]
- IP [RFC 791]
