Some auxiliary functions of linux_c socket datagram

Source: Internet
Author: User

1 /********************************************************************2 * DGRAM.C3 * Support functions for datagram based programs4  */5#include <stdio.h>6#include <unistd.h>7#include <sys/types.h>8#include <sys/socket.h>9#include <netinet/inch.h>Ten#include <arpa/inet.h> One#include <netdb.h> A#include <string.h> -  - #defineHostlen 256 the  - intmake_internet_address (); - /********************************************************************* - * for UDP (datagram server side) it is not as complex as TCP.  + * Server-side Application layer main call: - * Fd=socket (); + * BIND (FD, server address port), bind FD Source address, Destination address arbitrary A * (loop multiple times below) at * Recvfrom (FD, buf, size, addr) blocks waiting for client data requests and can learn from addr who sent the request - * Recvfrom Returns processing client requests - * SendTo (CONNFD, buf, size, addr) Send data reply to client address port addr -  ********************************************************************/ - intMake_dgram_server_soket (intportnum) { -   structSockaddr_in saddr;/*build our address here*/ in   CharHostname[hostlen];/*Address*/ -   intsock_id;/*The socket*/ to    +Sock_id=socket (Pf_inet, Sock_dgram,0);/*get a socket*/ -   if(sock_id = =-1)return-1; the  *   /** Build address and bind it to socket**/ $   Panax Notoginseng gethostname (hostname, hostlen); -Make_internet_address (hostname, Portnum, &saddr); the    +   if(Bind (sock_id, (structsockaddr*) &saddr,sizeof(SADDR))! =0) A     return-1; the    +   returnsock_id; - } $ /********************************************************************* $ * Not as complex as TCP for UDP (datagram client) - * The client application layer primarily calls: - * Fd=socket () assigns a file descriptor the * (loop multiple times below) - * SendTo (FD, buf, size, addr) sends a data request to the server address port addrWuyi * Recvfrom (FD, buf, size, addr) blocking wait for reply the * Recvfrom return - * End of cycle Wu * Close (FD) closed file descriptor -  ********************************************************************/ About intMake_dgram_client_socket () { $   returnSockets (Pf_inet, SOCK_DGRAM,0); - } -  - intMake_internet_address (Char* hostname,intPortstructSOCKADDR_IN *ADDRP) { A /* + * constructor for an Internet socket address, uses hostname and port the * (host, Port), *ADDRP; -  */ $   structHostent *HP; the    theBzero ((void*) ADDRP,sizeof(structsockaddr_in)); thehp=gethostbyname (hostname); the   if(Hp==null)return-1; -Bcopy ((void*) Hp->h_addr, (void*) &addrp->sin_addr, hp->h_length); inaddrp->sin_port=htons (port); theaddrp->sin_family=af_inet; the   return 0; About } the  the intGet_internet_address (Char* Host,intLenint*PORTP,structSOCKADDR_IN *ADDRP) { the /* + * Extracts host and port from an Internet socket address - * *addrp-> (host, Port) the  */Bayistrncpy (Host, Inet_ntoa (addrp->sin_addr), Len); the*portp=nthos (addrp->sin_port); the   return 0; -}

Some auxiliary functions of linux_c socket datagram

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.