I am trying to make Chat using sockets. I found an interesting exmaple that was working with VB 2005 but is not working in VB 2008
this is the code of client
Imports System.Net
Imports System.net.Sockets
Imports System.IO
Imports System.Windows.Forms
Public Class Form1
Private ClientSocket As Socket
Private recvBuffer(8191) As Byte
Private Sub btnConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConnect.Click
If btnConnect.Text = "Disconnect" Then
Try
SyncLock ClientSocket
ClientSocket.Shutdown(SocketShutdown.Both)
ClientSocket.Close()
End SyncLock
DisconnectedUI()
Catch
End Try
Else
ClientSocket = New Socket(AddressFamily.InterNetwork, _
SocketType.Stream, ProtocolType.Tcp)
Dim address As IPAddress = _
System.Net.Dns.GetHostEntry(txtAddress.Text).AddressList(0)
Dim endpoint As New IPEndPoint(address, CInt(txtPort.Text))
ClientSocket.BeginConnect(endpoint, AddressOf Connected, Nothing)
End If
End Sub
Public Sub Connected(ByVal ar As IAsyncResult)
Try
ClientSocket.EndConnect(ar)
'-- Call ConnectedUI
Dim cb As New SimpleCallback(AddressOf ConnectedUI)
Me.Invoke(cb)
'-- Start Receiving Data
ClientSocket.BeginReceive(recvBuffer, 0, recvBuffer.Length, _
SocketFlags.None, AddressOf ReceivedData, Nothing)
Catch ex As Exception
'-- Call DisconnectedUI
CallDisconnectedUI()
MessageBox.Show(ex.Message)
End Try
End Sub
Public Sub ReceivedData(ByVal ar As IAsyncResult)
Dim numBytes As Int32
Try
SyncLock ClientSocket
numBytes = ClientSocket.EndReceive(ar)
End SyncLock
Catch ex As Exception
CallDisconnectedUI()
Return
End Try
If numBytes = 0 Then
'-- Disconnected!
CallDisconnectedUI()
Return
End If
'-- We have data!
Dim data As String = _
System.Text.ASCIIEncoding.ASCII.GetString(recvBuffer, 0, numBytes)
CallDisplayTextCallback(data)
'-- Start Receiving Data Again!
ClientSocket.BeginReceive(recvBuffer, 0, recvBuffer.Length, SocketFlags.None, AddressOf ReceivedData, Nothing)
End Sub
Private Sub CallDisplayTextCallback(ByVal Text As String)
Dim cb As New DisplayTextCallback(AddressOf DisplayText)
Dim args() As Object = {Text}
Me.Invoke(cb, args)
End Sub
Private Delegate Sub DisplayTextCallback(ByVal Text As String)
Public Sub DisplayText(ByVal Text As String)
txtDisplay.AppendText(Text)
txtDisplay.SelectionStart = txtDisplay.Text.Length
End Sub
Private Sub CallDisconnectedUI()
Dim cb As New SimpleCallback(AddressOf DisconnectedUI)
Me.Invoke(cb)
End Sub
Private Delegate Sub SimpleCallback()
Public Sub ConnectedUI()
btnConnect.Text = "Disconnect"
btnSend.Enabled = True
Me.AcceptButton = btnSend
End Sub
Public Sub DisconnectedUI()
btnConnect.Text = "Connect"
btnSend.Enabled = False
Me.AcceptButton = Nothing
End Sub
Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
'-- Added this code to make it easy for chatting
Dim SendText As String = txtSend.Text
If SendText.EndsWith(vbCrLf) = False Then
SendText &= vbCrLf
End If
Dim bytes() As Byte = _
System.Text.ASCIIEncoding.ASCII.GetBytes(SendText)
SyncLock ClientSocket
ClientSocket.Send(bytes, bytes.Length, SocketFlags.None)
End SyncLock
'-- Added this code to make it easy for chatting
txtSend.Clear()
txtSend.Focus()
End Sub
End Class
The code runs without erors but cliebt doesn't connects to server and I don't understand why, everything seems to be fine. I am new to sockets.
please tell me why it is not working, how can I fix it??
thanks in advance. | | ShemoPT | You can enable logging by copying that xml into a file with the same name as your executable, but .config appended to the end. It should be in the same directory as your exe.
Since you have two exe's, one for client and one for server, then you should create two files: client.exe.config, and server.exe.config. Replace client/server with the actual names.
Also, do you have any firewall software running on the client, or the server?
feroze
--
My blog
- Marked As Answer byShemoPT Tuesday, September 22, 2009 11:54 AM
- Unmarked As Answer byShemoPT Sunday, September 20, 2009 1:08 PM
- Marked As Answer byShemoPT Sunday, September 20, 2009 1:07 PM
-
| | Feroze Daud | Feroze Daud
Thanks, that was a problem with the firewall, I allowed server from firewall and it works now.
This chat sends message to all users same Message, I want to send different messages to regular users and another to VIP, how should I fix the server project to be able to do that??
- Marked As Answer byShemoPT Tuesday, September 22, 2009 11:54 AM
- Marked As Answer byShemoPT Sunday, September 20, 2009 1:08 PM
- Unmarked As Answer byShemoPT Sunday, September 20, 2009 1:08 PM
-
| | ShemoPT | What do you mean by "client doesnt connect to the server"? What exception are you getting?
Try enabling tracing http://ferozedaud.blogspot.com/2009/08/tracing-with-systemnet.htmland that might help see what is going on.
feroze
--
My blog
| | Feroze Daud | The Programs gives such eror - Image
I found this example here _>Here I watched the video and my source code (that I downloaded from the same site) is exatly the same, but program
is not working for me and is giving erors like in the picture, please tell me what to do
| | ShemoPT | I cannot access the site where you have the error message. Can you copy/paste the text of the exception? Or, the best way to get a quick answer is to run your application with tracing enabled (see above) and paste the logfile, or the snippet around which the problem is shown (in the form of the exception)
feroze
--
My blog
| | Feroze Daud | Feroze Daud
the eror message is:
A connaction attempt failed because the connected party did not properly respond after a period of time, or estavlished connection failes because connected host has faild to respond 77.92.229.65:19203
this happens when I try to run server on remote computer the IP adress of remote computer is 77.92.29.65
but when I try to run it on local computer- both server and client it gives me the following error:
- No such host is known
and marks for eror the following line :
Dim address As IPAddress = _
System.Net.Dns.GetHostEntry(txtAddress.Text).AddressList(0)
I was not able to anable tracing using your blog artical.
| | ShemoPT | You can enable logging by copying that xml into a file with the same name as your executable, but .config appended to the end. It should be in the same directory as your exe.
Since you have two exe's, one for client and one for server, then you should create two files: client.exe.config, and server.exe.config. Replace client/server with the actual names.
Also, do you have any firewall software running on the client, or the server?
feroze
--
My blog
- Marked As Answer byShemoPT Tuesday, September 22, 2009 11:54 AM
- Unmarked As Answer byShemoPT Sunday, September 20, 2009 1:08 PM
- Marked As Answer byShemoPT Sunday, September 20, 2009 1:07 PM
-
| | Feroze Daud | Feroze Daud
Thanks, that was a problem with the firewall, I allowed server from firewall and it works now.
This chat sends message to all users same Message, I want to send different messages to regular users and another to VIP, how should I fix the server project to be able to do that??
- Marked As Answer byShemoPT Tuesday, September 22, 2009 11:54 AM
- Marked As Answer byShemoPT Sunday, September 20, 2009 1:08 PM
- Unmarked As Answer byShemoPT Sunday, September 20, 2009 1:08 PM
-
| | ShemoPT | Your server will need to maintain user list along with their priviliges.Then your server can make sure that it doesnt broadcast VIP message to non-VIP clients.
feroze
--
My blog
| | Feroze Daud |
|