UDP目前还未正式发布。
一个UDP的例子:
// This example shows a simple implementation of UPnP-SSDP M-SEARCH discovery using a multicast UDPSocket
var address = '239.255.255.250',
port = '1900',
serviceType = 'upnp:rootdevice',
rn = 'rn',
search = ''
// Create a new UDP client socket
var mySocket = new UDPSocket()
// Build an SSDP M-SEARCH multicast message
search += 'M-SEARCH * HTTP/1.1' + rn
search += 'ST: ' + serviceType + rn
search += 'MAN: "ssdp:discover"' + rn
search += 'HOST: ' + address + ':' + port + rn
search += 'MX: 10'
// Receive and log SSDP M-SEARCH response messages
function receiveMSearchResponses() { // While data in buffer, read and log UDP message
while (mySocket.readable.state === "readable") {
var msg = mySocket.readable.read()
console.log ('Remote address: ' + msg.remoteAddress +
' Remote port: ' + msg.remotePort +
'Message: ' + ab2str(msg.data))
// ArrayBuffer to string conversion could also be done by piping
// through a transform stream. To be updated when the Streams API
// specification has been stabilized on this point.
}
// Wait for SSDP M-SEARCH responses to arrive
mySocket.readable.wait().then(
receiveMSearchResponses,
e =>console.error("Receiving error: ", e)
)
}
// Join SSDP multicast group
mySocket.joinMulticast(address)
// Send SSDP M-SEARCH multicast message
mySocket.writeable.write(
{data : str2ab(search),
remoteAddress : address,
remotePort : port
}).then(
() =>{
// Data sent sucessfully, wait for response
console.log('M-SEARCH Sent')
receiveMSearchResponses()
},
e =>console.error("Sending error: ", e)
)
// Log result of UDP socket setup.
mySocket.opened.then(
() =>{
console.log("UDP socket created sucessfully")
},
e =>console.error("UDP socket setup failed due to error: ", e)
)
// Handle UDP socket closed, either as a result of the application
// calling mySocket.close() or an error causing the socket to be
closed.
mySocket.closed.then(
() =>{
console.log("Socket has been cleanly closed")
},
e =>console.error("Socket closed due to error: ", e)
)
相比UDP,TCP的示例代码显得简单一些
//
// This example shows a simple TCP echo client.
// The client will send "Hello World" to the server on port **9 and log
// what has been received from the server.
//
// Create a new TCP client socket and connect to remote host
var mySocket = new TCPSocket("127.0.0.1", **9)
// Send data to server
mySocket.writeable.write("Hello World").then(
() =>{
// Data sent sucessfully, wait for response
console.log("Data has been sent to server")
mySocket.readable.wait().then(
() =>{
// Data in buffer, read it
console.log("Data received from server:" + mySocket.readable.read())
// Close the TCP connection
mySocket.close()
},
e =>console.error("Receiving error: ", e)
)
},
e =>console.error("Sending error: ", e)
)
// Signal that we won't be writing any more and can close the write half of the connection.
mySocket.halfClose()
// Log result of TCP connection attempt.
mySocket.opened.then(
() =>{
console.log("TCP connection established sucessfully")
},
e =>console.error("TCP connection setup failed due to error: ", e)
)
// Handle TCP connection closed, either as a result of the application
// calling mySocket.close() or the other side closed the TCP
// connection or an error causing the TCP connection to be closed.
mySocket.closed.then(
() =>{
console.log("TCP socket has been cleanly closed")
},
e =>console.error("TCP socket closed due to error: ", e)
)
HTML5好优点
网络标准
HTML5本身是由W3C推荐出来的,它的开发是通过谷歌、苹果,诺基亚、中国移动等几百家公司一起酝酿的技术,这个技术最大的好处在于它是一个公开的技术。换句话说,每一个公开的标准都可以根据W3C的资料库找寻根源。另一方面,W3C通过的HTML5标准也就意味着每一个浏览器或每一个平台都会去实现。
多设备跨平台
用HTML5的优点主要在于,这个技术可以进行跨平台的使用。比如你开发了一款HTML5的游戏,你可以很轻易地移植到UC的开放平台、Opera的游戏中心、Facebook应用平台,甚至可以通过封装的技术发放到App Store或Google Play上,所以它的跨平台性非常强大,这也是大多数人对HTML5有兴趣的主要原因。
自适应网页设计
很早就有人设想,能不能“一次设计,普遍适用”,让同一张网页自动适应不同大小的屏幕,根据屏幕宽度,自动调整布局(layout)。
lass Program{
static List<AppSession>_sessions = new List<AppSession>()
static void Main(string[] args)
{
Console.WriteLine("Press any key to start the server!")
Console.ReadKey()
Console.WriteLine()
var appServer = new AppServer()
/*
name: 服务器实例的名称
serverType: 服务器实例的类型的完整名称
serverTypeName: 所选用的服务器类型在 serverTypes 节点的名字,配置节点 serverTypes 用于定义所有可用的服务器类型,我们将在后面再做详细介绍
ip: 服务器监听的ip地址。你可以设置具体的地址,也可以设置为下面的值 Any - 所有的IPv4地址 IPv6Any - 所有的IPv6地址
port: 服务器监听的端口
listenBacklog: 监听队列的大小
mode: Socket服务器运行的模式, Tcp (默认) 或者 Udp
disabled: 服务器实例是否禁用了
startupOrder: 服务器实例启动顺序, bootstrap 将按照此值的顺序来启动多个服务器实例
sendTimeOut: 发送数据超时时间
sendingQueueSize: 发送队列最大长度, 默认值为5
maxConnectionNumber: 可允许连接的最大连接数
receiveBufferSize: 接收缓冲区大小
sendBufferSize: 发送缓冲区大小
syncSend: 是否启用同步发送模式, 默认值: false
logCommand: 是否记录命令执行的记录
logBasicSessionActivity: 是否记录session的基本活动,如连接和断开
clearIdleSession: true 或 false, 是否定时清空空闲会话,默认值是 false
clearIdleSessionInterval: 清空空闲会话的时间间隔, 默认值是120, 单位为秒
idleSessionTimeOut: 会话空闲超时时间当此会话空闲时间超过此值,同时clearIdleSession被配置成true时,此会话将会被关闭默认值为300,单位为秒
security: Empty, Tls, Ssl3. Socket服务器所采用的传输层加密协议,默认值为空
maxRequestLength: 最大允许的请求长度,默认值为1024
textEncoding: 文本的默认编码,默认值是 ASCII
defaultCulture: 此服务器实例的默认 thread culture, 只在.Net 4.5中可用而且在隔离级别为 'None' 时无效
disableSessionSnapshot: 是否禁用会话快照, 默认值为 false.
sessionSnapshotInterval: 会话快照时间间隔, 默认值是 5, 单位为秒
keepAliveTime: 网络连接正常情况下的keep alive数据的发送间隔, 默认值为 600, 单位为秒
keepAliveInterval: Keep alive失败之后, keep alive探测包的发送间隔,默认值为 60, 单位为秒
*/
var serverConfig = new ServerConfig
{
Port = 4141, //set the listening port
Ip = "Any",
Name = "XHS_Server",
//Other configuration options
Mode = SocketMode.Tcp,
MaxConnectionNumber = 100,
IdleSessionTimeOut = 600,
TextEncoding = "UTF-8",
SyncSend = true,
SendBufferSize = 1024,
ReceiveBufferSize = 1024,
LogBasicSessionActivity = true,
LogAllSocketException = true,
KeepAliveTime = 300
}
serverConfig.SyncSend = true
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)