anubhavmax
Member
- Joined
- Jan 21, 2009
- Messages
- 10
- Programming Experience
- Beginner
I have been wrting a code to develop an Instant messenger by modifying an open source code project available at
Home-brew Your Own Instant Messenger App with Visual Studio .NET
Instead of the send message text box and message display text box, I have replaced it with RichTextBox for text formatting (bold/italics/underline/font/color). The problem I am facing is when I format the text and click on send, then the text displayed on the message display box (txtMessageHistory) is unformatted.
Here is the code for server--
The problem here is whenever the server sends or receives a message from the client, it receives it in the form of a string that has no formatting in it.
here is the code for the client-
Rest of the code of client is related to formatting of the rich text boxes that is not relevant to my problem.
I just need to transfer the formatted text from clint to server and then back to the client the same way and display the message with text formatting. Any kind of help would relly be appreciated.
Home-brew Your Own Instant Messenger App with Visual Studio .NET
Instead of the send message text box and message display text box, I have replaced it with RichTextBox for text formatting (bold/italics/underline/font/color). The problem I am facing is when I format the text and click on send, then the text displayed on the message display box (txtMessageHistory) is unformatted.
Here is the code for server--
VB.NET:
Public Class ChatClient
'---contains a list of all the clients
Public Shared AllClients As New HashTable
'---information about the client
Private _client As TcpClient
Private _clientIP As String
Private _ClientNick As String
'---used for sending/receiving data
Private data() As Byte
'---is the nick name being sent?
Private ReceiveNick As Boolean = True
Public Sub New(ByVal client As TcpClient)
_client = client
'---get the client IP address
_clientIP = client.Client.RemoteEndPoint.ToString
'---add the current client to the hash table
AllClients.Add(_clientIP, Me)
'---start reading data from the client in a
' separate thread
ReDim data(_client.ReceiveBufferSize)
_client.GetStream.BeginRead(data, 0, _
CInt(_client.ReceiveBufferSize), _
AddressOf ReceiveMessage, Nothing)
End Sub
<b>Public Sub ReceiveMessage(ByVal ar As IAsyncResult)
'---read from client---
Dim bytesRead As Integer
Try
SyncLock _client.GetStream
bytesRead = _client.GetStream.EndRead(ar)
End SyncLock
'---client has disconnected
If bytesRead < 1 Then
AllClients.Remove(_clientIP)
Broadcast(_ClientNick & _
" has left the chat.")
Exit Sub
Else
'---get the message sent
Dim messageReceived As String = _
System.Text.Encoding.ASCII. _
GetString(data, 0, bytesRead)
'---client is sending its nickname
If ReceiveNick Then
_ClientNick = messageReceived
'---tell everyone client has entered
' the chat
Broadcast(_ClientNick & _
" has joined the chat.")
ReceiveNick = False
Else
'---broadcast the message to everyone
Broadcast(_ClientNick & ">" & _
messageReceived)
End If
End If
'---continue reading from client
SyncLock _client.GetStream
_client.GetStream.BeginRead(data, 0, _
CInt(_client.ReceiveBufferSize), _
AddressOf ReceiveMessage, Nothing)
End SyncLock
Catch ex As Exception
AllClients.Remove(_clientIP)
Broadcast(_ClientNick & _
" has left the chat.")
End Try
End Sub</b>
Public Sub SendMessage(ByVal message As String)
Try
'---send the text
Dim ns As System.Net.Sockets.NetworkStream
SyncLock _client.GetStream
ns = _client.GetStream
End SyncLock
Dim bytesToSend As Byte() = _
System.Text.Encoding.ASCII.GetBytes(message)
ns.Write(bytesToSend, 0, bytesToSend.Length)
ns.Flush()
Catch ex As Exception
Console.WriteLine(ex.ToString)
End Try
End Sub
Finally, the Broadcast() function sends a message to all the clients stored in the AllClients HashTable object.
Public Sub Broadcast(ByVal message As String)
'---log it locally
Console.WriteLine(message)
Dim c As DictionaryEntry
For Each c In AllClients
'---broadcast message to all users
CType(c.Value, _
ChatClient).SendMessage(message & vbCrLf)
Next
End Sub
The problem here is whenever the server sends or receives a message from the client, it receives it in the form of a string that has no formatting in it.
here is the code for the client-
VB.NET:
Public Sub SendMessage(ByVal message As String)
Try
'---Send A Message To The Server
Dim ns As NetworkStream = client.GetStream
Dim data As Byte() = _
System.Text.Encoding.ASCII.GetBytes(message)
'---Send The Text
ns.Write(data, 0, data.Length)
ns.Flush()
Catch ex As Exception
MsgBox("Server Disconnected")
'MsgBox(ex.ToString)
End Try
End Sub
Public Sub ReceiveMessage(ByVal ar As IAsyncResult)
Try
Dim bytesRead As Integer
bytesRead = client.GetStream.EndRead(ar)
If bytesRead < 1 Then
Exit Sub
Else
Dim para() As Object = _
{System.Text.Encoding.ASCII.GetString( _
data, 0, bytesRead)}
Me.Invoke(New delUpdateHistory( _
AddressOf Me.UpdateHistory), para)
End If
client.GetStream.BeginRead( _
data, 0, CInt(client.ReceiveBufferSize), _
AddressOf ReceiveMessage, Nothing)
Catch ex As Exception
End Try
End Sub
Private Sub btnSignIn_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles btnSignIn.Click
If btnSignIn.Text = "Sign In" Then
Try
'---Connect To The Server
client = New TcpClient
client.Connect(TextBox1.Text, portNo)
ReDim data(client.ReceiveBufferSize)
SendMessage(txtNick.Text)
MsgBox("Successfully logged in")
'---Read From The Server
client.GetStream.BeginRead( _
data, 0, CInt(client.ReceiveBufferSize), _
AddressOf ReceiveMessage, Nothing)
btnSignIn.Text = "Sign Out"
btnSend.Enabled = True
Catch ex As Exception
MsgBox("Server is not reachable")
End Try
Else
'---Disconnect From The Server By Signing Out From The Client
Disconnect()
MsgBox("Successfully logged off")
btnSignIn.Text = "Sign In"
btnSend.Enabled = False
End If
End Sub
Public Sub Disconnect()
'---Disconnect From The Server By Closing The Client
Try
client.GetStream.Close()
client.Close()
Catch ex As Exception
'MessageBox.Show("Session Closed ")
End Try
End Sub
'---Delegate And Subroutine To Update The TextBox Control
Public Delegate Sub delUpdateHistory(ByVal str As String)
Public Sub UpdateHistory(ByVal str As String)
txtMessageHistory.AppendText(Str)
End Sub
'--- Disconnect() method to Disconnect The Client From The Server And Close The Client
Private Sub Form1_FormClosing( _
ByVal sender As Object, _
ByVal e As System.Windows.Forms.FormClosingEventArgs) _
Handles Me.FormClosing
Disconnect()
End Sub
Rest of the code of client is related to formatting of the rich text boxes that is not relevant to my problem.
I just need to transfer the formatted text from clint to server and then back to the client the same way and display the message with text formatting. Any kind of help would relly be appreciated.
Last edited by a moderator: