永发信息网

有些网站内容复制不了怎么办?

答案:4  悬赏:0  手机版
解决时间 2021-05-01 06:24
http://www.99inf.net/SoftwareDev/VB/9459.htm
最佳答案
有位网友发贴问我一个聊天的问题,不好拒绝;也看到最近VB.net版问这个的人很多,就写了一个放在这里。
正在装载数据……
本例只适合初学用Socket编程的人,因而力求简单,让老鸟见笑了J
另本示例用的是vs2005
1、服务端
Imports System.Net
Imports System.Net.Sockets
Imports System.Threading
Imports System.Text
Imports System.Diagnostics

Public Class frmServer

'服务端的Socket
Dim listener As Socket
'与客户端会话的Socket
Dim mySocket As Socket
'服务端的运行状态
Dim IsRun As Boolean = False
'监听接收数据线程
Dim myThread As Thread

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
myThread = New Thread(AddressOf Listen)
myThread.Start()
End Sub

Public Sub Listen()
Dim bytes() As Byte = New [Byte](1024) {}
Dim data As String = String.Empty
Dim tokens() As String

Dim localEndPoint As New
IPEndPoint(Net.IPAddress.Parse("127.0.0.1"), 11000)
'初始化socket
listener = New Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp)
'绑定端口
listener.Bind(localEndPoint)
'开始监听
listener.Listen(10)

BeginInvoke(New EventHandler(AddressOf AddInfo),
"服务端已启动,正在等待连接......")

mySocket = listener.Accept()
IsRun = True

While True
Dim bytesRec As Integer = mySocket.Receive(bytes)
data = Encoding.UTF8.GetString(bytes, 0, bytesRec)

tokens = data.Trim.Split("|")
Select Case tokens(0) '分析接收到的数据,可自己定义更多一些
Case "Chat"
BeginInvoke(New EventHandler(AddressOf AddInfo),
tokens(1)) 'Invoke保证线程安全
Case "Exit"
IsRun = False
BeginInvoke(New EventHandler(AddressOf AddInfo),
tokens(1)) 'Invoke保证线程安全
mySocket.Shutdown(SocketShutdown.Both)
mySocket.Close()
Exit Sub
End Select
End While

End Sub

'发送信息
Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnSend.Click
Dim msg As Byte() = Encoding.UTF8.GetBytes("Chat|" +
TextBox1.Text)
mySocket.Send(msg)
ListBox1.Items.Add("本地信息: " + TextBox1.Text)

TextBox1.Text = ""
End Sub

'非UI线程调用窗体控件,保证线程安全。与聊天实现无关。
Sub AddInfo(ByVal sender As System.Object, ByVal e As
System.EventArgs)
ListBox1.Items.Add("远程信息: " + sender.ToString)
End Sub

'关闭窗口时发关退出信息并清理资源
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As
System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
If IsRun Then
Dim msg As Byte() = Encoding.UTF8.GetBytes("Exit|服务端退出: " +
Me.Handle.ToString)
Dim bytesSent As Integer = mySocket.Send(msg)
End If
listener.Close()
mySocket = Nothing
myThread.Abort()
End Sub

End Class

2、客户端
Imports System.Net
Imports System.Net.Sockets
Imports System.Text
Imports System.Threading

Public Class frmClient
'客户端Socket
Dim cliSocket As Socket
'连接状态
Dim IsConnected As Boolean = False
'接收信息线程
Dim myThread As Thread

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim remoteEP As New IPEndPoint(Net.IPAddress.Parse("127.0.0.1"),
11000)
cliSocket = New Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp)
Try
cliSocket.Connect(remoteEP)
ListBox1.Items.Add("连接服务端成功!")
IsConnected = True

myThread = New Thread(AddressOf ReciveMsg)
myThread.Start()
Catch ex As Exception
ListBox1.Items.Add(ex.Message)
End Try
End Sub

Sub ReciveMsg()
Dim bytes() As Byte = New Byte(1024) {}
Dim data As String = String.Empty
Dim tokens() As String

While True
Dim bytesRec As Integer = cliSocket.Receive(bytes)
data = Encoding.UTF8.GetString(bytes, 0, bytesRec)

tokens = data.Trim.Split("|")
Select Case tokens(0) '分析接收到的数据,可自己定义更多一些
Case "Exit"
IsConnected = False
BeginInvoke(New EventHandler(AddressOf AddInfo),
tokens(1)) 'Invoke保证线程安全
cliSocket.Shutdown(SocketShutdown.Both)
cliSocket.Close()
Exit Sub
Case "Chat"
BeginInvoke(New EventHandler(AddressOf AddInfo),
tokens(1)) 'Invoke保证线程安全
End Select

End While

End Sub

'发送信息
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim msg As Byte() = Encoding.UTF8.GetBytes("Chat|" +
TextBox1.Text)
Dim bytesSent As Integer = cliSocket.Send(msg)

ListBox1.Items.Add("本地信息: " + TextBox1.Text)
TextBox1.Text = ""
End Sub

'非UI线程调用窗体控件,保证线程安全。与聊天实现无关。
Sub AddInfo(ByVal sender As System.Object, ByVal e As
System.EventArgs)
ListBox1.Items.Add("远程信息: " + sender.ToString)
End Sub

'关闭窗口时发关退出信息并清理资源
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As
System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
If IsConnected Then
Dim msg As Byte() = Encoding.UTF8.GetBytes("Exit|客户端退出: " +
Me.Handle.ToString)
Dim bytesSent As Integer = cliSocket.Send(msg)
myThread.Abort()
End If

End Sub
End Class
全部回答

另存下来,找个网页编辑软件就可了

你用屏幕剪切软件。切下来。或用腾讯TT捕捉屏幕一段一段切下来
将页面另存为,然后用别的程序比如word,打开在这里面复制
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
新高中生军训穿什么球鞋?
Master of Music和Master of Music Arts 有什
黑龙江省 肇州光荣院 详细地址
N86现在水行多少钱?会再降价吗?
劲松山庄在什么地方啊,我要过去处理事情
圣职者转什么职业最好
暗香玫瑰饱和精华有谁用过 真的有效果吗
大海有关唯美的句子,描写古古代树林的语句,
有好的幼儿教师的备课吗?希望能够让我借鉴一
公司的标志怎么画啊?用LD的标志谁有吗?
我得到我们厂的车间主任的赏识他让我当小组长
手机QQ怎么更改
笔记本电脑一般正常使用是几个小时
魔兽剑圣异界纵横 全文下载TXT地址 不要漏字
汉成快捷宾馆停车场地址有知道的么?有点事想
推荐资讯
右侧膝关节外侧半月板体部及后角损伤三级,关
重庆与成都哪里好?(全面比较)
WINDOW工具栏中对话框显示在右面?
想买个手机,大家推荐几款。
大唐无双,采集资源分布,比如说采集1草去哪
电脑时快时慢.我怀疑有人装控制软件.怎么办
600066宇通客车,敬请高手指点下周走势!
怎样陪养孩子的兴趣,对将来有用.
为什么电脑总是掉线啊;知道的告一声,谢谢!
龙阴皮我想知道这个在什么地方
在长沙“人参米”学爵士舞多少钱够?没基础可
QQLIVE怎么能点开但是放不了啊 也不卡
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?