SCTP客户端与服务器

  1 /**
  2  * @brief - Send a message, using advanced SCTP features
  3  * The sctp_sendmsg() function allows you to send extra information to a remote application.
  4  * Using advanced SCTP features, you can send a message through a specified stream,
  5  * pass extra opaque information to a remote application, or define a timeout for the particular message.
  6  *
  7  * @header - #include <netinet/sctp.h>
  8  *
  9  * @return ssize_t - The number of bytes sent, or -1 if an error occurs (errno is set).
 10  *
 11  * @ERRORS
 12  * EBADF
 13  *     An invalid descriptor was specified.
 14  * EDESTADDRREQ
 15  *     A destination address is required.
 16  * EFAULT
 17  *     An invalid user space address was specified for a parameter.
 18  * EMSGSIZE
 19  *     The socket requires that the message be sent atomically, but the size of the message made this impossible.
 20  * ENOBUFS
 21  *     The system couldn‘t allocate an internal buffer. The operation may succeed when buffers become available.
 22  * ENOTSOCK
 23  *     The argument s isn‘t a socket.
 24  * EWOULDBLOCK
 25  *     The socket is marked nonblocking and the requested operation would block.
 26  */
 27 ssize_t sctp_sendmsg(int s,                             /*! Socket descriptor. */
 28                      const void *msg,         /*! Message to be sent. */
 29                      size_t len,                     /*! Length of the message. */
 30                      struct sockaddr *to, /*! Destination address of the message. */
 31                      socklen_t tolen,         /*! Length of the destination address. */
 32                      uint32_t ppid,             /*! An opaque unsigned value that is passed to the remote end in each user message.
 33                                                                         The byte order issues are not accounted for and this information is passed opaquely
 34                                                                           by the SCTP stack from one end to the other. */
 35                      uint32_t flags,             /*! Flags composed of bitwise OR of these values:
 36                                                                          MSG_UNORDERED
 37                                                                              This flag requests the unordered delivery of the message.
 38                                                                              If the flag is clear, the datagram is considered an ordered send.
 39                                                                          MSG_ADDR_OVER
 40                                                                              This flag, in one-to-many style, requests the SCTP stack to override the primary destination address.
 41                                                                          MSG_ABORT
 42                                                                              This flag causes the specified association to abort -- by sending
 43                                                                              an ABORT message to the peer (one-to-many style only).
 44                                                                          MSG_EOF
 45                                                                              This flag invokes the SCTP graceful shutdown procedures on the specified association.
 46                                                                              Graceful shutdown assures that all data enqueued by both endpoints is successfully
 47                                                                              transmitted before closing the association (one-to-many style only). */
 48                      uint16_t stream_no,     /*! Message stream number -- for the application to send a message.
 49                                                                          If a sender specifies an invalid stream number, an error indication is returned and the call fails. */
 50                      uint32_t timetolive,    /*! Message time to live in milliseconds.
 51                                                                          The sending side expires the message within the specified time period
 52                                                                          if the message has not been sent to the peer within this time period.
 53                                                                          This value overrides any default value set using socket option.
 54                                                                          If you use a value of 0, it indicates that no timeout should occur on this message. */
 55                      uint32_t context);        /*! An opaque 32-bit context datum.
 56                                                                          This value is passed back to the upper layer if an error occurs while sending a message,
 57                                                                          and is retrieved with each undelivered message. */
 58
 59 #include <stdio.h>
 60 #include <stdlib.h>
 61 #include <string.h>
 62 #include <unistd.h>
 63 #include <time.h>
 64 #include <sys/socket.h>
 65 #include <sys/types.h>
 66 #include <netinet/in.h>
 67 #include <netinet/sctp.h>
 68 //#include "common.h"
 69 #define MAX_BUFFER  1024
 70 #define MY_PORT_NUM 19000
 71 #define LOCALTIME_STREAM    0
 72 #define GMT_STREAM   1
 73
 74 int main()
 75 {
 76     int listenSock, connSock, ret;
 77     struct sockaddr_in servaddr;
 78     char buffer[MAX_BUFFER + 1];
 79     time_t currentTime;
 80
 81     /* Create SCTP TCP-Style. Socket */
 82     listenSock = socket( AF_INET, SOCK_STREAM, IPPROTO_SCTP ); // 注意这里的IPPROTO_SCTP
 83     /* Accept connections from any interface */
 84     bzero( (void *)&servaddr, sizeof(servaddr) );
 85     servaddr.sin_family = AF_INET;
 86     servaddr.sin_addr.s_addr = htonl( INADDR_ANY );
 87     servaddr.sin_port = htons(MY_PORT_NUM);
 88     /* Bind to the wildcard address (all) and MY_PORT_NUM */
 89     ret = bind( listenSock,
 90                 (struct sockaddr *)&servaddr, sizeof(servaddr) );
 91     /* Place the server socket into the listening state */
 92     listen( listenSock, 5 );
 93     /* Server loop... */
 94     while( 1 )
 95     {
 96         /* Await a new client connection */
 97         connSock = accept( listenSock,
 98                            (struct sockaddr *)NULL, (int *)NULL );
 99         /* New client socket has connected */
100         /* Grab the current time */
101         currentTime = time(NULL);
102         /* Send local time on stream 0 (local time stream) */
103         snprintf( buffer, MAX_BUFFER, "%s\n", ctime(currentTime) );
104         ret = sctp_sendmsg( connSock,
105                             (void *)buffer, (size_t)strlen(buffer),
106                             NULL, 0, 0, 0, LOCALTIME_STREAM, 0, 0 );
107         /* Send GMT on stream 1 (GMT stream) */
108         snprintf( buffer, MAX_BUFFER, "%s\n",
109                   asctime( gmtime( currentTime ) ) );
110         ret = sctp_sendmsg( connSock,
111                             (void *)buffer, (size_t)strlen(buffer),
112                             NULL, 0, 0, 0, GMT_STREAM, 0, 0 );
113         /* Close the client connection */
114         close( connSock );
115     }
116     return 0;
117 }
118
119
120 #include <stdio.h>
121 #include <stdlib.h>
122 #include <string.h>
123 #include <unistd.h>
124 #include <sys/socket.h>
125 #include <sys/types.h>
126 #include <netinet/in.h>
127 #include <netinet/sctp.h>
128 #include <arpa/inet.h>
129 //#include "common.h"
130 #define MAX_BUFFER  1024
131 #define MY_PORT_NUM 19000
132 #define LOCALTIME_STREAM    0
133 #define GMT_STREAM   1
134
135 int main()
136 {
137     int connSock, in, i, flags;
138     struct sockaddr_in servaddr;
139     struct sctp_sndrcvinfo sndrcvinfo;
140     struct sctp_event_subscribe events;
141     char buffer[MAX_BUFFER + 1];
142     /* Create an SCTP TCP-Style. Socket */
143     connSock = socket( AF_INET, SOCK_STREAM, IPPROTO_SCTP );
144     /* Specify the peer endpoint to which we‘ll connect */
145     bzero( (void *)&servaddr, sizeof(servaddr) );
146     servaddr.sin_family = AF_INET;
147     servaddr.sin_port = htons(MY_PORT_NUM);
148     servaddr.sin_addr.s_addr = inet_addr( "127.0.0.1" );
149     /* Connect to the server */
150     connect( connSock, (struct sockaddr *)&servaddr, sizeof(servaddr) );
151     /* Enable receipt of SCTP Snd/Rcv Data via sctp_recvmsg */
152     memset( (void *)&events, 0, sizeof(events) );
153     events.sctp_data_io_event = 1;
154     setsockopt( connSock, SOL_SCTP, SCTP_EVENTS,
155                 (const void *)&events, sizeof(events) );
156     /* Expect two messages from the peer */
157     for (i = 0 ; i < 2 ; i++)
158     {
159         in = sctp_recvmsg( connSock, (void *)buffer, sizeof(buffer),
160                            (struct sockaddr *)NULL, 0,
161                            &sndrcvinfo, &flags );
162         /* Null terminate the incoming string */
163         buffer[in] = 0;
164         if (sndrcvinfo.sinfo_stream == LOCALTIME_STREAM)
165         {
166             printf("(Local) %s\n", buffer);
167         }
168         else if (sndrcvinfo.sinfo_stream == GMT_STREAM)
169         {
170             printf("(GMT  ) %s\n", buffer);
171         }
172     }
173     /* Close our socket and exit */
174     close(connSock);
175     return 0;
176 }
时间: 2024-10-11 03:39:34

SCTP客户端与服务器的相关文章

用java语言构建一个网络服务器,实现客户端和服务器之间通信,实现客户端拥有独立线程,互不干扰

服务器: 1.与客户端的交流手段多是I/O流的方式 2.对接的方式是Socket套接字,套接字通过IP地址和端口号来建立连接 3.(曾经十分影响理解的点)服务器发出的输出流的所有信息都会成为客户端的输入流,同时所有客户端的所有输出流都会包含在服务器的输入流中. (即套接字即使建立连接,输入输出流都是相对自己的而言的,向外发送自己的内部的信息都用输出流,接受外部的数据都使用输入流!) 简单服务器的代码实现: public static void main(String [] args){ try

介绍一款chrom浏览器插件 DHC是一款使用chrome模拟REST客户端向服务器发送测试数据的谷歌浏览器插件

先打个小广告哈 公司招java架构师,月薪25K以上,负责电商平台架构工作,工作地点在北京 1号线永安里站 附近,如有意向 请把简历发我邮箱[email protected] 可以内部推荐. DHC是一款使用chrome模拟REST客户端向服务器发送测试数据的谷歌浏览器插件. DHC的开发背景 在web开发中,服务器端和客户端的开发和测试必不可少,但是测试的工作往往需要服务器端完成之后,客户端才能进行测试,这无疑延后了测试流程,导致服务器端开发完成后,无法进行充分的数据测试,很容易造成服务器端和

java实现客户端向服务器发送文件的操作

服务器源代码: import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.ServerSocket;

10、使用TCP协议完成一个客户端一个服务器。客户端从键盘输入读取一个字符串,发送到服务器。 服务器接收客户端发送的字符串,反转之后发回客户端。客户端接收并打印。

/**10.使用TCP协议完成一个客户端一个服务器.客户端从键盘输入读取一个字符串,发送到服务器. 服务器接收客户端发送的字符串,反转之后发回客户端.客户端接收并打印. * 客户端*/ import java.io.*; import java.net.*; public class Test10_Client { public static void main(String[] args) throws Exception { Socket s = new Socket("192.168.0.

Android:解决客户端从服务器上获取数据乱码的方法

向服务器发送HTTP请求,接收到的JSON包为response,用String content = EntityUtils.toString(response.getEntity(),"utf-8");解码还是出现了中文乱码,在后面加了 String name = new String(response.getBytes("iso-8859-1"), "UTF-8"); 也无济于事.想到服务器好像是用URLENCODER编了码的,怀着试一试的态度

Android客户端与服务器交互方式-小结

最近的Android项目开发过程中一个问题困扰自己很长时间,Android客户端与服务器交互有几种方式,最常见的就是webservices和json.要在Android手机客户端与pc服务器交互,需要满足下面几种条件:跨平台.传输数据格式标准.交互方便. 为了与服务器通讯其实无非就两种协议HTTP和TCP,TCP的学习Socket,HTTP的话熟悉一下HTTP协议和相关Java API.而下面的几种方式就是从这两种协议扩展出来的:webservices soap.SSH的JSON(可参考:该链接

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

ZERO.前言 有关通信原理内容是在网上或百科整理得到,代码部分为本人所写,如果不当,还望指教. 一.Socket通信简介 Android与服务器的通信方式主要有两种,一是Http通信,一是Socket通信.两者的最大差异在于,http连接使用的是“请求—响应方式”,即在请求时建立连接通道,当客户端向服务器发送请求后,服务器端才能向客户端返回数据.而Socket通信则是在双方建立起连接后就可以直接进行数据的传输,在连接时可实现信息的主动推送,而不需要每次由客户端想服务器发送请求. 那么,什么是s

客户端和服务器地址前加“/”的不同

最近学习WEB开发这一块,对于客户端和服务器的提交.转发和重定向地址前面加"/"组一个小总结 (1)客户端提交表单是地址 创建表单页面(login.jsp),和一个处理提交数据页面(doLogin.jsp) 表单提交地址前面加"/" ,将会跳转服务器下根目录县的doLogin.jsp页面中,如图地址 表单提交地址前面不加"/",将会跳转到WEB项目的根目录下的doLogin.jsp页面中,如图地址 (2)服务器端转发和重定向 ****未完待续**

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

实验内容 1.掌握Socket程序的编写: 2.掌握密码技术的使用: 3.设计安全 4.对通信内容进行摘要计算并验证 实验步骤 1.信息安全传送: 发送方A——————>接收方B A加密时,用B的公钥 B解密时,用B的私钥 发送方A对信息(明文)采用DES密钥加密,使用RSA加密前面的DES密钥信息,最终将混合信息进行传递.同时用hash函数将明文进行用作验证.    接收方B接收到信息后,用RSA解密DES密钥信息,再用RSA解密获取到的密钥信息解密密文信息,最终就可以得到我们要的信息(明文)