Chat client in VB.net
By: Issac in VB.net Tutorials on 2009-02-21
The Chat Client is a Windows based Application and its main function is to send message to Chat Server. The connection request is one which intiates all sort of acivities and Chat client is responsible for that.
The VB.NET Multithreaded Chat Server Program has two sections.
- Chat Server
- Chat Client
The Chat Client here is to connect the PORT 8888 of the Chat Server in "127.0.0.1 ". Here we have given "127.0.0.1", because Chat Server and Chat Client are running on the same machine. When we start the Chat Client program, we have to enter a User Name just for identifying in Server. The Client program connect to the Chat Server and starts a Thread for receiving the messages from client, Here we implement an infinite loop in the function getMessage () and call this function in a Thread.
Create a new VB.NET Windows based project and put the source code in it..
Imports System.Net.Sockets Imports System.Text Public Class Form1 Dim clientSocket As New System.Net.Sockets.TcpClient() Dim serverStream As NetworkStream Dim readData As String Dim infiniteCounter As Integer Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click Dim outStream As Byte() = _ System.Text.Encoding.ASCII.GetBytes(TextBox2.Text + "$") serverStream.Write(outStream, 0, outStream.Length) serverStream.Flush() End Sub Private Sub msg() If Me.InvokeRequired Then Me.Invoke(New MethodInvoker(AddressOf msg)) Else TextBox1.Text = TextBox1.Text + _ Environment.NewLine + " >> " + readData End If End Sub Private Sub Button2_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button2.Click readData = "Conected to Chat Server ..." msg() clientSocket.Connect("127.0.0.1", 8888) 'Label1.Text = "Client Socket Program - Server Connected ..." serverStream = clientSocket.GetStream() Dim outStream As Byte() = _ System.Text.Encoding.ASCII.GetBytes(TextBox3.Text + "$") serverStream.Write(outStream, 0, outStream.Length) serverStream.Flush() Dim ctThread As Threading.Thread = _ New Threading.Thread(AddressOf getMessage) ctThread.Start() End Sub Private Sub getMessage() For infiniteCounter = 1 To 2 infiniteCounter = 1 serverStream = clientSocket.GetStream() Dim buffSize As Integer Dim inStream(10024) As Byte buffSize = clientSocket.ReceiveBufferSize serverStream.Read(inStream, 0, buffSize) Dim returndata As String = _ System.Text.Encoding.ASCII.GetString(inStream) readData = "" + returndata msg() Next End Sub End Class
Add Comment
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
- Your name, rating, website address, town, country, state and comment will be publicly displayed if entered.
- Aside from the data entered into these form fields, other stored data about your comment will include:
- Your IP address (not displayed)
- The time/date of your submission (displayed)
- Your email address will not be shared. It is collected for only two reasons:
- Administrative purposes, should a need to contact you arise.
- To inform you of new comments, should you subscribe to receive notifications.
- A cookie may be set on your computer. This is used to remember your inputs. It will expire by itself.
This policy is subject to change at any time and without notice.
These terms and conditions contain rules about posting comments. By submitting a comment, you are declaring that you agree with these rules:
- Although the administrator will attempt to moderate comments, it is impossible for every comment to have been moderated at any given time.
- You acknowledge that all comments express the views and opinions of the original author and not those of the administrator.
- You agree not to post any material which is knowingly false, obscene, hateful, threatening, harassing or invasive of a person's privacy.
- The administrator has the right to edit, move or remove any comment for any reason and without notice.
Failure to comply with these rules may result in being banned from submitting further comments.
These terms and conditions are subject to change at any time and without notice.
- Data Science
- Android
- React Native
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Related Tutorials
Changes in Controls from VB6 to VB.net
Unstructured Exception Handling in VB.net
Structured Exception Handling in VB.net
Creating Sub Procedures in VB.net
Passing a Variable Number of Arguments to Procedures in VB.net
Specifying Optional Arguments with default values in Procedures in VB.net
Preserving a Variable's Values between Procedure Calls in VB.net
Throwing an Exception in VB.net
Comments