互联网聊天vb.net 互联网聊天的利与弊
用VB做一个实现互联网的连接聊天程序
这个很简单,一个服务端和一个客户端。
我们提供的服务有:网站建设、成都网站制作、微信公众号开发、网站优化、网站认证、酒泉ssl等。为1000多家企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的酒泉网站制作公司
我们先画一个服务端界面,两个textbox,一个command,一个winsock,非别为:Text1,text2,command1
text1用于填写你需要发送的信息
text2用于显示接收的信息
command1用于发送信息
winsock1用于创建连接
客户端和服务端的界面一样。
接下来我们开始写代码:
服务端:
Private Sub Command1_Click()
Dim a As String
Text2.Text = Text2.Text vbCrLf Text1.Text
a = Text1.Text
Winsock1.SendData a
End Sub
Private Sub Form_Load()
Winsock1.Close
Winsock1.LocalPort = 4981 '这里填写你要监听的端口
Winsock1.Listen '开始监听
End Sub
Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long)
Winsock1.Close
Winsock1.Accept requestID
End Sub
Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Dim a As String
Winsock1.GetData a
Text2.Text = Text2.Text vbCrLf a
End Sub
Private Sub Winsock1_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)
Winsock1.Close '如果出错则关闭连接
End Sub
客户端:
Private Sub Command1_Click()
Dim a As String
Text2.Text = Text2.Text vbCrLf Text1.Text
a = Text1.Text vbCrLf
Winsock1.SendData a
End Sub
Private Sub Form_Load()
Winsock1.Close
Winsock1.RemoteHost = "wenlvzhao.3322.org" '这里填写你的静态IP或者是动态域名
Winsock1.RemotePort = 4981 '这里填写连接的端口
Winsock1.Connect
End Sub
Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Dim a As String
Winsock1.GetData a
Text2.Text = Text2.Text vbCrLf a
End Sub
Private Sub Winsock1_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)
Winsock1.Close '如果出错则关闭连接
End Sub
vb.net2005 tcp聊天程序问题
‘客户端向这样写:在窗体说放两个文本框、两个标签、两个按钮、一个列表框
Imports System.Net
Imports System.Net.Sockets
Imports System.IO
Imports System.Threading
Public Class Form1
'网络基础数据流
Private ns As NetworkStream
Private swriter As StreamWriter
Private nsread As StreamReader
Private tcpclient As TcpClient
Private tcpconnected As Boolean = False
Private clisocket As Socket
Private mythread As Thread
'发送
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If Me.TextBox2.Text "" Then
swriter.WriteLine(Me.TextBox2.Text)
swriter.Flush()
Me.TextBox2.Text = ""
Else
MessageBox.Show("发送信息不能为空!", "错误提示")
End If
End Sub
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'建立连连接
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim ipremote As IPAddress
Dim tcpclient As TcpClient
Try
ipremote = IPAddress.Parse(Me.TextBox1.Text)
Catch ex As Exception
MessageBox.Show("IP地址不合法!", "错误提示")
End Try
Try
tcpclient = New TcpClient(Me.TextBox1.Text, 8000)
ns = tcpclient.GetStream
swriter = New StreamWriter(ns)
Me.StatusBar1.Text = "已经连接上"
Me.Button2.Enabled = False
Me.Button1.Enabled = True
tcpconnected = True
Catch ex As Exception
MessageBox.Show("无法与远程8000端口建立连接!", "错误提示")
End Try
End Sub
Private Sub listen()
Try
Dim tcplistener As New TcpListener(IPAddress.Parse("127.0.0.1"), 8000)
tcplistener.Start()
tcpclient = tcplistener.AcceptTcpClient
ns = tcpclient.GetStream()
nsread = New StreamReader(ns)
While True
Dim msg As String = nsread.ReadLine
If msg = "stop" Then
tcplistener.Stop()
ns.Close()
nsread.Close()
mythread.Abort()
Else
Dim mytime As String = DateTime.Now.ToShortTimeString
Me.ListBox1.Items.Add(mytime + " " + msg)
End If
End While
Catch ex As System.Security.SecurityException
MessageBox.Show("侦听失败!", "错误")
End Try
End Sub
End Class
用vb.net实现网络通信
用 vb.net socket通信
Dim th As Threading.Thread
2 Dim tcpl As System.Net.Sockets.TcpListener
3
4 Private Sub Form1_Load()Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
5 th = New System.Threading.Thread(New System.Threading.ThreadStart(AddressOf MyListen))
6 th.Start()
7 End Sub
8
9 Public Sub SendMessage()Sub SendMessage(ByVal IP As String, ByVal SendMsg As String)
10 Try
11 If IP "" Then
12 Dim tcpc As New System.Net.Sockets.TcpClient(IP, 5656)
13 Dim tcpStream As Net.Sockets.NetworkStream = tcpc.GetStream
14 Dim reqStream As New IO.StreamWriter(tcpStream)
15 reqStream.Write(SendMsg)
16 reqStream.Flush()
17 tcpStream.Close()
18 tcpc.Close()
19 End If
20 Catch ex As Exception
21 MsgBox(ex.Message.ToString)
22 End Try
23 End Sub
24 Private Sub MyListen()Sub MyListen()
25 Try
26 Dim ipAddress As System.Net.IPAddress = System.Net.Dns.Resolve(System.Net.Dns.GetHostName).AddressList(0)
27 tcpl = New System.Net.Sockets.TcpListener(ipAddress, 5656)
28 tcpl.Start()
29 While True
30 Dim s As System.Net.Sockets.Socket = tcpl.AcceptSocket()
31 Dim MyBuffer(1024) As Byte
32 Dim i As Integer
33 i = s.Receive(MyBuffer)
34 If i 0 Then
35 Dim lstrRec As String
36 Dim j As Integer
37 For j = 0 To i - 1
38 TextBox1.Text += Chr(MyBuffer(j)) ","
39 Next
40 End If
41 End While
42 Catch ex As Exception
43 MsgBox(ex.Message.ToString)
44 End Try
45 End Sub
46
47 Private Sub Button1_Click()Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
48 SendMessage("192.168.0.61", TextBox2.Text)
49 End Sub
如何用VB.net作的网络聊天工具
很复杂的,而且聊天工具都不是用VB 做的
只是可视化好点
用VB只是可以做一个界面看看
具体的不用的,,,,
JAVA
当前标题:互联网聊天vb.net 互联网聊天的利与弊
当前网址:http://ybzwz.com/article/dohgcdc.html