使用vb.net创建聊天信使 [英] Creating a chat messenger using vb.net

查看:58
本文介绍了使用vb.net创建聊天信使的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,基本上我在这一部分有困难button1_click.So,基本上当我使用通用IP地址127.0.0.1时,我看到代码工作正常。但是当我输入我的ip地址时该怎么办朋友电脑。要使用以下代码与他聊天,我需要做什么?我完全糊涂了。请帮帮我。





Imports System.Net.Sockets'帮助我们建立和收听连接

Imports System.Threading'帮助我们同时做几件事

Imports System.IO'帮助我们在我们的连接上发送和接收消息







公共类Form1

Dim Listener As New TcpListener(65535)'侦听传入连接

Dim Client As New TcpClient'我发送邮件的计算机

Dim Message As String ="" '我发送的消息



Private Sub Button1_Click(ByVal sender As Object,ByVal e As System.EventArgs)处理Button1.Click

Client = New TcpClient(" 127.0.0.1",65535)









Dim Writer As New StreamWriter(Client.GetStream())

Writer.Write(TextBox1.Text)

Writer.Flush()

End Sub



Private Sub Form1_Load(ByVal sender As System.Object,ByVal e As System.EventArgs)Handles MyBase.Load

Dim ListThread As New Thread(New ThreadStart(AddressOf Listening))'创建主题



ListThread.Start()'开始主题



结束次级



私人子听力()

Listener.Start()



End Sub



Private Sub Timer1_Tick(ByVal sender As System.Obje ct,ByVal e As System.EventArgs)处理Timer1.Tick

如果Listener.Pending = True那么

消息=""

Client = Listener.AcceptTcpClient()



Dim Reader As New StreamReader(Client.GetStream())

虽然Reader.Peek> -1

消息=消息+转换.ToChar(Reader.Read())。ToString

结束时



MsgBox(消息,MsgBoxStyle.OkOnly)

结束如果



结束子

结束类

So, Basically I am having difficulty in this section button1_click.So, basically when i use the universal ip address 127.0.0.1, i see that the code works fine.But what do i do when i enter the ip address of my friends computer. To chat with him using the following code, what do i need to do? I am totally confused. Please help me out.


Imports System.Net.Sockets 'helps us to make and listen connections
Imports System.Threading 'helps us to do several things at the same time
Imports System.IO 'helps us to send and receive messages on our connection



Public Class Form1
Dim Listener As New TcpListener(65535) 'that listens for incoming connections
Dim Client As New TcpClient ' the computer that i am sending messages
Dim Message As String = "" ' the messsage that i m sending

Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Client = New TcpClient("127.0.0.1", 65535)




Dim Writer As New StreamWriter(Client.GetStream())
Writer.Write(TextBox1.Text)
Writer.Flush()
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim ListThread As New Thread(New ThreadStart(AddressOf Listening)) 'Creates the thread

ListThread.Start() 'Starts the thread

End Sub

Private Sub Listening()
Listener.Start()

End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If Listener.Pending = True Then
Message = ""
Client = Listener.AcceptTcpClient()

Dim Reader As New StreamReader(Client.GetStream())
While Reader.Peek > -1
Message = Message + Convert.ToChar(Reader.Read()).ToString
End While

MsgBox(Message, MsgBoxStyle.OkOnly)
End If

End Sub
End Class

推荐答案

请看我对这个问题的评论。

对于一些基本的想法,请看我过去的答案:

套接字编程中的业余问题 [ ^ ],

来自同一端口号码的多个客户端 [ ^ ]。



-SA
Please see my comment to the question.
For some basic ideas, please see my past answers:
an amateur question in socket programming[^],
Multple clients from same port Number[^].

—SA


这篇关于使用vb.net创建聊天信使的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆