博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
TCP带外数据读写
阅读量:6285 次
发布时间:2019-06-22

本文共 2656 字,大约阅读时间需要 8 分钟。

 

#include 
#include
#include
#include
#include
#include
#include
#include
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] ); struct sockaddr_in server_address; bzero( &server_address, sizeof( server_address ) ); server_address.sin_family = AF_INET; inet_pton( AF_INET, ip, &server_address.sin_addr ); server_address.sin_port = htons( port ); int sockfd = socket( PF_INET, SOCK_STREAM, 0 ); assert( sockfd >= 0 ); if ( connect( sockfd, ( struct sockaddr* )&server_address, sizeof( server_address ) ) < 0 ) { printf( "connection failed\n" ); } else { printf( "send oob data out\n" ); const char* oob_data = "abc"; const char* normal_data = "123"; send( sockfd, normal_data, strlen( normal_data ), 0 ); send( sockfd, oob_data, strlen( oob_data ), MSG_OOB ); send( sockfd, normal_data, strlen( normal_data ), 0 ); } close( sockfd ); return 0;}

 

#include 
#include
#include
#include
#include
#include
#include
#include
#include
#define BUF_SIZE 1024int 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] ); 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 sock = socket( PF_INET, SOCK_STREAM, 0 ); assert( sock >= 0 ); int ret = bind( sock, ( struct sockaddr* )&address, sizeof( address ) ); assert( ret != -1 ); ret = listen( sock, 5 ); assert( ret != -1 ); struct sockaddr_in client; socklen_t client_addrlength = sizeof( client ); int connfd = accept( sock, ( struct sockaddr* )&client, &client_addrlength ); if ( connfd < 0 ) { printf( "errno is: %d\n", errno ); } else { char buffer[ BUF_SIZE ]; memset( buffer, '\0', BUF_SIZE ); ret = recv( connfd, buffer, BUF_SIZE-1, 0 ); printf( "got %d bytes of normal data '%s'\n", ret, buffer ); memset( buffer, '\0', BUF_SIZE ); ret = recv( connfd, buffer, BUF_SIZE-1, MSG_OOB ); printf( "got %d bytes of oob data '%s'\n", ret, buffer ); memset( buffer, '\0', BUF_SIZE ); ret = recv( connfd, buffer, BUF_SIZE-1, 0 ); printf( "got %d bytes of normal data '%s'\n", ret, buffer ); close( connfd ); } close( sock ); return 0;}

  

转载于:https://www.cnblogs.com/wuxinliulei/p/4876227.html

你可能感兴趣的文章
10年重新出发
查看>>
2019年-年终总结
查看>>
聊聊elasticsearch的RoutingService
查看>>
让人抓头的Java并发(一) 轻松认识多线程
查看>>
从源码剖析useState的执行过程
查看>>
地包天如何矫正?
查看>>
中间件
查看>>
Android SharedPreferences
查看>>
css面试题
查看>>
Vue组建通信
查看>>
用CSS画一个带阴影的三角形
查看>>
前端Vue:函数式组件
查看>>
程鑫峰:1.26特朗.普力挺美元力挽狂澜,伦敦金行情分析
查看>>
safari下video标签无法播放视频的问题
查看>>
01 iOS中UISearchBar 如何更改背景颜色,如何去掉两条黑线
查看>>
对象的继承及对象相关内容探究
查看>>
Spring: IOC容器的实现
查看>>
Serverless五大优势,成本和规模不是最重要的,这点才是
查看>>
Nginx 极简入门教程!
查看>>
iOS BLE 开发小记[4] 如何实现 CoreBluetooth 后台运行模式
查看>>