各位大侠,我在一个网络数据传输C/S中用了两个Winsock来分别传输数据信息和文件,我设置了不同的端口,如果1000和1001.程序有时能正常运行,有时又不能,如接收不了文件。我重新改一下端口,前几次测试又可以了,但后面又会出现问题。我反复测试了很多次,都是不稳定的问题。请问有解决方法吗?winsock端口设置有什么学问呢?
小弟虚心请教,还望各位高手指导,十分感谢。

解决方案 »

  1.   

    端口应该没问题,估计是你的程序有问题,最好先看看别人的服务程序是如何做的再尝试自己写。
    虽然 VB 做这种网络程序不是很好,但普通测试的用还是可以的,你应该多了解一些架构上的思路,
    比如控件数组的方法做服务等,玩到高级点了,可以看看API的实际运用过程以及架构,如多线程、
    线程池管理等,不过这些可能要用到别的开发工具,VB做起来不太好做,因为VB的资源不太好管理。
    但最起码控件数组和握手协议以及Sock基本的使用理念应该搞清楚先。
      

  2.   

    好的,请多帮助啦。
    下面是服务器的代码。
    '''''''''''''''''''''''''''''''''''''''''''''''''''''
    private Sub Form_Load()
      SerListenInfo.LocalPort = 5007                         '将 LocalPort 属性设置为一个整数。
      SerListenInfo.Listen                                    '调用 Listen 方法,使其进入监听状态
      SerListenFile.LocalPort = 5008
      SerListenFile.Listen
    Form1.Hide
    Set def_path_folder = def_path.BrowseForFolder(0, "选择工作路径", 0, ssfDESKTOP)
    End SubPrivate Sub SerInfo1_Connect()
    MsgBox "客户端连接成功", vbOKOnly
    End SubPrivate Sub SerInfo1_DataArrival(ByVal bytesTotal As Long)
     On Error Resume Next
        
        Dim strInput As String         '定义所用到的变量
        Dim strData() As String
        Dim strParse() As String
        Dim strFlag As String
        Dim strText As String                   '模块数据
        Dim strOutput As String
        Dim strReturn() As String
        Dim i As Integer
        Dim GN As Integer    SerInfo1.GetData strInput, , bytesTotal
        'MsgBox "数据接收成功", vbOKOnly
        strData = Split(strInput, "\")
        For i = 0 To UBound(strData)
            strText = ""
            strText = strData(i)
            If strText <> "" Then
            strParse = Split(strText, ",")       '拆分新接收到的模块数据到数组中
            strFlag = strParse(0)
            GN = i                   '功能标号
         ' MsgBox strFlag, vbOKOnly
        Select Case (strFlag)               '根据标志位来判断所要执行的操作
                    Case "laodongli_data"
                        strReturn = LaoDongLi(strParse)
                        If strParse(3) = True Then
                        Call FileTransfer(def_path_folder.Items.Item.Path & "\劳动力配备结果.txt")
                        End If
                    Case "jiaoshoujia_data"
                    'MsgBox "succeed", vbOKOnly
                        strReturn = JiaoShouJia(strParse)
                        
                        'MsgBox strReturn(3), vbOKOnly
                        If Val(strParse(4)) = 1 Then
                        Call FileTransfer(def_path_folder.Items.Item.Path & "\脚手架设计结果.txt")
                        End If
                    End Select
               End If
         Next i
        For i = 0 To UBound(strData)
        strData(i) = ""
        Next i
      strData(GN) = Join(strReturn, ",")
      strOutput = Join(strData, "\")
    SerInfo1.SendData strOutput
    End SubPrivate Sub SerInfo1_Error(ByVal Number As Integer, Description As String, ByVal Scode As Long, ByVal Source As String, ByVal HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean)
    SerInfo1.Close
    End SubPrivate Sub SerListenFile_ConnectionRequest(ByVal requestID As Long)
    SerFile1.Accept requestID
    End Sub
    Private Sub SerListenInfo_ConnectionRequest(ByVal requestID As Long)SerInfo1.Accept requestID
    End Sub ''''''''''文件传输函数
     Public Function FileTransfer(Filename As String)                         '核心模块,用以传输
        Dim strAdd As String
        Dim FreeF As Integer
        Dim LenFile As Long                       '文件的长度
        Dim bytData() As Byte                     '存放数据的数组
        Dim ipos As Long
        '首先发送文件标志
       ' strAdd = Filename
        
        '发送文件
        Const imax = 65535
        FreeF = FreeFile                          '获得空闲的文件号
        Open Filename For Binary As #FreeF        '打开文件
        DoEvents
        LenFile = LOF(FreeF)                       '获得文件长度
    If LenFile <= imax Then
    ReDim bytData(0 To LenFile)              '根据文件长度重新定义数组大小
    Get #FreeF, , bytData                      '把文件读入到数组里
        Close #FreeF                               '关闭文件
        SerFile1.SendData bytData                   '发送数据
        Exit Function
        End If
    Do Until (ipos >= (LenFile - imax))       '发送整块数据的循环
        ReDim bytData(1 To imax)
        Get #FreeF, ipos + 1, bytData
        SerFile1.SendData bytData
    ipos = ipos + imax                  '移动iPos,使它指向下来要读的数据
        Loop
    ReDim bytData(1 To LenFile - ipos)    '发送剩下的不够一个数据块的数据
        Get #FreeF, ipos + 1, bytData
        SerFile1.SendData bytData
        Close #FreeF
        MsgBox "发送成功", vbOKOnly
    End Function''''''''''''''''''''''''''''''''''''''''''''''
      

  3.   


    下面是客户端的代码
    ''''''''''''''''''''''''''''''''''''Private str1 As StringPrivate Sub Command1_Click()Hide MeForm_GongNeng.ShowEnd SubPrivate Sub Command2_Click()'Call SkinRemove
    EndEnd SubPrivate Sub ClientFile_Connect()
    MsgBox "文件传输控件连接成功", vbOKOnly
    End SubPrivate Sub ClientFile_DataArrival(ByVal bytesTotal As Long)
        On Error Resume Next
        
        Dim data As String
        ClientFile.GetData data, , bytesTotal          '取得数据
        
        MsgBox "客户端收到文件数据", vbOKOnly
     
        
        Dim intFile As Integer
        intFile = FreeFile                '产生一个文件号
        MkDir def_path_folder.Items.Item.Path & "\分析结果文件"            '在当前路径下建立一个文件夹
            Kill def_path_folder.Items.Item.Path & "\分析结果文件\" & "劳动力配备结果.txt"
           Open def_path_folder.Items.Item.Path & "\分析结果文件\" & "劳动力配备结果.txt" For Binary As #intFile
        'Kill def_path_folder.Items.Item.Path & "\分析结果文件\" & "脚手架设计结果.txt"
          ' Open def_path_folder.Items.Item.Path & "\分析结果文件\" & "脚手架设计结果.txt" For Binary As #intFile
            
        Put #intFile, LOF(intFile) + 1, data      '写入数据
        Close #intFile               '关闭文件
        
    End SubPrivate Sub Form_Load()Skin1.ApplySkin Me.hWndImage1.Height = Screen.Height * 0.75
    Image1.Width = Screen.Width * 0.45Image1.Left = Screen.Width / 3.5
    Image1.Top = Screen.Height / 10ClientInfo.RemoteHost = ClientInfo.LocalHostName                     '设置远程用户
    ClientInfo.RemotePort = 5007                                    '指定远程端口号
    ClientFile.RemoteHost = ClientFile.LocalHostName                     '设置远程用户
    ClientFile.RemotePort = 5008                                  '指定远程端口号  str1 = "日一二三四五六"
      StatusBar1.Panels.Item(1).Text = "星期" & Mid(str1, Weekday(Date), 1)
      StatusBar1.Panels.Item(2).Text = DateEnd SubPrivate Sub mnuabout_Click()
    Form_about.ShowEnd SubPrivate Sub mnuanimate_Click()
    form_help_animation.Show
    End SubPrivate Sub mnudataanalize_Click()
    Form_dataanalize.ShowForm_dataanalize.Left = Screen.Width / 4
    Form_dataanalize.Top = Screen.Height / 10End SubPrivate Sub mnuexit_Click()
    End
    End Sub
    Private Sub M_analize_Click()
    Me.Hide
      Form_ShuJuFenXi.Show
    End Sub
    Private Sub M_animation_Click()
      Form_DongHua.Show
    End SubPrivate Sub M_time_Click()
    Me.Hide
      Form_GongShi.Show
    End SubPrivate Sub M_scaffold_Click()
    Me.Hide
      Form_JiaoShou.ShowEnd SubPrivate Sub M_labor_Click()
    Me.Hide
    'Form_LaoDongLi.show 1
      Form_LaoDongLi.Show
    End Sub
    Private Sub mnufold_Click()
    Form_help_jiaoshou.Show 1
    End SubPrivate Sub mnulabor_Click()
    form_help_labor.Show 1
    End SubPrivate Sub mnupath_Click()Set def_path_folder = def_path.BrowseForFolder(0, "选择工作路径", 0, ssfDESKTOP)
       MsgBox def_path_folder.Items.Item.Path, vbOKOnlyEnd SubPrivate Sub mnutime_Click()
    form_help_gongshi.Show 1
    End SubPrivate Sub Toolbar1_ButtonClick(ByVal Button As ComctlLib.Button)On Error GoTo ErrorHandler         '''''错误处理If def_path_folder.Title <> "" Then
      Select Case Button.Key
      
        Case "m_analize"
          Call M_analize_Click      
        Case "m_animation"
          Call M_animation_Click
          
        Case "m_time"
          Call M_time_Click
          
        Case "m_scaffold"
          Call M_scaffold_Click
          
        Case "m_labor"
          Call M_labor_Click
          
        Case "m_exit"
          End
     
        
        
        End Select
     End If
     Exit Sub
        
    ErrorHandler:''点击退出的特殊情况If Button.Key = "m_exit" Then
    End
    End Ifresponse = MsgBox("请在首页“帮助”栏选择工作路径", vbOKOnly, "提示")If response = 1 Then
    End If     ' Resume Next
    End Sub
    Private Sub cmdConnect_Click()
     ClientInfo.Connect                                               '连接
     ClientFile.Connect
    End Sub
    Private Sub ClientInfo_Connect()
    MsgBox "服务器连接成功", vbOKOnly
    End Sub
    Private Sub ClientInfo_DataArrival(ByVal bytesTotal As Long)
    Dim strInput As String
    Dim strData() As String
    Dim strtext As String
    Dim strparse() As String
    Dim strFlag As String ClientInfo.GetData strInput                                          '接收信息 strData = Split(strInput, "\")
       
      For i = 0 To UBound(strData)
            strtext = ""
            strtext = strData(i)
            If strtext <> "" Then
            strparse = Split(strtext, ",")       '拆分新接收到的模块数据到数组中
            strFlag = strparse(0)
            'MsgBox strFlag, vbOKOnly
      Select Case (strFlag)                '根据标志位来判断所要执行的操作
              Case "laodongli_data"
                 Form_LaoDongLi.SkinLabel2.Caption = strparse(1)
    End Select
    End If
    NextEnd Sub