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通信进行了封装 实现的功能: ...
随机推荐
- NOIWC2017&&THUWC2017 滚粗记
因为NOI WC的时候一直在生病,浑浑噩噩就过去了7天,基本没什么记忆了,所以就压到一篇里好了. day -1 第一次发现高铁的椅子原来还可以转过来,于是我们四个小伙伴面对面愉快的打了一路宣红A. 在 ...
- 简短的创建Ajax对象代码
假如你的脚本只针对某个浏览器开发,那么创建XMLHTTP是很简单的一件事,用XMLHttpRequest或者ActiveXObject即可.但事实上绝大多数的时候,我们都要考虑兼容,于是我们通常写成: ...
- 利用SHELL的PROMPT_COMMAND添加日志审计功能,实时记录任何用户的操作到日志文件中
利用 PROMPT_COMMAND 实现命令审计功能:记录什么用户,在什么时间,做了什么操作,然后将查到的信息记录到一个文件里. 具体操作: 将以下内容追加到/etc/profile: ####### ...
- linux环境下安装PHP扩展swoole
swoole linux环境下的安装 最近在折腾一个伪直播页面,需求中有用到评论 开始在想直接ajax直接实现,不过想了想觉得对数据库读写太过频繁 而且对服务器压力也挺大的 百度一番发现了这么个东西 ...
- Python【sys】模块和【hashlib】模块
import sysimport osprint(sys.platform) #判断操作系统,windows10输出win32print("sys.path:",sys.path) ...
- 用js获取客户端IP地址
<script src="http://pv.sohu.com/cityjson?ie=utf-8"></script> <script type=& ...
- python---CRM用户关系管理
Day1:项目分析 一:需求分析 二:CRM角色功能介绍 三:业务场景分析 销售: .销售A 从百度推广获取了一个客户,录入了CRM系统,咨询了Python课程,但是没有报名 .销售B 从qq群获取一 ...
- node的“宏任务(macro-task)”和“微任务(micro-task)”机制
macrotask 和 microtask 表示异步任务的两种分类.在挂起任务时,JS 引擎会将所有任务按照类别分到这两个队列中,首先在 macrotask 的队列(这个队列也被叫做 task que ...
- javascript 千分
var str = '123456789'; function division(str){ var arr = str.split(''), len = arr.length, i = 3; whi ...
- c++刷题(15/100)矩阵转置,最深子树
题目一:矩阵转置 给定一个矩阵 A, 返回 A 的转置矩阵. 矩阵的转置是指将矩阵的主对角线翻转,交换矩阵的行索引与列索引. 示例 1: 输入:[[1,2,3],[4,5,6],[7,8,9]] 输出 ...