SCTP客户端与服务器
/**
* @brief - Send a message, using advanced SCTP features
* The sctp_sendmsg() function allows you to send extra information to a remote application.
* Using advanced SCTP features, you can send a message through a specified stream,
* pass extra opaque information to a remote application, or define a timeout for the particular message.
*
* @header - #include <netinet/sctp.h>
*
* @return ssize_t - The number of bytes sent, or -1 if an error occurs (errno is set).
*
* @ERRORS
* EBADF
* An invalid descriptor was specified.
* EDESTADDRREQ
* A destination address is required.
* EFAULT
* An invalid user space address was specified for a parameter.
* EMSGSIZE
* The socket requires that the message be sent atomically, but the size of the message made this impossible.
* ENOBUFS
* The system couldn't allocate an internal buffer. The operation may succeed when buffers become available.
* ENOTSOCK
* The argument s isn't a socket.
* EWOULDBLOCK
* The socket is marked nonblocking and the requested operation would block.
*/
ssize_t sctp_sendmsg(int s, /*! Socket descriptor. */
const void *msg, /*! Message to be sent. */
size_t len, /*! Length of the message. */
struct sockaddr *to, /*! Destination address of the message. */
socklen_t tolen, /*! Length of the destination address. */
uint32_t ppid, /*! An opaque unsigned value that is passed to the remote end in each user message.
The byte order issues are not accounted for and this information is passed opaquely
by the SCTP stack from one end to the other. */
uint32_t flags, /*! Flags composed of bitwise OR of these values:
MSG_UNORDERED
This flag requests the unordered delivery of the message.
If the flag is clear, the datagram is considered an ordered send.
MSG_ADDR_OVER
This flag, in one-to-many style, requests the SCTP stack to override the primary destination address.
MSG_ABORT
This flag causes the specified association to abort -- by sending
an ABORT message to the peer (one-to-many style only).
MSG_EOF
This flag invokes the SCTP graceful shutdown procedures on the specified association.
Graceful shutdown assures that all data enqueued by both endpoints is successfully
transmitted before closing the association (one-to-many style only). */
uint16_t stream_no, /*! Message stream number -- for the application to send a message.
If a sender specifies an invalid stream number, an error indication is returned and the call fails. */
uint32_t timetolive, /*! Message time to live in milliseconds.
The sending side expires the message within the specified time period
if the message has not been sent to the peer within this time period.
This value overrides any default value set using socket option.
If you use a value of 0, it indicates that no timeout should occur on this message. */
uint32_t context); /*! An opaque 32-bit context datum.
This value is passed back to the upper layer if an error occurs while sending a message,
and is retrieved with each undelivered message. */ #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netinet/sctp.h>
//#include "common.h"
#define MAX_BUFFER 1024
#define MY_PORT_NUM 19000
#define LOCALTIME_STREAM 0
#define GMT_STREAM 1 int main()
{
int listenSock, connSock, ret;
struct sockaddr_in servaddr;
char buffer[MAX_BUFFER + ];
time_t currentTime; /* Create SCTP TCP-Style. Socket */
listenSock = socket( AF_INET, SOCK_STREAM, IPPROTO_SCTP ); // 注意这里的IPPROTO_SCTP
/* Accept connections from any interface */
bzero( (void *)&servaddr, sizeof(servaddr) );
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl( INADDR_ANY );
servaddr.sin_port = htons(MY_PORT_NUM);
/* Bind to the wildcard address (all) and MY_PORT_NUM */
ret = bind( listenSock,
(struct sockaddr *)&servaddr, sizeof(servaddr) );
/* Place the server socket into the listening state */
listen( listenSock, );
/* Server loop... */
while( )
{
/* Await a new client connection */
connSock = accept( listenSock,
(struct sockaddr *)NULL, (int *)NULL );
/* New client socket has connected */
/* Grab the current time */
currentTime = time(NULL);
/* Send local time on stream 0 (local time stream) */
snprintf( buffer, MAX_BUFFER, "%s\n", ctime(currentTime) );
ret = sctp_sendmsg( connSock,
(void *)buffer, (size_t)strlen(buffer),
NULL, , , , LOCALTIME_STREAM, , );
/* Send GMT on stream 1 (GMT stream) */
snprintf( buffer, MAX_BUFFER, "%s\n",
asctime( gmtime( currentTime ) ) );
ret = sctp_sendmsg( connSock,
(void *)buffer, (size_t)strlen(buffer),
NULL, , , , GMT_STREAM, , );
/* Close the client connection */
close( connSock );
}
return ;
} #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netinet/sctp.h>
#include <arpa/inet.h>
//#include "common.h"
#define MAX_BUFFER 1024
#define MY_PORT_NUM 19000
#define LOCALTIME_STREAM 0
#define GMT_STREAM 1 int main()
{
int connSock, in, i, flags;
struct sockaddr_in servaddr;
struct sctp_sndrcvinfo sndrcvinfo;
struct sctp_event_subscribe events;
char buffer[MAX_BUFFER + ];
/* Create an SCTP TCP-Style. Socket */
connSock = socket( AF_INET, SOCK_STREAM, IPPROTO_SCTP );
/* Specify the peer endpoint to which we'll connect */
bzero( (void *)&servaddr, sizeof(servaddr) );
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(MY_PORT_NUM);
servaddr.sin_addr.s_addr = inet_addr( "127.0.0.1" );
/* Connect to the server */
connect( connSock, (struct sockaddr *)&servaddr, sizeof(servaddr) );
/* Enable receipt of SCTP Snd/Rcv Data via sctp_recvmsg */
memset( (void *)&events, , sizeof(events) );
events.sctp_data_io_event = ;
setsockopt( connSock, SOL_SCTP, SCTP_EVENTS,
(const void *)&events, sizeof(events) );
/* Expect two messages from the peer */
for (i = ; i < ; i++)
{
in = sctp_recvmsg( connSock, (void *)buffer, sizeof(buffer),
(struct sockaddr *)NULL, ,
&sndrcvinfo, &flags );
/* Null terminate the incoming string */
buffer[in] = ;
if (sndrcvinfo.sinfo_stream == LOCALTIME_STREAM)
{
printf("(Local) %s\n", buffer);
}
else if (sndrcvinfo.sinfo_stream == GMT_STREAM)
{
printf("(GMT ) %s\n", buffer);
}
}
/* Close our socket and exit */
close(connSock);
return ;
}
SCTP客户端与服务器的更多相关文章
- Android客户端与服务器
就是普通的服务器端编程,还不用写界面,其实还比服务器编程简单一些.跟J2EE一样的服务器,你android这一方面只要用json或者gson直接拿数据,后台的话用tomcat接受请求操作数据,功能不复 ...
- Socket与SocketServer结合多线程实现多客户端与服务器通信
需求说明:实现多客户端用户登录,实现多客户端登录一般都需要使用线程技术: (1)创建服务器端线程类,run()方法中实现对一个请求的响应处理: (2)修改服务器端代码,实现循环监听状态: (3)服务器 ...
- xmpp笔记2(客户端到服务器的例子)--xml
xmpp( 客户端到服务器的例子 ) 1 步:客户端初始流给服务器: <stream:stream xmlns='jabber:client' xmlns:stream='http://ethe ...
- [ActionScript 3.0] NetConnection建立客户端与服务器的双向连接
一个客户端与服务器之间的接口测试的工具 <?xml version="1.0" encoding="utf-8"?> <!--- - - - ...
- Oracle客户端与服务器字符集不统一的处理
当Oracle客户端与服务器的字符集不统一时. 症状: 如:ORA-00283: ?????????? 提示信息中有好多问号. 解决方法: 1查询服务器的字符集: SQL> conn / as ...
- SignalR一个集成的客户端与服务器库。内部的两个对象类:PersistentConnection和Hub
SignalR 将整个交换信息的行为封装得非常漂亮,客户端和服务器全部都使用 JSON 来沟通,在服务器端声明的所有 hub 的信息,都会一般生成 JavaScript 输出到客户端. 它是基于浏览器 ...
- Java实验四 TCP客户端和服务器的应用
实验内容 1.掌握Socket程序的编写: 2.掌握密码技术的使用: 3.设计安全 4.对通信内容进行摘要计算并验证 实验步骤 1.信息安全传送: 发送方A——————>接收方B A加密时,用B ...
- Socket 通信原理(Android客户端和服务器以TCP&&UDP方式互通)
转载地址:http://blog.csdn.net/mad1989/article/details/9147661 ZERO.前言 有关通信原理内容是在网上或百科整理得到,代码部分为本人所写,如果不当 ...
- nodejs的cs模式聊天客户端和服务器实现
学习完nodejs的基础后,自然要写点东西练练手,以下是一个基于nodejs的cs模式的聊天软件代码: net模块是nodejs的网络编程必定用到的一个模块,对socket通信进行了封装 实现的功能: ...
随机推荐
- 21天实战caffe笔记_第二天
1 传统机器学习 传统机器学习:通过人工设计特征提取器,将原始数据转化为合适的中间表示形式或者特征向量,利用学习系统(通常为分类器)可以对输入模式进行检测或者分类.流程如下: 传统机器学习的局限在于需 ...
- bootstrap.yml与application.yml的区别
说明:其实yml和properties文件是一样的原理,主要是说明application和bootstrap的加载顺序.且一个项目上要么yml或者properties,二选一的存在. Bootstra ...
- insert sort
插入排序将数据分为前面有序部分和后面无序部分,取无序部分的第一个元素插入到有序序列中. 注意与选择排序的区别. // insert sortvoid insertionSort(int arr[], ...
- bzoj 4919 [Lydsy1706月赛]大根堆 set启发式合并+LIS
4919: [Lydsy1706月赛]大根堆 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 599 Solved: 260[Submit][Stat ...
- 实验四:终极改造之使用EF
回顾一下我们前面经过改造后的程序代码: (1)Listing.aspx:负责将Product对象集合(产品集合)按要求显示出来 (2)Repository.cs:负责读将数据库中读到的数据转换成Pro ...
- Android Studio 安装在Windows10中的陷阱
操作系统:Windows 10 Pro CPU:AMD IDE:Android Studio 2.0 JDK:8.0 安装完AS(Android Studio)之后,运行AS发现无法启动模拟器,提示“ ...
- LINUX的文件按时间排序
转载 2014年12月29日 00:49:23 20298 > ls -alt # 按修改时间排序 > ls --sort=time -la # 等价于> ls -alt > ...
- Docker入门与应用系列(二)镜像管理
1.1 什么是镜像 简单说,Docker镜像是一个不包含Linux内核而又精简的Linux操作系统. 1.2 镜像从哪里来 Docker Hub是由Docker公司负责维护的公共注册中心,包含大量的容 ...
- Java基础-Calendar类常用方法介绍
Java基础-Calendar类常用方法介绍 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.Calendar类概念 Calendar 类是一个抽象类,它为特定瞬间与一组诸如 Y ...
- element ui 栅格布局
<el-row> <el-col :span="24"><div class="grid-content bg-purple-dark&qu ...