//TCP and UDP can bind to the same IP & port.

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/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 1024 int 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, INET_ADDRSTRLEN ), ntohs( client_address.sin_port ) );
}
}
//UDP is readable
else if ( sockfd == udpfd )
{
char buf[ UDP_BUFFER_SIZE ];
memset( buf, '\0', 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 sockaddr* )&client_address, client_addrlength );
}
else if (ret == 0)
{
printf("client peer closed().\n");
//shutdown(udpfd); //udp套接字而言shutdown毫无意义
close(udpfd);
}
else
{
printf("UDP recvfrom ran into error: \n", errno);
//handle error....
}
}
//TCP connfd is readable
else if ( events[i].events & EPOLLIN )
{
char buf[ TCP_BUFFER_SIZE ];
while( 1 )
{
memset( buf, '\0', 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() socket.\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 what we have received! ECHO~
{
send( sockfd, buf, ret, 0 );
}
}
}
else
{
printf( "something else happened \n" );
}
}
} close( listenfd );
return 0;
}

  

linux socket TCP UDP bind 同义IP和port的更多相关文章

  1. 从Linux源码看Socket(TCP)的bind

    从Linux源码看Socket(TCP)的bind 前言 笔者一直觉得如果能知道从应用到框架再到操作系统的每一处代码,是一件Exciting的事情. 今天笔者就来从Linux源码的角度看下Server ...

  2. 27.Socket,TCP,UDP,HTTP基本通信原理

    Socket,TCP,UDP,HTTP基本通信原理(摘自百度): TCP.UDP,HTTP 底层通信都是通过 socket 套接字实现 网络上不同的计算机,也可以通信,那么就得使用网络套接字(sock ...

  3. SOCKET, TCP/UDP, HTTP, FTP 浅析

    SOCKET, TCP/UDP, HTTP, FTP (一)TCP/UDP,SOCKET,HTTP,FTP简析   TCP/IP是个协议组,可分为三个层次:网络层.传输层和应用层: 网络层:IP协议. ...

  4. 网络编程之socket(TCP,UDP)

    socket层 tcp协议和udp协议 1)Socket服务器编程 主要包括下面的几步: 1.打开socket 2.绑定到一个地址和端口 3.侦听进来的连接 4.接受连接 5.读写数据 (2)Sock ...

  5. 简单使用SOCKET,TCP,UDP模式之间的通信

    TCP(Transmission Control Protocol 传输控制协议)是一种面向连接的.可靠的.基于字节流的传输层通信协议,由IETF的RFC 793定义.在简化的计算机网络OSI模型中, ...

  6. TCP/UDP 、HTTP、IP 、socket 的关系。

    网络有上下分为7 层.物理层,数据链路层.网络层.会话层.应用层.传输层: IP协议位于网络层,IP和端口来控制网络流向: TCP.UDP是基于传输层.TCP保证三次握手.传递数据: UDP为不考虑是 ...

  7. day29 python 套接字socket TCP udp 形式发送信息的区别

    我们经常把socket翻译为套接字,socket是在应用层和传输层之间的一个抽象层,它把TCP/IP层复杂的操作抽象为几个简单的接口供应用层调用已实现进程在网络中通信. socket起源于UNIX,在 ...

  8. iOS socket TCP UDP

    TCP: 服务器: #import <Foundation/Foundation.h> #include <sys/socket.h> #include <netinet ...

  9. 网络编程 套接字socket TCP UDP

    网络编程与套接字 网络编程 网络编程是什么: ​ 网络通常指的是计算机中的互联网,是由多台计算机通过网线或其他媒介相互链接组成的 ​ 编写基于网络的应用程序的过程序称之为网络编程. 网络编程最主要的工 ...

随机推荐

  1. JQuery插件autocomplete使用说明文档

    项目中有时会用到ajax自动补全查询,就像Google的搜索框中那样,输入汉字或者字母的首个字母,则包含这个汉字或者字母的相关条目会显示出来供用户选择,该插件就是实现这样的功能的.autocomple ...

  2. 转载 为什么print在Python 3中变成了函数?

    转载自编程派http://codingpy.com/article/why-print-became-a-function-in-python-3/ 原作者:Brett Cannon 原文链接:htt ...

  3. Vue 使用 prerender-spa-plugin 添加loading

    主要配置代码: new PrerenderSPAPlugin({ staticDir: path.join(__dirname, 'dist'), routes: ['/', '/introducti ...

  4. Bug Bounty Reference

    https://github.com/ngalongc/bug-bounty-reference/blob/master/README.md#remote-code-execution Bug Bou ...

  5. BGM时长

    1.can u feel it 00:08-00:30 22s 2.纤夫的爱 00:43-00:54 11s 3.渡情 00:55-01:52 57s 4.nobody 01:56-02:25 29s ...

  6. Linux下USB suspend/resume源码分析【转】

    转自:http://blog.csdn.net/aaronychen/article/details/3928479 Linux下USB suspend/resume源码分析 Author:aaron ...

  7. win10 无法打开 APICloud Studio 2 的解决方案

    坑爹. 新搭建了系统   apicloud studio2  打开无反应 无任何报错提示 双击没有方案.弄了一天 最后搞定. . 百度搜索  win10    null.sys 替换进去 C:/Win ...

  8. Babel(抽象语法树,又称AST)

    文章:https://juejin.im/post/5a9315e46fb9a0633a711f25 https://github.com/jamiebuilds/babel-handbook/blo ...

  9. AngularJs(SPA)单页面SEO以及百度统计应用(下)

    苍苍之天不得久视,堂堂之地不得久履 当你小心翼翼的开启服务端渲染的同时,一个问题不得不注意,使用内存模式去保存渲染过的页面,这样服务断掉重启后,缓存也没有了,所以这里我们使用mongdodb进行本地化 ...

  10. Sqlserver中PIVOT行转列透视操作

    创建表: IF OBJECT_ID('T040_PRODUCT_SALES') IS NOT NULL DROP TABLE T040_PRODUCT_SALES create table T040_ ...