/**
* @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. golang json 编码解码

    json 编码 package main import ( "encoding/json" "fmt" ) type Person struct { Name ...

  2. 四、java面向对象编程_2

    目录 六.对象的创建和使用 七.this关键字 八.static关键字 九.package和import语句 十.类的继承 十一.访问控制 十二.方法的重写 十三.super关键字 十四.继承中的构造 ...

  3. tp 事务处理

    tp的事务开启是非常简单的, 只需要M()->startTrans();//开启事务,M()可以是M('xxx') $m->rollback();//事务回滚 $m->commit( ...

  4. Java基础之疑难知识点

    问题列表: 1. Java中子类中可以有与父类相同的属性名吗? 2. Java中子类继承了父类的私有属性及方法吗? 3. Java中抽象类到底能不能被实例化? 1. Java中子类中可以有与父类相同的 ...

  5. OpenStack安装部署(二)

    中文文档:http://docs.openstack.org/mitaka/zh_CN/install-guide-rdo/提示:这个中文文档是直接翻译过来的,所以会有很多不通顺的地方. 服务介绍 M ...

  6. U33405 纽约

    U33405 纽约 花费 \(w\) 元可以购买一辆容量为 \(w\) 的车 现在你有 \(n <= 2000\) 个物品, 搬运策略: 一直搬能放下里面最重的, 直到任意物品都不能搬上为止 求 ...

  7. 使用spring boot访问mongodb数据库

    一. spring boot中传参的方法 1.自动化配置 spring Boot 对于开发人员最大的好处在于可以对 Spring 应用进行自动配置.Spring Boot 会根据应用中声明的第三方依赖 ...

  8. 七、Kafka 用户日志上报实时统计之编码实践

    一.数据生产实现 1.配置数据生产模块 项目基础配置所包含的内容,如下所示: •项目工程的文件配置 •集群连接信息配置 •开发演示 2.实现 Flume 到 Kafka 模块 实现 Flume 到 K ...

  9. 【学习DIV+CSS】2. 学习CSS(一)--CSS控制页面样式

    1. CSS如何控制页面 使用XHTML+CSS布局页面,其中有一个很重要的特点就是“结构与表现相分离”(结构指XHTML,表现指CSS).有人这样描述这种分离的关系,结构XHTML好比一个人,表现C ...

  10. [hadoop]hadoop api 新版本与旧版本的差别

    突然现在对以后的职业方向有些迷茫,不知道去干什么,现在有一些语言基础,相对而言好的一些有Java和C,选来选去不知道该选择哪个方向,爬了好多网页后,觉得自己应该从java开始出发,之前有点心不在焉,不 ...