UDP----Socket Communication

Source: Internet
Author: User
Tags htons

Socket programming based on UDP (not connection-oriented), divided into client and server side.

The process for the client is as follows:

(1) Create socket (socket)

(2) Communication with the server (sendto)

(3) Close socket

SendTo function: to send data to a specified destination, sendto () for sending UDP packets with no established connection

ssize_t sendto (int sockfd, const void *buf, size_t len, int flags,

const struct SOCKADDR *dest_addr, socklen_t Addrlen);


The return value is integer and, if successful, returns the number of bytes sent, and the failure returns SOCKET_ERROR.

SOCKFD: Sockets

BUF: Buffer for data to be sent

Len: Buffer length

Flags: Call way flag bit, typically 0, change flags, will change the form of sendto send: Blocking

addr: (optional) Pointers , the address that points to the destination socket

Addr:lenaddr the length of the address referred to

The server-side process is as follows:

(1) Create socket (socket)

(2) Bind sockets to a local address and port (BIND)

(3) Communicating with the client using the returned socket (recvfrom)

(4) Return, wait for another customer to request.

(5) Close the socket.

recvfrom function: This function is used from (connected) Socket Interface and captures the address of the source that sent the data.

#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);

SOCKFD: Identifies a connected Socket Interface description of the word.

buf: Receive Data Buffers .

Len: buffers length.

Flags: Invoke operation mode.

src_addr: (optional) Pointers , pointing to the buffer that contains the source address. --Output type parameter

Addrlen: (optional) pointer, pointing to the from buffer length value. --Input and output type parameters.

The return value is integer and, if successful, returns the number of bytes received, and the failure returns SOCKET_ERROR.

Server-Side #include<stdio.h>                                                                           #include <stdlib.h> #include <errno.h> #include <string.h> #include <sys/ socket.h> #include <sys/types.h> #include <netinet/in.h> #include <arpa/inet.h>void usage ( Const char* proc) {    printf ("%s [ip][port]\n");} Int main (int argc,char* argv[]) {    if (argc!=3) {         usage (argv[0]);        return 1;     }    int sOck=socket (af_inet,sock_dgram,0);     if (sock<0) {         perror ("socket");        return 2;     }    int _port=atoi (argv[2]);    char* _ip=argv[1];     struct sockaddr_in local;    local.sin_family=AF_INET;     local.sin_port=htons (_port);     local.sin_addr.s_addr=inet_addr (_ IP);     if (Bind (sock, (struct sockaddr*) &local,sizeof (local) <0) {         perror ("bind");         exit (1);     }    char buf[1024];    struct  sockaddr_in remote;    socklen_t len=sizeof (remote);     while (1)     {&Nbsp;       ssize_t _s=recvfrom (sock,buf,sizeof (BUF) -1,0, (struct  sockaddr*) &remote,&len)         if (_s>0)     &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;{&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;BUF[_S] = ' + ';             printf ("client:[ip:%s][port:%d")   %s ", Inet_ntoa (REMOTE.SIN_ADDR), Ntohs (Remote.sin_port), buf);          }         else if (_s==0) {             printf ("Client close");             break;        }         else        {             break;        }     }    close (sock);     return 0;} Client Side #include<stdio.h>                                                                           #include <stdlib.h> #include <errno.h> #include <sys/socket.h> #include <sys/ types.h> #include <netinet/in.h> #include <arpa/inet.h> #include <string.h>void usage ( Const char* proc) {    printf ("%s[ip][port]", proc);} Int main (int argc,char* argv[]) {    if (argc!=3) {        usage (argv[0]);         return 1;    }    int sock=socket (AF_INET,SOCK_ dgram,0)     if (sock<0) {        perror ("socket") );        return 2;    }     int _port=atoi (argv[2]);    char* _ip=argv[1];     struct sockaddr_in client;    client.sin_family=af_inet;     Client.sin_port=htons (_port);     client.sin_addr.s_addr=inet_addr (_IP);     char buf[1024];    while (1)     {         ssize_t _s=read (0,buf,sizeof (BUF)-1);         if (_s>0) {             buf[_s]= ';         }        _s=sendto (sock,buf,sizeof (BUF) -1,0, (struct  sockaddr*) &client,sizeof (client));     }    return 0;}

Run:

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/80/67/wKiom1dADyaBR6V6AAAzI-DUquY400.png "title=" 206. PNG "alt=" Wkiom1dadyabr6v6aaazi-duquy400.png "/>

This article is from the "Small Stop" blog, please be sure to keep this source http://10541556.blog.51cto.com/10531556/1775684

UDP----Socket Communication

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.