UDP features
No connection, data transmission service for datagram (message-based, non-sticky);
Unreliable (May drop packets), but typically UDP is more efficient;
UDP client/service basic model
UDP Base API
1.Recvfrom
Synopsis
#include <sys/types.h> #include <sys/socket.h> ssize_t recvfrom (int sockfd, void *buf, size_t len, int flags, struct sockaddr *src_addr, socklen_t *addrlen);
2.Sendto
Synopsis
#include <sys/types.h> #include <sys/socket.h> ssize_t sendto (int sockfd, const void *BUF, size_t Len, int flags, const struct SOCKADDR *dest_addr, socklen_t Addrlen);
Practice: A rudimentary echo echo server/client
Server.cpp#include "Commen.h" int main () {//create UDP socket int SOCKFD = socket (af_inet,sock_dgram,0); if (SOCKFD = =-1) {Err_exit ("socket error"); }//Fill in server-side information struct sockaddr_in serveraddr; serveraddr.sin_family = af_inet; Serveraddr.sin_port = htons (9001); ServerAddr.sin_addr.s_addr =inaddr_any; Bind server End port number if (bind (SOCKFD, struct sockaddr *) &serveraddr,sizeof (serveraddr)) = =-1) {Err_exit ("bind Error "); } Char Buf[bufsiz]; struct sockaddr_in peeraddr; socklen_t Peerlen = sizeof (PEERADDR); while (true) {memset (buf,0,sizeof (BUF)); int readcount = 0; Receive data sent from the client if ((Readcount = Recvfrom (sockfd,buf,sizeof (BUF), 0, (struct sockaddr *) &peeraddr,&peerlen)) = =-1) {err_exit ("Recvfrom error"); }//Print client information cout << "client:" << Endl; cout << "\tsin_port:" << ntohs (peeraddr.sin_port) << Endl; cout << "\tsin_addr:" << inet_ntoa (peeraddr.sin_addr) << Endl; Write back the information sent by the client to the IF (SendTo (sockfd,buf,sizeof (BUF), 0, (struct sockaddr *) &peeraddr,peerlen) = =-1) { Err_exit ("SendTo error"); } Buf[readcount] = 0; Output to screen fputs (buf,stdout); } return 0;}
Client.cpp#include "Commen.h" int main () {//create UDP socket int SOCKFD = socket (af_inet,sock_dgram,0); if (SOCKFD = =-1) {Err_exit ("socket error"); }//Fill in server-side information struct sockaddr_in serveraddr; serveraddr.sin_family = af_inet; Serveraddr.sin_port = htons (9001); SERVERADDR.SIN_ADDR.S_ADDR = inet_addr ("127.0.0.1"); Char Buf[bufsiz]; Receive data from the keyboard while (Fgets (buf,sizeof (BUF), stdin)! = NULL) {//Send data to server side if (SendTo (Sockfd,buf,strlen (b UF), 0, (struct sockaddr *) &serveraddr,sizeof (serveraddr)) = =-1) {err_exit ("SendTo error"); } memset (Buf,0,sizeof (BUF)); int readcount = 0; Receives data from the server side and ignores server-side information recvfrom (xx,xx,xx,xx,null,null) if (Readcount = Recvfrom (sockfd,buf,sizeof (BUF), 0,nul l,null)) = =-1) {err_exit ("Recvfrom error"); } Buf[readcount] = 0; Writes data to the screen fputs (buf,stdout); } return 0;}
Experimental results :
To view ports:
Attached -commen.h
#ifndef commen_h_included#define commen_h_included#include <unistd.h> #include <signal.h> #include < errno.h> #include <fcntl.h> #include <sys/types.h> #include <sys/wait.h> #include <sys/stat.h > #include <sys/ipc.h> #include <sys/shm.h> #include <sys/msg.h> #include <sys/sem.h># Include <sys/socket.h> #include <sys/select.h> #include <arpa/inet.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <string.h> #include <stdio.h> #include <stdlib.h> #include <iostream>using namespace std;void err_exit (std::string str) { perror (Str.c_str () ); Exit (exit_failure);} void Peercloseprint (std::string str = "Peer Connect closed") { cerr << str << endl; _exit (0);} #endif//commen_h_included
Attached -makefile
CC = g++ Cppflags =-wall-g-pthreadbin = Server Clientsources = $ (bin.=.cpp). Phony:clean All: $ (BIN) Clean: -RM-RF $ (BIN) Bin/obj/core
Socket programming Practice (1)--UDP Programming Basics