-
Feb 22nd, 2012, 07:35 PM
#1
Thread Starter
Hyperactive Member
which one i should use for chat server?
Hi guys,
I need your help. I am working on the client-server on my application and I am unsure which one of those method that I should use for the chat server.
When I tried this:
Code:
Dim port As Int32 = 13000
Dim localAddr As IPAddress = IPAddress.Parse("127.0.0.1")
server = New TcpListener(localAddr, port)
' Start listening for client requests.
server.Start()
Try
MessageBox.Show("connected!")
Catch ex As Exception
'reconnect to the server
End Try
And also when I tried this:
Code:
Dim clientSocket As New System.Net.Sockets.TcpClient()
Dim serverStream As NetworkStream
clientSocket.Connect("127.0.0.1", 13000)
If clientSocket.Connected Then
MessageBox.Show("connected!")
Else
'reconnect to the server
End If
I found that both of these methods are the same, I can connect to the server when I use either of them. Please can you tell me which one of them is the easy way and which one of them that I should use to connect to the server, send message...etc?
Thanks in advance
-
Feb 22nd, 2012, 08:36 PM
#2
Re: which one i should use for chat server?
Those two code snippets are completely different. The first one is for the server and the second is for the client. A server is, by definition, a piece of hardware or software that receives connection requests while a client is a piece of hardware or software that makes the connection request. In this case, your TcpListener is the server, listening for connection requests from clients. The TcpClient is the client, requesting the connection. When the connection request is received and accepted, the server creates a TcpClient as well and the two TcpClient objects basically act like two telephones at either end of a call.
-
Feb 22nd, 2012, 08:38 PM
#3
Re: which one i should use for chat server?
The first one doesn't connect to a server. It is a server. Your code simply tells it to start listening for an incoming client connection which you would then need to Accept with a TCPClient or Socket object.
The second one actually trys to connect to a server.
Edit: Looks like I was a little late on that one
-
Feb 22nd, 2012, 09:04 PM
#4
Thread Starter
Hyperactive Member
Re: which one i should use for chat server?
Thanks for the suggest guys, I am going to use the first code which it works well for me. However, please can you help me with if statement as I want to listen to the server to see if the client is connected then display the messagebox.
When I tried this:
Code:
if server.Server.Connected = true)
MessageBox.Show("connected!")
End If
The messagebox doesn't display, so when i tried this:
Code:
if server.Server.Connected)
MessageBox.Show("connected!")
End If
its still the same, so I don't know which property that I should use.
Here's the current code:
Code:
Dim server As TcpListener = Nothing
server = Nothing
Try
Dim port As Int32 = 13000
Dim localAddr As IPAddress = IPAddress.Parse("127.0.0.1")
server = New TcpListener(localAddr, port)
' Start listening for client requests.
server.Start()
Dim clientSocket As New System.Net.Sockets.TcpClient()
If server.Server.Accept() Then
MessageBox.Show("connected!")
End If
Catch ex As Exception
'reconnect to the server
End Try
Any idea what property that I should use if I wish to listen to the server to see if the client is connected?
Last edited by PillKilla; Feb 22nd, 2012 at 09:08 PM.
-
Feb 22nd, 2012, 09:09 PM
#5
Re: which one i should use for chat server?
You need to go back and read the posts again. Your first Code sample is not connecting to anything. It is not a client it is a listener which listens for and allows a client to connect. If you want to connect to a server you use a client.
-
Feb 22nd, 2012, 09:16 PM
#6
Thread Starter
Hyperactive Member
Re: which one i should use for chat server?
Oh sorry, it was my mistake. I didn't realise what the two methods are using for so let me get this, if I want to host a server, I will use the listener to host as a server and the users will connect to my server by using tcpclient as a client. Is that correct?
If so I want to know in what property that i should use to listen to the server whether if i connect to my own server and get accepted?
Last edited by PillKilla; Feb 22nd, 2012 at 09:22 PM.
-
Feb 22nd, 2012, 09:36 PM
#7
Re: which one i should use for chat server?
Yes you would use the listener to listen for incoming clients. You would then need to check for pending clients periodically, in a timer for example.
When the Listener.Pending=True then there is a Client trying to connect. You then use Listener.Accept to create a client process that will talk to the incoming client.
There are lots of samples on the net, you may want to do a search.
-
Feb 22nd, 2012, 10:39 PM
#8
Re: which one i should use for chat server?
You might like to follow the CodeBank link in my signature and check out my Asynchronous TCP thread for an example of a multi-client server using the TcpListener and TcpClient classes.
-
Feb 24th, 2012, 04:50 PM
#9
Thread Starter
Hyperactive Member
Re: which one i should use for chat server?
Originally Posted by DataMiser
Yes you would use the listener to listen for incoming clients. You would then need to check for pending clients periodically, in a timer for example.
When the Listener.Pending=True then there is a Client trying to connect. You then use Listener.Accept to create a client process that will talk to the incoming client.
There are lots of samples on the net, you may want to do a search.
Thanks for your help, do you know what is the default ports for the tcpclient?
-
Feb 24th, 2012, 05:31 PM
#10
Re: which one i should use for chat server?
You need to set the TCPClient port equal to the port the server to which you want to connect is listening on.
Edit: That is assuming you are talking about a client trying to connect to the server. If you are talking about the client process that would be spawned by the accept connection it will default to the port being used.
Last edited by DataMiser; Feb 24th, 2012 at 05:35 PM.
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
|