Chat application in VB.NET

Page 1 of 1

6 Replies - 12827 Views - Last Post: 07 May 2010 - 05:51 PM Rate Topic: -----

#1 musicmaniac123   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 28-June 08

Chat application in VB.NET

Posted 28 June 2008 - 11:22 AM

Hello, i am also working on a chat application. Its been dificult for me because i have visual basics 2008 and i have had a hard time finding any information about a chat program in vb2008. I dont have very much knowledge on the topic of sockets but i think that is the answer to my problem. If someone could put together a simple application (in visual basics express edition 2008 --unless it can be converted without a problem--) to help me achieve my goal. Because of the policy you have at Dream.in.color, i will show what i have so far. Your example will help me because i will be able to study it and figure out what it is doing. I am only 14 yrs old so any help would be greatly appreciated.

I got this code from a website for a chat application in vb.net.
It is a console application for the server.
Imports System.Net.Sockets
Imports System.Text

Module Module1
	Dim clientsList As New Hashtable
	Sub Main()
		Dim serverSocket As New TcpListener(8888)
		Dim clientSocket As TcpClient
		Dim infiniteCounter As Integer
		Dim counter As Integer

		serverSocket.Start()
		msg("Chat Server Started ....")
		counter = 0
		infiniteCounter = 0
		For infiniteCounter = 1 To 2
			infiniteCounter = 1
			counter += 1
			clientSocket = serverSocket.AcceptTcpClient()

			Dim bytesFrom(10024) As Byte
			Dim dataFromClient As String

			Dim networkStream As NetworkStream = _
			clientSocket.GetStream()
			networkStream.Read(bytesFrom, 0, CInt(clientSocket.ReceiveBufferSize))
			dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom)
			dataFromClient = _
			dataFromClient.Substring(0, dataFromClient.IndexOf("$"))

			clientsList(dataFromClient) = clientSocket

			broadcast(dataFromClient + " Joined ", dataFromClient, False)

			msg(dataFromClient + " Joined chat room ")
			Dim client As New handleClient
			client.startClient(clientSocket, dataFromClient, clientsList)

		Next

		clientSocket.Close()
		serverSocket.Stop()
		msg("exit")
		Console.ReadLine()
	End Sub

	Sub msg(ByVal mesg As String)
		mesg.Trim()
		Console.WriteLine(">> " + mesg)
	End Sub

	Private Sub broadcast(ByVal msg As String, _
	ByVal uName As String, ByVal flag As Boolean)
		Dim Item As DictionaryEntry
		For Each Item In clientsList
			Dim broadcastSocket As TcpClient
			broadcastSocket = CType(Item.Value, TcpClient)
			Dim broadcastStream As NetworkStream = _
					broadcastSocket.GetStream()
			Dim broadcastBytes As [Byte]()

			If flag = True Then
				broadcastBytes = _
			  Encoding.ASCII.GetBytes(uName + " says : " + msg)
			Else
				broadcastBytes = Encoding.ASCII.GetBytes(msg)
			End If

			broadcastStream.Write(broadcastBytes, 0, broadcastBytes.Length)
			broadcastStream.Flush()
		Next
	End Sub

	Public Class handleClient
		Dim clientSocket As TcpClient
		Dim clNo As String
		Dim clientsList As Hashtable

		Public Sub startClient(ByVal inClientSocket As TcpClient, _
		ByVal clineNo As String, ByVal cList As Hashtable)
			Me.clientSocket = inClientSocket
			Me.clNo = clineNo
			Me.clientsList = cList
			Dim ctThread As Threading.Thread = _
			 New Threading.Thread(AddressOf doChat)
			ctThread.Start()
		End Sub

		Private Sub doChat()
			Dim infiniteCounter As Integer
			Dim requestCount As Integer
			Dim bytesFrom(10024) As Byte
			Dim dataFromClient As String
			Dim sendBytes As [Byte]()
			Dim serverResponse As String
			Dim rCount As String
			requestCount = 0
			For infiniteCounter = 1 To 2
				infiniteCounter = 1
				Try
					requestCount = requestCount + 1
					Dim networkStream As NetworkStream = _
							clientSocket.GetStream()
					networkStream.Read(bytesFrom, 0, CInt(clientSocket.ReceiveBufferSize))
					dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom)
					dataFromClient = _
						dataFromClient.Substring(0, dataFromClient.IndexOf("$"))
					msg("From client - " + clNo + " : " + dataFromClient)
					rCount = Convert.ToString(requestCount)

					broadcast(dataFromClient, clNo, True)
				Catch ex As Exception
					MsgBox(ex.ToString)
				End Try
			Next
		End Sub

	End Class
End Module 




Once again, any help would be greatly appreciated

Is This A Good Question/Topic? 0
  • +

Replies To: Chat application in VB.NET

#2 PsychoCoder   User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1660
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Chat application in VB.NET

Posted 28 June 2008 - 04:53 PM

@musicmaniac123: The code you posted is in VB.NET (Not Visual Basic 6 or older, which is what the Visual Basic forum is for), so I split it off and made it it's own thread in the proper forum :)
Was This Post Helpful? 0
  • +
  • -

#3 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5485
  • View blog
  • Posts: 14,449
  • Joined: 18-April 07

Re: Chat application in VB.NET

Posted 28 June 2008 - 05:24 PM

I have located the ebook version of a book I have in my library which covers socket / remoting connections in VB.NET. One of the chapters shows you how to build your own messenger for chatting and provides the step by step code and screenshots. Should prove useful to you. I have uploaded it to my private server for you to download. Be sure to right click the link and choose "Save As"

Let me know after you have downloaded it. Thanks.

Apress - Peer to Peer with VB.NET

Enjoy! :)
Was This Post Helpful? 1

#4 bruce2424   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 2
  • Joined: 26-June 08

Re: Chat application in VB.NET

Posted 11 July 2008 - 11:09 AM

hi ,

you can find a multi threded chat server program here in this link

http://vb.net-inform...programming.htm

also in this site i found the basics of socket programs , plz check it before u go for programming chat server

http://vb.net-inform...ns_tutorial.htm

brl
Was This Post Helpful? 1

#5 slimtugo   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 28-October 08

Re: Chat application in VB.NET

Posted 28 October 2008 - 09:02 AM

Hi
If you are really interested in building a chat application the likes of yahoo messenger send me an email. We could work together.
Was This Post Helpful? 0
  • +
  • -

#6 Guest_Morbidity*


Reputation:

Re: Chat application in VB.NET

Posted 07 May 2010 - 05:11 PM

Im really interested in getting a chat application in vb moving forward... Anyone who is wanting to help please contact me at {REMOVED} or {REMOVED} or on yim...
Was This Post Helpful? 0

#7 PsychoCoder   User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1660
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Chat application in VB.NET

Posted 07 May 2010 - 05:51 PM

You dig up an old post just trying to get people to write code for you?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1