/**
* @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客户端与服务器的更多相关文章

  1. Android客户端与服务器

    就是普通的服务器端编程,还不用写界面,其实还比服务器编程简单一些.跟J2EE一样的服务器,你android这一方面只要用json或者gson直接拿数据,后台的话用tomcat接受请求操作数据,功能不复 ...

  2. Socket与SocketServer结合多线程实现多客户端与服务器通信

    需求说明:实现多客户端用户登录,实现多客户端登录一般都需要使用线程技术: (1)创建服务器端线程类,run()方法中实现对一个请求的响应处理: (2)修改服务器端代码,实现循环监听状态: (3)服务器 ...

  3. xmpp笔记2(客户端到服务器的例子)--xml

    xmpp( 客户端到服务器的例子 ) 1 步:客户端初始流给服务器: <stream:stream xmlns='jabber:client' xmlns:stream='http://ethe ...

  4. [ActionScript 3.0] NetConnection建立客户端与服务器的双向连接

    一个客户端与服务器之间的接口测试的工具 <?xml version="1.0" encoding="utf-8"?> <!--- - - - ...

  5. Oracle客户端与服务器字符集不统一的处理

    当Oracle客户端与服务器的字符集不统一时. 症状: 如:ORA-00283: ?????????? 提示信息中有好多问号. 解决方法: 1查询服务器的字符集: SQL> conn / as ...

  6. SignalR一个集成的客户端与服务器库。内部的两个对象类:PersistentConnection和Hub

    SignalR 将整个交换信息的行为封装得非常漂亮,客户端和服务器全部都使用 JSON 来沟通,在服务器端声明的所有 hub 的信息,都会一般生成 JavaScript 输出到客户端. 它是基于浏览器 ...

  7. Java实验四 TCP客户端和服务器的应用

    实验内容 1.掌握Socket程序的编写: 2.掌握密码技术的使用: 3.设计安全 4.对通信内容进行摘要计算并验证 实验步骤 1.信息安全传送: 发送方A——————>接收方B A加密时,用B ...

  8. Socket 通信原理(Android客户端和服务器以TCP&&UDP方式互通)

    转载地址:http://blog.csdn.net/mad1989/article/details/9147661 ZERO.前言 有关通信原理内容是在网上或百科整理得到,代码部分为本人所写,如果不当 ...

  9. nodejs的cs模式聊天客户端和服务器实现

    学习完nodejs的基础后,自然要写点东西练练手,以下是一个基于nodejs的cs模式的聊天软件代码: net模块是nodejs的网络编程必定用到的一个模块,对socket通信进行了封装 实现的功能: ...

随机推荐

  1. fzyjojP2963 -- [校内训练20161227]疫情控制问题

    (题干中的废话已经划去) dp显而易见 收益为负数的可以直接扔掉不管.不要一定更优 子串问题,考虑SAM 建立广义SAM 尝试匹配,匹配到的位置的parent树祖先如果有完整的串,那么可以从这个串转移 ...

  2. JAVA中的File.separate(跨平台路径)

    转: JAVA中的File.separate(跨平台路径) 2016年03月27日 23:33:50 才不是本人 阅读数:1952   在Windows下的路径分隔符和Linux下的路径分隔符是不一样 ...

  3. C++模版详解(-)

    C++模版:       模版时C++支持多参数多态的工具,使用模版可以为用户为类或函数声明一般模式,使得类的数据成员,或者成员函数的参数,返回值取得任意类型. 模版是一种对类型进行参数化的工具: 通 ...

  4. laravel 实用扩展包

    1.beyondcode / laravel-self-diagnosis 环境检测.检测 php 版本.扩展 是否正常,数据库连接是否正常等 2.nunomaduro/larastan larave ...

  5. struts的问题

    将SSH框架进行整合的时候,将三者的jar包加入到lib下面,然后测试struts,结果页面显示不出来报404错误,可是路径没有问题 找到罪魁祸首是:原因两个:(1)在未用到spring的时候,先不要 ...

  6. 转:Xcode 删除文件后编译出现的missing file的警告

    进入“Missing File”对应的目录进行删除即可. 1.由于使用SVN导致的,可进行如下操作: # cd ~/iHost/Demo/sfsimonutility/SFSimonUtility/S ...

  7. vue、入门

    入门vue v-on:click:chang   绑定事件点击 生面周期,整个vue的执行过程,他的应用执行了生面周期,也就是执行过程,这个执行过程如下图表,我们可以参考下图,也可以访问官方网址:ht ...

  8. Python练习-基于socket的FTPServer

    # 编辑者:闫龙 import socket,json,struct class MySocket: with open("FtpServiceConfig","r&qu ...

  9. 【译】第十一篇 Integration Services:日志记录

    本篇文章是Integration Services系列的第十一篇,详细内容请参考原文. 简介在前一篇,我们讨论了事件行为.我们分享了操纵事件冒泡默认行为的方法,介绍了父子模式.在这一篇,我们会配置SS ...

  10. 【译】第九篇 Replication:复制监视器

    本篇文章是SQL Server Replication系列的第九篇,详细内容请参考原文. 复制监视器允许你查看复制配置组件的健康状况.这一篇假设你遵循前八篇,并且你已经有一个合并发布和事务发布.启动复 ...