由于web德无状态性,数据接收肯定是数据接收完后就段开了。

解决方案 »

  1.   

    我刚才在局网上做了一个测试在ASP.NET上用Socket向另一台机器的发送消息,该机器是用VB6编的应用程序,用VB的WinSock控件,能够接受到ASP发来的消息,但ASP一直处于等待状态,直到关闭VB程序,立即就得到了返回值,这是为什么呀,因为在VB中我让发送消息的命令在接受数据后立即执行的。可是为什么要等到关闭时才能收到。我还没有做过并发测试,不知道这种解决方案有什么风险没有?
      

  2.   

    你要定好Server Socket与ClientSocket之间通信的协议和怎要通信。例如你的方式是发送->接收返回的消息,那可使用Tcp协议的Socket啊。
    你担心接收数据时网络故障了,你可以做一些相应措施解决,例如:
    1 C:给你信息
    2 S:(收到)给你一个返回
     (Client端一段时间内收不到返回则转到第一步重发)
     (Client端收到返回)
    3 C:我已经收到返回了
    4 S:(收到)把该Socket关掉
        (收不到转到2)
    情况二
      

  3.   

    在.net里用向后台(Delphi的ServerSocket)的Socket发消息
    给你一些相关的代码作参考
    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)
      

  4.   

    zzhuz(大件):因为Socket的Server端是要把接受到的数据添到数据库里去,所以不能这样子反复发的,如果它已经写入数据库了,但是在返回时出错就不能重发,看事务能不能解决这个问题,并且不能关掉Socket,必须有一个线程每天24小时打开,因为随时都有接受数据的可能啊。
    还有就是为什么要等到VB程序关闭后才能得到返回信息?
      

  5.   

    第一问题,修改一下流程就可以了。Server端确保Client已经收到回应后才进数据库;或者表里增加一个字段,S、C两端都需要对这字段作操作那这记录才有效。第二个问题,你把你的Server端的Socket做成多线程就可以了,主线程不断的监听,收到一个连接就分配一条线程来处理它(SMTP、POP等服务器都是这样做的啊)
      

  6.   

    如果你用线程进对进行操作时,你就小心。
    因为WEB是没状态的,好可能WEB处理完,那个线程还在进行。
    出来的结果就会不正确。
      

  7.   

    我觉得用web service更好一点吧  处理完就挂
      

  8.   

    我就是这样做的,Asp.Net作为客户端通过Socket跟Server端通信。
    幸好Delphi里有ServerSocket这控件,支持多线程
      

  9.   

    在asp.net中可以实现邮件收发、FTP等socket过程。
    但是web是无状态的,它的生存期是这样的。
    C:请求页面,
    S:处理数据,发送页面.
    至此,C S 断开
    生存期很短,经常出现超时现象.
    web是无状态的,它不可能长时间运行,这一点是与windows程序最大的,或者说是最本质的差别。
    "还有就是我的联接状态应不应该保持,还是发送->接受返回数据->断开"
    当然,你的联接状态应该保持,把socket过程看成是一个方法的调用,用try catch 来判断其调用成败。
      

  10.   

    '微软的例子Imports System
    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