由于web德无状态性,数据接收肯定是数据接收完后就段开了。
解决方案 »
- 我这样写对不对?Jquery、
- asp.net 如何动态生成界面?
- Service 如何才能返回 DataTable和使用DataTable作为方法的参数,急...搞定就结贴
- 大数据量的处理问题
- //***********在.NET中,如何利用POP3获取邮箱中未读邮件数量**********************//
- 为什么我建用.net来创建一个Web服务器 而网页显示错误啊
- visual studio 2005 无法升级
- 关于.net 的datareader 的问题
- 动态创建表格出错!请指教!
- 在ASP.Net中如何实现“上一页”的功能?
- 搞不懂就问人!
- 新手告急:vb.net中sql语句的查询。
你担心接收数据时网络故障了,你可以做一些相应措施解决,例如:
1 C:给你信息
2 S:(收到)给你一个返回
(Client端一段时间内收不到返回则转到第一步重发)
(Client端收到返回)
3 C:我已经收到返回了
4 S:(收到)把该Socket关掉
(收不到转到2)
情况二
给你一些相关的代码作参考
Imports System.Net.Sockets
Imports System.Net//实例化一个连向服务器地址为123.123.123.123端口9999的Socket
Dim strServer As String
strServer = ConfigurationSettings.AppSettings("123.123.123.123")
Dim lipa As IPHostEntry = Dns.Resolve(strSMSServer)
Dim Ep As New IPEndPoint(lipa.AddressList(0), 9999)
Dim MySocket As Socket
MySocket = New Socket(Ep.AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)Dim bContent As Byte()
bContent = Encoding.ASCII.GetBytes("hello")
MySocket.Connect(Ep) //连接Socket
MySocket.Send(bContent, 0, bContent.Length, SocketFlags.None) //发送"hello"//接收
Dim length As Long
Dim RecvBytes As Byte()
length = MySocket.Receive(RecvBytes, 0, ASocket.Available, SocketFlags.None)
还有就是为什么要等到VB程序关闭后才能得到返回信息?
因为WEB是没状态的,好可能WEB处理完,那个线程还在进行。
出来的结果就会不正确。
幸好Delphi里有ServerSocket这控件,支持多线程
但是web是无状态的,它的生存期是这样的。
C:请求页面,
S:处理数据,发送页面.
至此,C S 断开
生存期很短,经常出现超时现象.
web是无状态的,它不可能长时间运行,这一点是与windows程序最大的,或者说是最本质的差别。
"还有就是我的联接状态应不应该保持,还是发送->接受返回数据->断开"
当然,你的联接状态应该保持,把socket过程看成是一个方法的调用,用try catch 来判断其调用成败。
Imports System.Net
Imports System.Net.Sockets
Imports System.Threading
Imports System.Text' State object for receiving data from remote device.
Public Class StateObject
Public workSocket As Socket = Nothing ' Client socket.
Public BufferSize As Integer = 256 ' Size of receive buffer.
Public buffer(256) As Byte ' Receive buffer.
Public sb As New StringBuilder() ' Received data string.
End Class 'StateObjectPublic Class AsynchronousClient
' The port number for the remote device.
Private Shared port As Integer = 11000
' ManualResetEvent instances signal completion.
Private Shared connectDone As New ManualResetEvent(False)
Private Shared sendDone As New ManualResetEvent(False)
Private Shared receiveDone As New ManualResetEvent(False)
' The response from the remote device.
Private Shared response As [String] = [String].Empty
Private Shared Sub StartClient()
' Connect to a remote device.
Try
' Establish the remote endpoint for the socket.
' The name of the
' remote device is "host.contoso.com".
Dim ipHostInfo As IPHostEntry = Dns.Resolve("host.contoso.com")
Dim ipAddress As IPAddress = ipHostInfo.AddressList(0)
Dim remoteEP As New IPEndPoint(ipAddress, port)
' Create a TCP/IP socket.
Dim client As New Socket(AddressFamily.InterNetwork, _
SocketType.Stream, ProtocolType.Tcp)
' Connect to the remote endpoint.
client.BeginConnect(remoteEP, AddressOf ConnectCallback, client)
connectDone.WaitOne()
' Send test data to the remote device.
Send(client, "This is a test<EOF>")
sendDone.WaitOne()
' Receive the response from the remote device.
Receive(client)
receiveDone.WaitOne()
' Write the response to the console.
Console.WriteLine("Response received : {0}", response)
' Release the socket.
client.Shutdown(SocketShutdown.Both)
client.Close()
Catch e As Exception
Console.WriteLine(e.ToString())
End Try
End Sub 'StartClient
Private Shared Sub ConnectCallback(ar As IAsyncResult)
Try
' Retrieve the socket from the state object.
Dim client As Socket = CType(ar.AsyncState, Socket)
' Complete the connection.
client.EndConnect(ar)
Console.WriteLine("Socket connected to {0}", _
client.RemoteEndPoint.ToString())
' Signal that the connection has been made.
connectDone.Set()
Catch e As Exception
Console.WriteLine(e.ToString())
End Try
End Sub 'ConnectCallback
Private Shared Sub Receive(client As Socket)
Try
' Create the state object.
Dim state As New StateObject()
state.workSocket = client
' Begin receiving the data from the remote device.
client.BeginReceive(state.buffer, 0, state.BufferSize, 0, _
AddressOf ReceiveCallback, state)
Catch e As Exception
Console.WriteLine(e.ToString())
End Try
End Sub 'Receive
Private Shared Sub ReceiveCallback(ar As IAsyncResult)
Try
' Retrieve the state object and client socket
' from the async state object.
Dim state As StateObject = CType(ar.AsyncState, StateObject)
Dim client As Socket = state.workSocket
' Read data from the remote device.
Dim bytesRead As Integer = client.EndReceive(ar)
If bytesRead > 0 Then
' There might be more data, so store the data received so far.
state.sb.Append(Encoding.ASCII.GetString(state.buffer, 0, _
bytesRead))
' Get the rest of the data.
client.BeginReceive(state.buffer, 0, state.BufferSize, 0, _
AddressOf ReceiveCallback, state)
Else
' All the data has arrived; put it in response.
If state.sb.Length > 1 Then
response = state.sb.ToString()
End If
' Signal that all bytes have been received.
receiveDone.Set()
End If
Catch e As Exception
Console.WriteLine(e.ToString())
End Try
End Sub 'ReceiveCallback
Private Shared Sub Send(client As Socket, data As [String])
' Convert the string data to byte data using ASCII encoding.
Dim byteData As Byte() = Encoding.ASCII.GetBytes(data)
' Begin sending the data to the remote device.
client.BeginSend(byteData, 0, byteData.Length, 0, _
AddressOf SendCallback, client)
End Sub 'Send
Private Shared Sub SendCallback(ar As IAsyncResult)
Try
' Retrieve the socket from the state object.
Dim client As Socket = CType(ar.AsyncState, Socket)
' Complete sending the data to the remote device.
Dim bytesSent As Integer = client.EndSend(ar)
Console.WriteLine("Sent {0} bytes to server.", bytesSent)
' Signal that all bytes have been sent.
sendDone.Set()
Catch e As Exception
Console.WriteLine(e.ToString())
End Try
End Sub 'SendCallback
'Entry point that delegates to C-style main Private Function.
Public Overloads Shared Sub Main()
System.Environment.ExitCode = _
Main(System.Environment.GetCommandLineArgs())
End Sub
Overloads Public Shared Function Main(args() As [String]) As Integer
StartClient()
Return 0
End Function 'Main
End Class 'AsynchronousClient