VS 2005 which one i should use for chat server?-VBForums
Results 1 to 10 of 10

Thread: which one i should use for chat server?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2010
    Posts
    309

    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

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    86,138

    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.

    2007-2014

    Why is my data not saved to my database? | MSDN Data Walkthroughs
    MSDN "How Do I?" Videos: VB | C#
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts) | WP8 Turnstile Feather Transition with Pivot Control
    Beginner Tutorials: VB | C# | SQL

  3. #3
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    6,993

    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

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2010
    Posts
    309

    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 08:08 PM.

  5. #5
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    6,993

    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.

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2010
    Posts
    309

    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 08:22 PM.

  7. #7
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    6,993

    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.

  8. #8
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    86,138

    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.

    2007-2014

    Why is my data not saved to my database? | MSDN Data Walkthroughs
    MSDN "How Do I?" Videos: VB | C#
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts) | WP8 Turnstile Feather Transition with Pivot Control
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2010
    Posts
    309

    Re: which one i should use for chat server?

    Quote Originally Posted by DataMiser View Post
    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?

  10. #10
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    6,993

    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 04: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
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.