| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 452 人关注过本帖
标题:socket通信
只看楼主 加入收藏
admin_xyz
Rank: 2
等 级:论坛游民
帖 子:195
专家分:77
注 册:2012-6-24
结帖率:97.3%
收藏
已结贴  问题点数:20 回复次数:2 
socket通信
程序代码:
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。。高手别喷
搜索更多相关主题的帖子: 通信 
2013-01-05 14:23
lowxiong
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:27
帖 子:653
专家分:3402
注 册:2008-5-7
收藏
得分:20 
代码好长,不愿细看。
TCP连接一般服务器端要使用控件数组,Winsock1(0)始终负责Listen,一旦侦听到连接后动态增加一个Winsock1(i)负责连接,并由这个连接进行通讯,这样即可一对多通讯,如果只是聊天,建议使用udp连接,简单方便。在帖子https://bbs.bccn.net/viewthread.php?tid=382750&page=1#pid2164527里,六楼我曾经给别人回复过,你可以参考下,简单局域网文本聊天是没问题的。
2013-01-05 20:20
admin_xyz
Rank: 2
等 级:论坛游民
帖 子:195
专家分:77
注 册:2012-6-24
收藏
得分:0 
回复 2楼 lowxiong
求你了帮我看下,我都研究好几天了。。

总有人比你努力,可怕的是比你牛的人比你还努力。
2013-01-07 20:55
快速回复:socket通信
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019152 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved