Linux socket TCP UDP bind synonymous IP and port

Source: Internet
Author: User
Tags assert epoll readable htons

TCP and UDP can bind to the same IP & port. #include <sys/types.h> #include <sys/socket.h> #include <net inet/in.h> #include <arpa/inet.h> #include <assert.h> #include <stdio.h> #include <unistd.h > #include <errno.h> #include <string.h> #include <fcntl.h> #include <stdlib.h> #include < sys/epoll.h> #include <pthread.h> #define Max_event_number 1024#define tcp_buffer_size 512#define udp_buffer_    SIZE 1024int setnonblocking (int fd) {int old_option = Fcntl (fd, F_GETFL); int new_option = Old_option |    O_nonblock;    Fcntl (FD, F_SETFL, new_option); return old_option;}    void addfd (int epollfd, int fd) {epoll_event event;    EVENT.DATA.FD = FD; event.events = Epollin |    Epollet; Event.events = Epollin;    Concern about Read Event Epoll_ctl (EPOLLFD, Epoll_ctl_add, FD, &event); Setnonblocking (FD);} int main (int argc, char* argv[]) {if (argc <= 2) {printf ("Usage:%s ip_addreSS port_number\n ", BaseName (Argv[0]));    return 1;    } const char* IP = argv[1];    int port = atoi (argv[2]);    int ret = 0;    struct SOCKADDR_IN address;    Bzero (&address, sizeof (address));    address.sin_family = af_inet;    Inet_pton (Af_inet, IP, &address.sin_addr);    Address.sin_port = htons (port);    int LISTENFD = socket (pf_inet, sock_stream, 0);    ASSERT (LISTENFD >= 0);    ret = bind (LISTENFD, (struct sockaddr*) &address, sizeof (address));    ASSERT (Ret! =-1);    RET = Listen (LISTENFD, 5);    ASSERT (Ret! =-1);    Bzero (&address, sizeof (address));    address.sin_family = af_inet;    Inet_pton (Af_inet, IP, &address.sin_addr);    Address.sin_port = htons (port);    int UDPFD = socket (pf_inet, SOCK_DGRAM, 0);    ASSERT (UDPFD >= 0);    ret = bind (UDPFD, (struct sockaddr*) &address, sizeof (address));    ASSERT (Ret! =-1);    Epoll_event events[Max_event_number]; int EPOLLFD = Epoll_create ( 5);    ASSERT (EPOLLFD! =-1);    ADDFD (EPOLLFD, LISTENFD);    ADDFD (EPOLLFD, UDPFD);        while (1) {int number = Epoll_wait (EPOLLFD, events, Max_event_number,-1);            if (number < 0) {printf ("Epoll failure\n");        Break            } for (int i = 0; i < number; i++) {int sockfd = EVENTS[I].DATA.FD;                if (sockfd = = listenfd) {struct sockaddr_in client_address;                socklen_t client_addrlength = sizeof (client_address);                int CONNFD = Accept (LISTENFD, (struct sockaddr*) &client_address, &client_addrlength);    if (CONNFD < 0) {printf ("errno is:%d\n", errno);                } else {ADDFD (EPOLLFD, CONNFD);        Char Remote[inet_addrstrlen]; printf ("Connected client IP:%s and port:%d\n", Inet_ntop (Af_inet, &client_address.sin_addr, remote, in Et_addrstrlen), NtohS (client_address.sin_port)); }}//udp is readable else if (sockfd = = udpfd) {char buf[U                Dp_buffer_size];                memset (buf, ' udp_buffer_size ');                struct sockaddr_in client_address;                socklen_t client_addrlength = sizeof (client_address);                ret = Recvfrom (UDPFD, buf, udp_buffer_size-1, 0, (struct sockaddr*) &client_address, &client_addrlength); if (Ret > 0) {sendto (UDPFD, buf, udp_buffer_size-1, 0, (struct sockadd                r*) &client_address, client_addrlength);                } else if (ret = = 0) {printf ("client peer closed () \ n");   Shutdown (UDPFD);                UDP sockets shutdown meaningless close (UDPFD);                } else {printf ("UDP Recvfrom ran into error: \ n", errno); HandlE error ....} }//tcp CONNFD is readable else if (events[i].events & Epollin) {ch                Ar buf[tcp_buffer_size];                    while (1) {memset (buf, ' n ', tcp_buffer_size);                    SOCKFD is nonblock ret = recv (SOCKFD, buf, tcp_buffer_size-1, 0); if (Ret < 0) {if (errno = = Eagain) | | (errno = = Ewouldblock) | |                        (errno = = eintr))                        {break; } else {printf ("TCP recv ran into error, close () Soc                        Ket.\n ");                        Close (SOCKFD);                        Break                    }} else if (ret = = 0)//peer called close () and sent a FIN to here               {     printf ("TCP recv return 0:peer client close (), so Close () self.\n");                    Close (SOCKFD); } else//normal, send back, and we have received!                    echo~ {Send (SOCKFD, buf, ret, 0);            }}} else {printf ("something else happened \ n");    }}} close (LISTENFD); return 0;}

  

Linux socket TCP UDP bind synonymous IP and port

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.