注册 登录
编程论坛 VB6论坛

socket通信

admin_xyz 发布于 2013-01-05 14:23, 452 次点击
程序代码:

Private Sub Form_Load()

Winsock1.LocalPort = 4858   '服务端 端口为 4858


Winsock1.Listen

local_port.Caption = Winsock1.LocalPort

gettext.Enabled = False
sendtext.Enabled = False

End Sub



Private Sub sendtext_KeyPress(KeyAscii As Integer)

    If KeyAscii = 13 Then
        Winsock1.SendData sendtext.Text
        sendtext.Text = ""
   
   
    End If
   
   
End Sub

Private Sub Timer1_Timer()

    winsock_state.Caption = Winsock1.State

End Sub

Private Sub Winsock1_Close()

    Winsock1.Close
   
    MsgBox "No Client On Line !", vbInformation, "xyz"

End Sub

Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long)

    MsgBox "Connect Success !", vbInformation, "xyz"
   
    local_ip.Caption = Winsock1.LocalIP
    client_ip.Caption = Winsock1.RemoteHostIP
    Timer1.Enabled = True
   
    gettext.Enabled = True
    sendtext.Enabled = True
   
   If Winsock1.State <> sckClosed Then Winsock1.Close

    Winsock1.Accept requestID
   
   
End Sub

Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)

    Dim tmpstr As String
   
    date_time = Date & "  " & Time()
   
    Winsock1.GetData tmpstr
   
    gettext.Text = gettext.Text & date_time & vbCrLf & tmpstr & vbCrLf

End Sub        '这是server
  

程序代码:
Private Sub Command1_Click()


Winsock1.RemoteHost = Text1.Text

Winsock1.Connect Winsock1.RemoteHost, Winsock1.RemotePort



local_ip.Caption = Winsock1.LocalIP
remote_port.Caption = Winsock1.RemotePort


sendtext.Enabled = True
gettext.Enabled = True

Command1.Enabled = False

End Sub



Private Sub Form_Load()

    gettext.Enabled = False
    sendtext.Enabled = False
   
    Winsock1.RemotePort = 4858

End Sub


Private Sub sendtext_KeyPress(KeyAscii As Integer)

    If KeyAscii = 13 Then
        Winsock1.SendData sendtext.Text
        sendtext.Text = ""
    End If



End Sub

Private Sub Winsock1_Close()
   
    Winsock1.Close
    MsgBox "Server Off Line !", vbInformation, "xyz"
   
End Sub

Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
   
    Dim tmpstr As String
   
    date_time = Date & "  " & Time()
   
    Winsock1.GetData tmpstr
   
    gettext.Text = gettext.Text & date_time & vbCrLf & tmpstr & vbCrLf


 End Sub  '这是client



把客户端发给别人,可是还不是能聊天..代码应该没问题呀,怎么解决下.刚接触winsock。。高手别喷
2 回复
#2
lowxiong2013-01-05 20:20
代码好长,不愿细看。
TCP连接一般服务器端要使用控件数组,Winsock1(0)始终负责Listen,一旦侦听到连接后动态增加一个Winsock1(i)负责连接,并由这个连接进行通讯,这样即可一对多通讯,如果只是聊天,建议使用udp连接,简单方便。在帖子https://bbs.bccn.net/viewthread.php?tid=382750&page=1#pid2164527里,六楼我曾经给别人回复过,你可以参考下,简单局域网文本聊天是没问题的。
#3
admin_xyz2013-01-07 20:55
回复 2楼 lowxiong
求你了帮我看下,我都研究好几天了。。
1