因为使用的是TCP协议不需要分段,head为4字节,存储整个数据包长度,加密模式开启时为20字节,后16字节存储IV。收到一个完整的数据包后去掉head部分交由应用层处理,发送时也一样由应用层提交发送请求后添加head部分再加入待发送队列。
项目地址
应用层接口,数据传输实现不对应用层开放
数据读写类,写入时自动检查扩容
常量字段,head部分长度,加密使用的私钥存于本地
C#数据交互服务器(二)
C#数据交互服务器(三)
Rijndael加密算法
WinSock IO模型实现首先不要管安卓端还是苹果端,现在一般都是响应式的app,你放到安卓或者苹果或者pc或者平板都是没有问题的。一般采用的是>客户端简单代码:
Imports SystemNetSockets
Imports SystemIO
Private Output As NetworkStream
Private Writer As BinaryWriter
Private Reader As BinaryReader
Dim StrSend As String
Dim client As New TcpClient '
clientConnect(servIP, 2006)
Output = clientGetStream
Writer = New BinaryWriter(Output)
Reader = New BinaryReader(Output)
StrSend = "字符串"
SystemThreadingThreadSleep(100)
WriterWrite(StrSend)
Try
Message = ReaderReadString
If Message = "OK" Then
LBinfoText = "成功!" '测试
Else
LBinfoText = "失败!"
End If
SystemThreadingThreadSleep(100)
Catch ex As Exception
MessageBoxShow("Client Application Closing!")
Finally
WriterClose()
ReaderClose()
OutputClose()
clientClose()
End Try
服务器端
用线程
Imports SystemNetSockets
Imports SystemIO
Imports SystemThreading
Imports SystemWindowsForms
Private Connection As Socket
Private readThread As Thread
Private SocketStream As NetworkStream
Private Writer As BinaryWriter
Private Reader As BinaryReader
Public Sub New()
MyBaseNew()
'该调用是 Windows 窗体设计器所必需的。
InitializeComponent()
'在 InitializeComponent() 调用之后添加任何初始化
readThread = New Thread(AddressOf RunServer)
readThreadStart()
End Sub
Public Sub RunServer()
Dim Listener As TcpListener
Try
Listener = New TcpListener(NetIPAddressParse("192168116"), 2006)
ListenerStart()
While True
Connection = ListenerAcceptSocket 'accept an incoming connection
SocketStream = New NetworkStream(Connection) 'create networkstream object associated with socket
Writer = New BinaryWriter(SocketStream) 'create object for transferring data across stream
Reader = New BinaryReader(SocketStream) 'read string data sent from client
Try
Do
strIncept = ReaderReadString
strIncept = DataDisp(strIncept) '此处插入数据处理程序()
SendInfoToClient(strIncept) 'send to client
Loop While ConnectionConnected
Catch ex As Exception
MsgBox(exToString)
Finally
WriterClose()
ReaderClose()
SocketStreamClose()
End Try
End While
Catch ex As Exception
MsgBox(exToString)
End Try
End Sub
Public Sub SendInfoToClient(ByVal Info As String)
'Send to client
Try
If ConnectionConnected Then
WriterWrite(Info)
ConnectionClose()
End If
Catch ex As SocketException
MessageBoxShow(exToString)
End Try
End Sub
整个过程都有了ASPNET(C#)提供的常用内置对象有
1 Request: 接收客户端信息
2 Response: 传送信息给客户端。
至于具体的用法,你可在网上百度哈。一种实现不同系统之间数据交互的方法,将需要服务的客户进程与提供服务的服务进程通过接口代理进行交互,该接口代理接收命令行参数并独立运行
可以总结为向第三方系统推送数据或从服务器拉回数据两种
给点初步建议:
◎ 不暴露数据库,否则:人家统计你等待、人家锁表你死机、人家改数你纠错;
◎ 约松耦合越好,能批处理就不要实时处理,能用数据交换就不用接口调用,能用异步接口就不用同步接口;
◎ 是不是WebService随意,不过现在有不少轻量级方案,主要还是看安全性和性能要求了。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)