首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VB >

关于winsock有关问题

2012-02-27 
关于winsock问题。第一个问题:在客户端接收完信息后,要不要winsock1.close。还是等关闭form的时候,在运行win

关于winsock问题。
第一个问题:在客户端接收完信息后,要不要winsock1.close。还是等关闭form的时候,在运行winsock.close。
第二个问题:我想在服务器那里显示连接的客户端。代码应当怎么写呢。能不能给出一个例子。
第三个问题:当服务器还在给客户端发信息的时候,客户端死机、断网了。这种情况服务器应当怎么处理呢。

[解决办法]
第一个问题:在客户端接收完信息后,要不要winsock1.close。还是等关闭form的时候,在运行winsock.close。
·客户端接收完信息后,要winsock1.close
第二个问题:我想在服务器那里显示连接的客户端。代码应当怎么写呢。能不能给出一个例子。
·服务端使用WinSock控件数组

VB code
VERSION 5.00 
Object = "{248DD890-BB45-11CF-9ABC-0080C7E7B78D}#1.0#0"; "MSWINSCK.OCX"
Begin VB.Form MainForm
BorderStyle  =  1  'Fixed Single
Caption    =  "Server"
ClientHeight  =  7545
ClientLeft    =  45
ClientTop    =  330
ClientWidth  =  8265
LinkTopic    =  "Form1"
MaxButton    =  0  'False
ScaleHeight  =  7545
ScaleWidth    =  8265
StartUpPosition =  3  '窗口缺省
Begin VB.ListBox logList
  Height      =  7080
  Left      =  120
  TabIndex    =  0
  Top      =  240
  Width      =  7935
End
Begin MSWinsockLib.Winsock tcpServer
  Index      =  0
  Left      =  3915
  Top      =  2445
  _ExtentX    =  741
  _ExtentY    =  741
  _Version    =  393216
End
End
Attribute VB_Name = "MainForm"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
Dim MstrS(0 To 244) As String
Dim SlotS(0 To 10) As Integer
Dim SrchS As Integer
Dim GateS(0 To 10) As String
Dim LinkB As Integer
Dim LinkS As Integer
Dim strSR(0 To 10) As String
Private Sub Form_Load()
Dim i As Integer
  ChDrive App.Path
  ChDir App.Path
  For i = 1 To 10
    SlotS(i) = 0
    Load tcpServer(i)
    strSR(i) = ""
  Next
  SrchS = 1
  tcpServer(0).LocalPort = 1010
  tcpServer(0).Listen
  logtofile "start listen at port 1010,max 10 connection at same time."
End Sub
Private Sub Form_Unload(Cancel As Integer)
Dim i As Integer
  tcpServer(0).Close
  For i = 1 To 10
    If SlotS(i) = 1 Then
      tcpServer(i).Close
    End If
  Next
  logtofile "end."
End Sub
Private Sub tcpServer_ConnectionRequest(Index As Integer, ByVal requestID As Long)
Dim i As Integer
Dim IP As String
Dim Gate As String
On Error GoTo SCRerr
  If Index = 0 Then
    IP = tcpServer(Index).RemoteHostIP
    If IP <> "127.0.0.1" Then
      logtofile IP + String(15 - Len(IP), " ") + "[" & Date$ & " " & Time$ & "] refused!"
      Exit Sub
    End If
    For i = SrchS To 10
      If SlotS(i) = 0 Then Exit For
    Next
    If i <= 10 Then
      GoTo ACCEPT
    ElseIf SrchS > 1 Then
      For i = 1 To SrchS - 1
        If SlotS(i) = 0 Then GoTo ACCEPT


      Next
    End If
    logtofile "ERROR! tcpServer SlotS full"
    For i = 1 To 10
      tcpServer_Close (i)
    Next
    SrchS = 1
  End If
  Exit Sub
ACCEPT:
  SlotS(i) = 1
  SrchS = (i Mod 10) + 1
  tcpServer(i).LocalPort = 1010
  tcpServer(i).ACCEPT requestID
  logtofile CStr(i) + " " + tcpServer(i).RemoteHostIP + String(15 - Len(tcpServer(i).RemoteHostIP), " ") + " Connected"
  Exit Sub
SCRerr:
  logtofile "tcpServer_ConnectionRequest 出现 #" & str(Err.Number) & " 意外错误: " & Err.Description
  Exit Sub
End Sub

Private Sub tcpServer_DataArrival(Index As Integer, ByVal bytesTotal As Long)
Dim strData As String
Dim strData1 As String
Dim ii As Integer
Dim n As Integer
Dim L As Integer
On Error GoTo SDerr
  'Link = Index
  tcpServer(Index).GetData strData
  strData1 = strSR(Index) + strData
  strSR(Index) = ""
  n = CntStrC(strData1, Chr(26))
  If n >= 1 Then
    For ii = 1 To n
      L = InStr(strData1, Chr(26))
      strData = Left(strData1, L - 1)
      strData1 = Mid(strData1, L + 1)
      MsgProcess Index, strData
    Next
  End If
  strSR(Index) = strData1
  Exit Sub
SDerr:
  logtofile "tcpServer_DataArrival 出现 #" & str(Err.Number) & " 意外错误: " & Err.Description
  Exit Sub
End Sub
Private Sub MsgProcess(Index As Integer, strData As String)
  logtofile CStr(Index) + "=>[" + strData + "]"
  MsgSend Index, strData
End Sub

Private Sub tcpServer_Close(Index As Integer)
On Error Resume Next
  tcpServer(Index).Close
  If SlotS(Index) = 1 Then
    SlotS(Index) = 0
    logtofile CStr(Index) + " " + tcpServer(Index).RemoteHostIP + String(15 - Len(tcpServer(Index).RemoteHostIP), " ") + " Disconnected"
  End If
End Sub
Function CntStrC(str As String, c As String) As Integer
Dim i As Integer
Dim n As Integer
On Error Resume Next
  n = 0
  For i = 1 To Len(str)
    If Mid(str, i, 1) = c Then n = n + 1
  Next
  CntStrC = n
End Function
Private Sub logtofile(s As String)
Dim f As Integer
On Error Resume Next
  log s
  f = FreeFile()
  Open "Server.LOG" For Append As #f
  Print #f, Format(Now,"YYYY-MM-DD hh:mm:ss") + " " + s
  Close #f
End Sub
Private Sub MsgSend(i As Integer, s As String)
On Error GoTo MSerr
  tcpServer(i).SendData s + Chr(26)
logtofile CStr(i) + " <=[" + s + "]"
  Exit Sub
MSerr:
  On Error Resume Next
  If Err.Number = 40006 Then
logtofile CStr(i) + " <=[" + s + "] after disconnected!"
    SlotS(i) = 0
    Exit Sub
  Else
    logtofile CStr(i) + " " + "出现 #" & str(Err.Number) & " 意外错误: " & Err.Description
    tcpServer_Close (i)
    Exit Sub
  End If
End Sub
Private Sub log(s As String)
  Debug.Print s
  If logList.ListCount >= 38 Then logList.Clear
  logList.AddItem s
End Sub



第三个问题:当服务器还在给客户端发信息的时候,客户端死机、断网了。这种情况服务器应当怎么处理呢。
·通常情况下客户端死机或断网时,服务器端在发送数据时会发生错误或_Close事件,但也可能无任何发现,此时应用层就需要通过收发自己事先约定好的握手消息是否正常来判断链路是否可用了。

热点排行