用winsock控件编TCP程序
做一“列车时刻表”不能显示
目的:用自己的机子做主机,并且想自己访问!代码如下,请大家指点错误!  谢谢!服务端:Option Explicit
Dim nSocket As Integer
Dim Schedule(9) As StringPrivate Sub Form_Load()InitSchedule
nSocket = 0
srSocket(0).LocalPort = 1024
srSocket(0).Listen
StatusBar1.SimpleText = "服务器已经工作,准备接收请求。"End SubPrivate Sub srSocket_Close(Index As Integer)
StatusBar1.SimpleText = "客户终止对话!"
srSocket(Index).Close
End SubPrivate Sub srSocket_ConnectionRequest(Index As Integer, ByVal requestID As Long)nSocket = nSocket + 1
Load srSocket(nSocket)
srSocket(nSocket).Accept requestID
StatusBar1.SimpleText = "有客户请求,建立连接。"
End SubPrivate Sub srSocket_DataArrival(Index As Integer, ByVal bytesTotal As Long)Dim Rec
Dim NextOne As String
StatusBar1.SimpleText = "进行数据传输"
srSocket(Index).GetData Rec
NextOne = getNext()
srSocket(Index).SendData NextOne
If srSocket(Index).State <> 0 Then
srSocket(Index).Close
End If
End SubPrivate Sub srSocket_Error(Index As Integer, 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)
MsgBox "错误[" & Number & "]" & Description
End SubPublic Sub InitSchedule()Schedule(0) = "13次    北京--上海         开车时间       14:40"
Schedule(1) = "165次   北京--牡丹江       开车时间        0:18"
Schedule(2) = "237次   北京--沈阳       开车时间       0:42"
Schedule(3) = "11次   北京--沈阳北       开车时间       6:35"
Schedule(4) = "755次   北京--燕郊        开车时间       6:54"
Schedule(5) = "137次   北京--哈尔滨东       开车时间       8:14"
Schedule(6) = "311次   北京--天津        开车时间       9:13"
Schedule(7) = "167次   北京--齐齐哈尔       开车时间       9:35"
Schedule(8) = "319次   北京--唐山        开车时间       10:50"
Schedule(9) = "81次   北京--大连       开车时间       16:30"
End SubPublic Function getNext()Static Counter As Integer
getNext = Schedule(Counter)
Counter = Counter + 1
Counter = Counter Mod 10End Function
客户端:Option ExplicitPrivate Sub clSocket_Close()
clSocket.Close
End SubPrivate Sub clSocket_Connect()
clSocket.SendData "Inform Me"
End SubPrivate Sub clSocket_DataArrival(ByVal bytesTotal As Long)
Dim gResult As String
clSocket.GetData gResult, vbString
DisplayAndRequestNext gResult
End SubPrivate Sub Form_Load()
clSocket.RemoteHost = "127.0.0.1"
clSocket.RemotePort = 1024
clSocket.Connect
End SubPublic Sub DisplayAndRequestNext(ByVal iStr As String)
Dim i As Integer
Dim sLen As Integer
Dim sTmp1 As String
Dim stmp2 As String
sLen = Len(iStr)
sTmp1 = Space(sLen)
stmp2 = Space(sLen)
sTmp1 = sTmp1 & iStr & stmp2
For i = 1 To 2 * sLen + 1
Label2.Caption = Mid$(sTmp1, i, sLen)
Delay 2000
Next
If clSocket.State <> 0 Then
clSocket.SendData "Next"
Else
Label2.Caption = "服务器已经关闭!"
End If
End SubPublic Function Delay(dt As Integer)
Dim i As Integer
For i = 0 To dtDoEvents
Next
End Function

解决方案 »

  1.   

    帮帮了!Help!1Thank  you !
      

  2.   

    Option ExplicitDim nSocket As Integer
    Dim Schedule(9) As StringPrivate Sub Form_Load()InitSchedule
    nSocket = 0
    srsocket(0).LocalPort = 1024
    srsocket(0).Bind srsocket(0).LocalPort
    srsocket(0).Listen
    StatusBar1.SimpleText = "服务器已经工作,准备接收请求。"
    'MsgBox "服务器已经工作,准备接收请求。"
    End SubPrivate Sub srSocket_Close(Index As Integer)
    StatusBar1.SimpleText = "客户终止对话!"
    srsocket(Index).Close
    'MsgBox "客户终止对话!"
    End SubPrivate Sub srSocket_ConnectionRequest(Index As Integer, ByVal requestID As Long)nSocket = nSocket + 1
    Load srsocket(nSocket)
    srsocket(nSocket).Accept requestID
    StatusBar1.SimpleText = "有客户请求,建立连接。"
    'MsgBox "有客户请求,建立连接。"
    End SubPrivate Sub srSocket_DataArrival(Index As Integer, ByVal bytesTotal As Long)Dim Rec
    Dim NextOne As String
    StatusBar1.SimpleText = "进行数据传输"
    srsocket(Index).GetData Rec
    NextOne = getNext()
    srsocket(Index).SendData NextOne
    'If srsocket(Index).State <> 0 Then
    'srsocket(Index).Close
    'End If
    End SubPrivate Sub srSocket_Error(Index As Integer, 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)
    MsgBox "错误[" & Number & "]" & Description
    End SubPublic Sub InitSchedule()Schedule(0) = "13次    北京--上海         开车时间       14:40"
    Schedule(1) = "165次   北京--牡丹江       开车时间        0:18"
    Schedule(2) = "237次   北京--沈阳       开车时间       0:42"
    Schedule(3) = "11次   北京--沈阳北       开车时间       6:35"
    Schedule(4) = "755次   北京--燕郊        开车时间       6:54"
    Schedule(5) = "137次   北京--哈尔滨东       开车时间       8:14"
    Schedule(6) = "311次   北京--天津        开车时间       9:13"
    Schedule(7) = "167次   北京--齐齐哈尔       开车时间       9:35"
    Schedule(8) = "319次   北京--唐山        开车时间       10:50"
    Schedule(9) = "81次   北京--大连       开车时间       16:30"
    End SubPublic Function getNext()Static Counter As Integer
    getNext = Schedule(Counter)
    Counter = Counter + 1
    Counter = Counter Mod 10End Function-----------------------------------------------Option Explicit
    Private Sub clSocket_Close(Index As Integer)
    clSocket(0).Close
    End SubPrivate Sub clSocket_Connect(Index As Integer)
    clSocket(0).SendData "Inform Me"
    End SubPrivate Sub clSocket_DataArrival(Index As Integer, ByVal bytesTotal As Long)
    Dim gResult As String
    clSocket(0).GetData gResult, vbString
    DisplayAndRequestNext gResult
    End SubPrivate Sub Form_Load()
    clSocket(0).RemoteHost = "127.0.0.1"
    clSocket(0).RemotePort = 1024
    clSocket(0).Connect clSocket(0).RemoteHost, clSocket(0).RemotePort
    End SubPublic Sub DisplayAndRequestNext(ByVal iStr As String)
    Dim i As Integer
    Dim sLen As Integer
    Dim sTmp1 As String
    Dim stmp2 As String
    sLen = Len(iStr)
    sTmp1 = Space(sLen)
    stmp2 = Space(sLen)
    sTmp1 = sTmp1 & iStr & stmp2
    For i = 1 To 2 * sLen + 1
    Label2.Caption = Mid$(sTmp1, i, sLen)
    Delay 2000
    Next
    If clSocket(0).State <> 0 Then
    clSocket(0).SendData "Next"
    Else
    Label2.Caption = "服务器已经关闭!"
    End If
    End SubPublic Function Delay(dt As Integer)
    Dim i As Integer
    For i = 0 To dtDoEvents
    Next
    End Function---------------------------------------具体的代码你自己去修改吧。反正你的server端srSocket_DataArrival把socket直接关闭了。
      

  3.   

    谢谢JennyVenus() 不过,好象还是不行