|
-
Jun 13th, 2006, 02:08 AM
#1
Thread Starter
Member
[02/03] help creating a messenger
guys im new to .net network programming and i want to make this simple messenger that would allow multiple connections, now i've read and downloaded the tutorial on sockets and the 1client - 1 server chat system. what i want to do is to have the program open with windows and stay in the background only to pop up when ever I (server) send a message to a specific client or all clients. Do I have to make the client server as a windows service? how do i go about doing that?
i plan on using this application at home, i have like 5 pc's at home on 3 floors, and i want to be able to send a message to my sister or someone using a client computer on my home LAN.
Thanks much for reading and your patience...
\
m1k3
-
Jun 13th, 2006, 12:06 PM
#2
Frenzied Member
-
Jun 13th, 2006, 02:31 PM
#3
Hyperactive Member
Re: [02/03] help creating a messenger
Laugh out loud. Dude...MrGTi is right why don't you just use MSN messenger? They have the new Beta called "Windows Live Messenger Beta". Just download that.....it's pretty easy to use. But I'll also so see what I can do.
"Imagination is more important than knowledge" - Albert Einstein, born on March 14th 1879.
Can't find it here on VBForums? Go to the CodeProject. MSDN is your friend . I have such a bad website, my friend decided it would be funny to change the template and he moderates the site for me: visit my site!
"Thinking of you, wherever you are
We pray for our sorrows to end, and hope that our hearts will blend.
Now I will step forward to realize this wish.
And who knows, starting a new journey may not be so hard…
Or maybe it has already begun.
There are many worlds, but they share the same sky
one sky, one destiny..."
-
Jun 13th, 2006, 03:23 PM
#4
New Member
Re: [02/03] help creating a messenger
Hi. I do this task by using the NET SEND command. It is not a chat program, but just a message sender program. The problem with it is.. its functions are limited. They can only recieve the message, but they cannot reply. But in most cases, this is not important, especially if you are an admin.
-
Jun 13th, 2006, 03:26 PM
#5
Fanatic Member
Re: [02/03] help creating a messenger
g0d5_g1ft you may want to take alook at msdn's async socket examples.
despite the bit of chatter you catch quickly when asking hard questions there are actually people here who can help... provided you actually have something you need help with.
MSDN Async Server Socket Example
-
Jun 13th, 2006, 03:30 PM
#6
Hyperactive Member
Re: [02/03] help creating a messenger
Ok first of all you need to have a server. Once you have a server then you can build the chat client. The server has to be listening and then you can start using the chat client. This may not be the best site but it pretty much sums up the things you need: http://www.eggheadcafe.com/articles/20020323.asp Sorry just noticed the guy above me, TokersBall_CDXX has the correct website you should use.
"Imagination is more important than knowledge" - Albert Einstein, born on March 14th 1879.
Can't find it here on VBForums? Go to the CodeProject. MSDN is your friend . I have such a bad website, my friend decided it would be funny to change the template and he moderates the site for me: visit my site!
"Thinking of you, wherever you are
We pray for our sorrows to end, and hope that our hearts will blend.
Now I will step forward to realize this wish.
And who knows, starting a new journey may not be so hard…
Or maybe it has already begun.
There are many worlds, but they share the same sky
one sky, one destiny..."
-
Jun 13th, 2006, 03:39 PM
#7
Re: [02/03] help creating a messenger
Ok first of all you need to have a server
Just to clarify, any computer can be used as a server in the async socket examples. It's just a title they give to the computer everyone is connecting to.
-
Jun 13th, 2006, 03:44 PM
#8
Hyperactive Member
Re: [02/03] help creating a messenger
 Originally Posted by sevenhalo
Just to clarify, any computer can be used as a server in the async socket examples. It's just a title they give to the computer everyone is connecting to.
Yea. Sorry. That's what I meant. You still need to host the server though!
"Imagination is more important than knowledge" - Albert Einstein, born on March 14th 1879.
Can't find it here on VBForums? Go to the CodeProject. MSDN is your friend . I have such a bad website, my friend decided it would be funny to change the template and he moderates the site for me: visit my site!
"Thinking of you, wherever you are
We pray for our sorrows to end, and hope that our hearts will blend.
Now I will step forward to realize this wish.
And who knows, starting a new journey may not be so hard…
Or maybe it has already begun.
There are many worlds, but they share the same sky
one sky, one destiny..."
-
Jun 13th, 2006, 03:49 PM
#9
Re: [02/03] help creating a messenger
 Originally Posted by sheikh78
Yea. Sorry. That's what I meant. You still need to host the server though! 
Yep yep, so long as it's listening. 
I had worked on something like this awhile back. The full source is here (along with the issue I was having):
http://www.vbforums.com/showthread.php?t=390441
It's a modified version of that async socket example Tokers posted. It might be of some help?
(It's crazy to think that project was only 3 months ago... It feels like ages and ages. It's true what they say; as programmers, we live entire lives in the span of a week.)
-
Jun 13th, 2006, 08:48 PM
#10
Thread Starter
Member
Re: [02/03] help creating a messenger
thanks guys, ok i may not be exactly new to .net... ive got the client -server messenger thing done... my only concern was for the client to start with windows, i got that too , the last thing was for the client to be hidden after it starts and just pop up whenever the admin(me) sends a message to the client... much like yahoo or msn, it stays in with the icons by the system clock... hows that? thanks for all the replies....
-
Jun 13th, 2006, 09:09 PM
#11
Hyperactive Member
Re: [02/03] help creating a messenger
"Imagination is more important than knowledge" - Albert Einstein, born on March 14th 1879.
Can't find it here on VBForums? Go to the CodeProject. MSDN is your friend . I have such a bad website, my friend decided it would be funny to change the template and he moderates the site for me: visit my site!
"Thinking of you, wherever you are
We pray for our sorrows to end, and hope that our hearts will blend.
Now I will step forward to realize this wish.
And who knows, starting a new journey may not be so hard…
Or maybe it has already begun.
There are many worlds, but they share the same sky
one sky, one destiny..."
-
Jun 13th, 2006, 09:10 PM
#12
Thread Starter
Member
Re: [02/03] help creating a messenger
thanx sheikh i'll look into this
-
Jun 13th, 2006, 10:09 PM
#13
Hyperactive Member
Re: [02/03] help creating a messenger
Lol. This may be funny but I can't get the server to work. Does anybody have some sample code? I have this as my code from the MSDN article:
VB Code:
Imports System.Net.Sockets
Imports System.Text
Imports System
Imports System.Net
Imports System.Threading
Class TCPSrv
' State object for reading client data asynchronously
Public Class StateObject
' Client socket.
Public workSocket As Socket = Nothing
' Size of receive buffer.
Public Const BufferSize As Integer = 1024
' Receive buffer.
Public buffer(BufferSize) As Byte
' Received data string.
Public sb As New StringBuilder()
End Class 'StateObject
Public Class AsynchronousSocketListener
' Incoming data from the client.
Public Shared data As String = Nothing
' Thread signal.
Public Shared allDone As New ManualResetEvent(False)
Public Sub New()
End Sub 'New
Public Shared Sub StartListening()
' Data buffer for incoming data.
Dim bytes() As Byte = New [Byte](1024) {}
' Establish the local endpoint for the socket.
' The DNS name of the computer
' running the listener is "host.contoso.com".
Dim ipHostInfo As IPHostEntry = Dns.GetHostEntry(Dns.GetHostName())
Dim ipAddress As IPAddress = ipHostInfo.AddressList(0)
Dim localEndPoint As New IPEndPoint(ipAddress, 8000)
' Intializes a TCP/IP socket.
Dim listener As New Socket(AddressFamily.InterNetwork, _
SocketType.Stream, ProtocolType.Tcp)
' Bind the socket to the local endpoint and listen for incoming
' connections.
Try
listener.Bind(localEndPoint)
listener.Listen(1)
While True
' Set the event to nonsignaled state.
allDone.Reset()
' Start an asynchronous socket to listen for connections.
Console.WriteLine("Waiting for a connection...")
listener.BeginAccept(New AsyncCallback(AddressOf AcceptCallback), _
listener)
' Wait until a connection is made before continuing.
allDone.WaitOne()
End While
Catch e As Exception
Console.WriteLine(e.ToString())
End Try
Console.WriteLine(ControlChars.Cr + "Press ENTER to continue...")
Console.Read()
End Sub 'StartListening
Public Shared Sub AcceptCallback(ByVal ar As IAsyncResult)
' Signal the main thread to continue.
allDone.Set()
' Get the socket that handles the client request.
Dim listener As Socket = CType(ar.AsyncState, Socket)
Dim handler As Socket = listener.EndAccept(ar)
' Create the state object.
Dim state As New StateObject()
state.workSocket = handler
handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, _
New AsyncCallback(AddressOf ReadCallback), state)
End Sub 'AcceptCallback
Public Shared Sub ReadCallback(ByVal ar As IAsyncResult)
Dim content As [String] = [String].Empty
' Retrieve the state object and the handler socket
' from the asynchronous state object.
Dim state As StateObject = CType(ar.AsyncState, StateObject)
Dim handler As Socket = state.workSocket
' Read data from client socket.
Dim bytesRead As Integer = handler.EndReceive(ar)
If bytesRead > 0 Then
' There might be more data, so store the data received so far.
state.sb.Append(Encoding.ASCII.GetString(state.buffer, 0, _
bytesRead))
' Check for end-of-file tag. If it is not there, read
' more data.
content = state.sb.ToString()
If content.IndexOf("<EOF>") > -1 Then
' All the data has been read from the
' client. Display it on the console.
Console.WriteLine("Read {0} bytes from socket. " + _
ControlChars.Cr + " Data : {1}", content.Length, content)
' Echo the data back to the client.
Send(handler, content)
Else
' Not all data received. Get more.
handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, _
0, New AsyncCallback(AddressOf ReadCallback), state)
End If
End If
End Sub 'ReadCallback
Private Shared Sub Send(ByVal handler As Socket, ByVal data As [String])
' Convert the string data to byte data using ASCII encoding.
Dim byteData As Byte() = Encoding.ASCII.GetBytes(Data)
' Begin sending the data to the remote device.
handler.BeginSend(byteData, 0, byteData.Length, 0, _
New AsyncCallback(AddressOf SendCallback), handler)
End Sub 'Send
Private Shared Sub SendCallback(ByVal ar As IAsyncResult)
Try
' Retrieve the socket from the state object.
Dim handler As Socket = CType(ar.AsyncState, Socket)
' Complete sending the data to the remote device.
Dim bytesSent As Integer = handler.EndSend(ar)
Console.WriteLine("Sent {0} bytes to client.", bytesSent)
handler.Shutdown(SocketShutdown.Both)
handler.Close()
Catch e As Exception
Console.WriteLine(e.ToString())
End Try
End Sub 'SendCallback
'Entry point that delegates to C-style main Private Function.
Private Overloads Shared Sub Main()
System.Environment.ExitCode = _
Main(System.Environment.GetCommandLineArgs())
End Sub
Public Overloads Shared Function Main(ByVal args() As [String]) As Integer
StartListening()
Return 0
End Function 'Main
End Class 'AsynchronousSocketListener
End Class
NEXT POST
"Imagination is more important than knowledge" - Albert Einstein, born on March 14th 1879.
Can't find it here on VBForums? Go to the CodeProject. MSDN is your friend . I have such a bad website, my friend decided it would be funny to change the template and he moderates the site for me: visit my site!
"Thinking of you, wherever you are
We pray for our sorrows to end, and hope that our hearts will blend.
Now I will step forward to realize this wish.
And who knows, starting a new journey may not be so hard…
Or maybe it has already begun.
There are many worlds, but they share the same sky
one sky, one destiny..."
-
Jun 13th, 2006, 10:10 PM
#14
Hyperactive Member
Re: [02/03] help creating a messenger
And this also from the MSDN article for the chat client:
VB Code:
Imports System
Imports System.Net
Imports System.Net.Sockets
Imports System.Threading
Imports System.Text
' State object for receiving data from remote device.
Public Class StateObject
' Client socket.
Public workSocket As Socket = Nothing
' Size of receive buffer.
Public BufferSize As Integer = 256
' Receive buffer.
Public buffer(256) As Byte
' Received data string.
Public sb As New StringBuilder()
End Class 'StateObject
Public Class AsynchronousClient
' The port number for the remote device.
Private Shared port As Integer = 8000
' ManualResetEvent instances signal completion.
Private Shared connectDone As New ManualResetEvent(False)
Private Shared sendDone As New ManualResetEvent(False)
Private Shared receiveDone As New ManualResetEvent(False)
' The response from the remote device.
Private Shared response As [String] = [String].Empty
Private Shared Sub StartClient()
' Connect to a remote device.
Try
' Establish the remote endpoint for the socket.
' The name of the
' remote device is "host.contoso.com".
Dim ipHostInfo As IPHostEntry = Dns.GetHostEntry("127.0.0.1")
Dim ipAddress As IPAddress = ipHostInfo.AddressList(0)
Dim remoteEP As New IPEndPoint(ipAddress, port)
' Create a TCP/IP socket.
Dim client As New Socket(AddressFamily.InterNetwork, _
SocketType.Stream, ProtocolType.Tcp)
' Connect to the remote endpoint.
client.BeginConnect(remoteEP, AddressOf ConnectCallback, client)
connectDone.WaitOne()
' Send test data to the remote device.
Send(client, "This is a test<EOF>")
sendDone.WaitOne()
' Receive the response from the remote device.
Receive(client)
receiveDone.WaitOne()
' Write the response to the console.
Console.WriteLine("Response received : {0}", response)
' Release the socket.
client.Shutdown(SocketShutdown.Both)
client.Close()
Catch e As Exception
Console.WriteLine(e.ToString())
End Try
End Sub 'StartClient
Private Shared Sub ConnectCallback(ByVal ar As IAsyncResult)
Try
' Retrieve the socket from the state object.
Dim client As Socket = CType(ar.AsyncState, Socket)
' Complete the connection.
client.EndConnect(ar)
Console.WriteLine("Socket connected to {0}", _
client.RemoteEndPoint.ToString())
' Signal that the connection has been made.
connectDone.Set()
Catch e As Exception
Console.WriteLine(e.ToString())
End Try
End Sub 'ConnectCallback
Private Shared Sub Receive(ByVal client As Socket)
Try
' Create the state object.
Dim state As New StateObject()
state.workSocket = client
' Begin receiving the data from the remote device.
client.BeginReceive(state.buffer, 0, state.BufferSize, 0, _
AddressOf ReceiveCallback, state)
Catch e As Exception
Console.WriteLine(e.ToString())
End Try
End Sub 'Receive
Private Shared Sub ReceiveCallback(ByVal ar As IAsyncResult)
Try
' Retrieve the state object and client socket
' from the asynchronous state object.
Dim state As StateObject = CType(ar.AsyncState, StateObject)
Dim client As Socket = state.workSocket
' Read data from the remote device.
Dim bytesRead As Integer = client.EndReceive(ar)
If bytesRead > 0 Then
' There might be more data, so store the data received so far.
state.sb.Append(Encoding.ASCII.GetString(state.buffer, 0, _
bytesRead))
' Get the rest of the data.
client.BeginReceive(state.buffer, 0, state.BufferSize, 0, _
AddressOf ReceiveCallback, state)
Else
' All the data has arrived; put it in response.
If state.sb.Length > 1 Then
response = state.sb.ToString()
End If
' Signal that all bytes have been received.
receiveDone.Set()
End If
Catch e As Exception
Console.WriteLine(e.ToString())
End Try
End Sub 'ReceiveCallback
Private Shared Sub Send(ByVal client As Socket, ByVal data As [String])
' Convert the string data to byte data using ASCII encoding.
Dim byteData As Byte() = Encoding.ASCII.GetBytes(data)
' Begin sending the data to the remote device.
client.BeginSend(byteData, 0, byteData.Length, 0, _
AddressOf SendCallback, client)
End Sub 'Send
Private Shared Sub SendCallback(ByVal ar As IAsyncResult)
Try
' Retrieve the socket from the state object.
Dim client As Socket = CType(ar.AsyncState, Socket)
' Complete sending the data to the remote device.
Dim bytesSent As Integer = client.EndSend(ar)
Console.WriteLine("Sent {0} bytes to server.", bytesSent)
' Signal that all bytes have been sent.
sendDone.Set()
Catch e As Exception
Console.WriteLine(e.ToString())
End Try
End Sub 'SendCallback
'Entry point that delegates to C-style main Private Function.
Private Overloads Shared Sub Main()
System.Environment.ExitCode = _
Main(System.Environment.GetCommandLineArgs())
End Sub
Public Overloads Shared Function Main(ByVal args() As [String]) As Integer
StartClient()
Return 0
End Function 'Main
End Class 'AsynchronousClient
Help? The client keep saying: "System.Net.Sockets.SocketException: No connection could be made because the target machine actively refused it." But if you look at the code it doesn't refuse it.
"Imagination is more important than knowledge" - Albert Einstein, born on March 14th 1879.
Can't find it here on VBForums? Go to the CodeProject. MSDN is your friend . I have such a bad website, my friend decided it would be funny to change the template and he moderates the site for me: visit my site!
"Thinking of you, wherever you are
We pray for our sorrows to end, and hope that our hearts will blend.
Now I will step forward to realize this wish.
And who knows, starting a new journey may not be so hard…
Or maybe it has already begun.
There are many worlds, but they share the same sky
one sky, one destiny..."
-
Jun 13th, 2006, 10:11 PM
#15
Hyperactive Member
Re: [02/03] help creating a messenger
I should also specify that I am using the .NET Framework 2.0, VB.NET 2005
"Imagination is more important than knowledge" - Albert Einstein, born on March 14th 1879.
Can't find it here on VBForums? Go to the CodeProject. MSDN is your friend . I have such a bad website, my friend decided it would be funny to change the template and he moderates the site for me: visit my site!
"Thinking of you, wherever you are
We pray for our sorrows to end, and hope that our hearts will blend.
Now I will step forward to realize this wish.
And who knows, starting a new journey may not be so hard…
Or maybe it has already begun.
There are many worlds, but they share the same sky
one sky, one destiny..."
-
Jun 14th, 2006, 06:14 AM
#16
Hyperactive Member
Re: [02/03] help creating a messenger
"Imagination is more important than knowledge" - Albert Einstein, born on March 14th 1879.
Can't find it here on VBForums? Go to the CodeProject. MSDN is your friend . I have such a bad website, my friend decided it would be funny to change the template and he moderates the site for me: visit my site!
"Thinking of you, wherever you are
We pray for our sorrows to end, and hope that our hearts will blend.
Now I will step forward to realize this wish.
And who knows, starting a new journey may not be so hard…
Or maybe it has already begun.
There are many worlds, but they share the same sky
one sky, one destiny..."
-
Jun 14th, 2006, 08:18 AM
#17
Fanatic Member
Re: [02/03] help creating a messenger
in your server code...
VB Code:
Dim ipHostInfo As IPHostEntry = Dns.GetHostEntry(Dns.GetHostName())
Dim ipAddress As IPAddress = ipHostInfo.AddressList(0)
Dim localEndPoint As New IPEndPoint(ipAddress, 8000)
basically gets the first interface address and makes an endpoint out of it.
127.0.0.1 is not an interface, but rather localhost loopback found in your host file.
try connecting to your system's real ip address... (the first interface when you run ipconfig)
-
Jun 14th, 2006, 01:46 PM
#18
Hyperactive Member
Re: [02/03] help creating a messenger
In the server code are you saying that I should connect to my computer's ip address? like:
VB Code:
Dim ipHostInfo As IPHostEntry = Dns.GetHostEntry("xxx.xxx.xxx.xxx")
Dim ipAddress As IPAddress = ipHostInfo.AddressList(0)
Dim localEndPoint As New IPEndPoint(ipAddress, 8000)
? And for the client should I change any code in there? Because the client, once it starts automatically disappears....I think it is something with the code...I'll check out in the meantime.
"Imagination is more important than knowledge" - Albert Einstein, born on March 14th 1879.
Can't find it here on VBForums? Go to the CodeProject. MSDN is your friend . I have such a bad website, my friend decided it would be funny to change the template and he moderates the site for me: visit my site!
"Thinking of you, wherever you are
We pray for our sorrows to end, and hope that our hearts will blend.
Now I will step forward to realize this wish.
And who knows, starting a new journey may not be so hard…
Or maybe it has already begun.
There are many worlds, but they share the same sky
one sky, one destiny..."
-
Jun 14th, 2006, 04:54 PM
#19
Hyperactive Member
Re: [02/03] help creating a messenger
Anyway I got my code to work. But my server can't be accessed from computers outside my home. I tell their clients to connect to my ip address to access the server console....but it doesn't work for them, yet the same client works for me. Any ideas how to fix this?
"Imagination is more important than knowledge" - Albert Einstein, born on March 14th 1879.
Can't find it here on VBForums? Go to the CodeProject. MSDN is your friend . I have such a bad website, my friend decided it would be funny to change the template and he moderates the site for me: visit my site!
"Thinking of you, wherever you are
We pray for our sorrows to end, and hope that our hearts will blend.
Now I will step forward to realize this wish.
And who knows, starting a new journey may not be so hard…
Or maybe it has already begun.
There are many worlds, but they share the same sky
one sky, one destiny..."
-
Jun 14th, 2006, 07:45 PM
#20
Hyperactive Member
Re: [02/03] help creating a messenger
Help? Por favor? Civu Play? (ok so i don't know the exact spelling in french, its been a long time). LOOK AT ABOVE PLEASE IF YOU ARE COMING FROM MY OTHER THREAD!
Last edited by sheikh78; Jun 14th, 2006 at 08:07 PM.
"Imagination is more important than knowledge" - Albert Einstein, born on March 14th 1879.
Can't find it here on VBForums? Go to the CodeProject. MSDN is your friend . I have such a bad website, my friend decided it would be funny to change the template and he moderates the site for me: visit my site!
"Thinking of you, wherever you are
We pray for our sorrows to end, and hope that our hearts will blend.
Now I will step forward to realize this wish.
And who knows, starting a new journey may not be so hard…
Or maybe it has already begun.
There are many worlds, but they share the same sky
one sky, one destiny..."
-
Jun 14th, 2006, 08:13 PM
#21
Addicted Member
Re: [02/03] help creating a messenger
 Originally Posted by sheikh78
Anyway I got my code to work. But my server can't be accessed from computers outside my home. I tell their clients to connect to my ip address to access the server console....but it doesn't work for them, yet the same client works for me. Any ideas how to fix this?
Are you behind a firewall or router? If so you may need to open the ports your server is using or doing some port forwarding, I think.
-
Jun 14th, 2006, 08:15 PM
#22
Hyperactive Member
Re: [02/03] help creating a messenger
I opened up ports using code and I had turned off my firewall when this was happening.
"Imagination is more important than knowledge" - Albert Einstein, born on March 14th 1879.
Can't find it here on VBForums? Go to the CodeProject. MSDN is your friend . I have such a bad website, my friend decided it would be funny to change the template and he moderates the site for me: visit my site!
"Thinking of you, wherever you are
We pray for our sorrows to end, and hope that our hearts will blend.
Now I will step forward to realize this wish.
And who knows, starting a new journey may not be so hard…
Or maybe it has already begun.
There are many worlds, but they share the same sky
one sky, one destiny..."
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|