Results 1 to 21 of 21

Thread: [2008] Basic TCP Communication

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Dec 2007
    Posts
    167

    [2008] Basic TCP Communication

    Ive been wanting to create a simple Server-Client programs and Ive searched the net alot, but all the coding online are very complex for a newbie. So Atheist helped me code a simple TCP communication program
    The original topic is
    http://www.vbforums.com/showthread.php?t=502795

    Attached is the Server code and Client code
    have fun
    Attached Files Attached Files

  2. #2
    New Member
    Join Date
    Feb 2008
    Posts
    1

    Re: [2008] Basic TCP Communication

    thanks

  3. #3
    New Member
    Join Date
    Feb 2008
    Posts
    3

    Re: [2008] Basic TCP Communication

    thnx

  4. #4
    Fanatic Member
    Join Date
    Feb 2006
    Posts
    607

    Re: [2008] Basic TCP Communication

    Remember: TCPListener only listens on LOCAL IP.

    If your using a router, or a modem that assigns your computer a local IP then you must do the following:

    A scenario:

    Say your server on your computer listens to port 43001 on your local IP.

    Your client on another computer tries to connect to your WAN IP but throws an exception becuase the computer did not respond in a timely manner.

    What you should do:

    Since your listening to port 43001 on local IP (ex: 192.168.2.1), you must forward ports.

    You must forward the same port from your router to your local IP. For example
    if I am connecting to 43.11.14.50 port 43001. I must do:

    forward 43.11.14.50:43001 -> 192.168.2.1: 43001.

    Make sure your firewall allows these ports to go through.

    ALSO OPENING PORTS IS VERY DANGEROUS. YOU SHOULD RESEARCH INTO PROPER SECURITY IF YOUR DOING THIS!

  5. #5
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,091

    Re: [2008] Basic TCP Communication

    Very nice, thanks for the sample code!

    Visual Studio 2010

  6. #6
    New Member
    Join Date
    Apr 2007
    Posts
    5

    Re: [2008] Basic TCP Communication

    Thanks alot Have nice day

  7. #7
    New Member
    Join Date
    Aug 2008
    Posts
    15

    Re: [2008] Basic TCP Communication

    Hi,

    Just seems to stick on finding an Ident... until it pings out

  8. #8
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [2008] Basic TCP Communication

    Could you elaborate on your problem? Perhaps post a code snippet of what is causing it?
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  9. #9
    New Member
    Join Date
    Oct 2007
    Posts
    10

    Re: [2008] Basic TCP Communication

    This code is exactly what I was after thank you.
    One small thing though, the client app sends a message saying /DISCONNECT before it disconnects, which fires an event on the server. Is there anyway to have the server fire an event when a client leaves, without needing the client to send a message?

    Thanks

  10. #10
    Lively Member
    Join Date
    Sep 2007
    Posts
    73

    Re: [2008] Basic TCP Communication

    brilliant code, thanks heaps, I'm sure plenty of people will benefit from this

  11. #11
    Member
    Join Date
    Jan 2010
    Posts
    46

    Re: [2008] Basic TCP Communication

    what if you want to stop listener? I mean if the user wants to change username or port?

    This is where you start to listen from clients:

    listener = New TcpListener(IPAddress.Any, txtPort.Text)
    listener.Start() 'Start listening.
    listenThread = New Thread(AddressOf DoListen)
    listenThread.IsBackground = True
    listenThread.Start()

    And here's the DoListen

    Private Sub DoListen()
    Dim incomingClient As System.Net.Sockets.TcpClient
    Do
    incomingClient = listener.AcceptTcpClient
    Dim connClient As New ConnectedClient(incomingClient, Me)
    AddHandler connClient.dataReceived, AddressOf Me.MessageReceived
    clients.Add(connClient) 'Adds the connected client to the list of connected clients.
    Me.MessageReceived
    Loop
    End Sub

    How do you stop listener? I tried doing this but I always get an error:

    listener.Stop()
    listenThread.Abort()

  12. #12
    Addicted Member
    Join Date
    Jul 2010
    Posts
    134

    Re: [2008] Basic TCP Communication

    hi

    please share Solution for newbie like me for this.. you will help me a lot if you share

  13. #13
    Addicted Member
    Join Date
    Jul 2010
    Posts
    134

    Re: [2008] Basic TCP Communication

    Im having a problem creating Design ....

    when i have pasted to my form1.vb the whole codes for server theres a lot of red line even created a design

    'We have
    '4 textboxes txtSend, txtUsername, txtPort, txtMain (txtMain is multiline and readonly)
    'lblConnection
    'btnListen, btnSend, btnSendAll
    'listbox lbClients

    but on client it was ok.. help

  14. #14
    Member
    Join Date
    Jul 2010
    Posts
    63

    Re: [2008] Basic TCP Communication

    Thanks for example,
    but i two problems:

    1. When I want to send every message from every client to all connected clients (multiusers chat) I try with this:
    Code:
      For Each cc As ConnectedClient In clients
                        cc.SendMessage(receivedString)
                    Next
    but problem is because when is connected 10-12 peoples and writing to much sometime happend that two, or more messages going in one LINE.
    For example, if I send "test", my friend 1 send "test2" its showing like this:
    stefanACM: testfriend1: test2
    How to solve that ???

    2. What is easiest way to transfer CLIENT LIST to every client ?


    Thanks in advice

  15. #15
    Member
    Join Date
    Jul 2010
    Posts
    63

    Re: [2008] Basic TCP Communication

    Any one ?

  16. #16
    New Member
    Join Date
    Sep 2011
    Location
    Melbourne, Victoria
    Posts
    3

    Re: [2008] Basic TCP Communication

    Hey i have been playing around with this code in VB .net 2010 and its really cool,

    RE: You would might need to add & vbcrlf to the end here:

    For Each cc As ConnectedClient In clients
    cc.SendMessage(RecievedString & vbcrlf)
    Next


    hope it helps

  17. #17
    New Member
    Join Date
    Jan 2012
    Location
    Antioch, CA
    Posts
    3

    Re: [2008] Basic TCP Communication

    I'm a newbie to VB 2010 Express and I just wanted to say a huge thank you to both Atheist and perito for your threads on Basic TCP Communication.

    I had a small project where I wanted to download trace data from an RF spectrum analyzer and place it into a .CSV file for analysis. Your comments, tutorials and examples have proved invaluable to me. My little project has been a success!

    Thanks so much!

  18. #18
    Hyperactive Member ibennz's Avatar
    Join Date
    Aug 2010
    Posts
    282

    Re: [2008] Basic TCP Communication

    How send a message from server to client? I cannot make it work proprely.

  19. #19
    Addicted Member lecfox's Avatar
    Join Date
    Dec 2011
    Location
    Jamaica
    Posts
    174

    Re: [2008] Basic TCP Communication

    how can i turn this code into a messenger? as in multiple ppl sending message to each other? do i need to send the message to the server thn to the client?
    for example

    Client 1 to Client 2

    would be like C1 to S thn S to C2

    right?
    FOX Designs

  20. #20
    New Member
    Join Date
    Aug 2012
    Posts
    1

    Re: [2008] Basic TCP Communication

    Quote Originally Posted by masfenix View Post
    Remember: TCPListener only listens on LOCAL IP.

    If your using a router, or a modem that assigns your computer a local IP then you must do the following:

    A scenario:

    Say your server on your computer listens to port 43001 on your local IP.

    Your client on another computer tries to connect to your WAN IP but throws an exception becuase the computer did not respond in a timely manner.

    What you should do:

    Since your listening to port 43001 on local IP (ex: 192.168.2.1), you must forward ports.

    You must forward the same port from your router to your local IP. For example
    if I am connecting to 43.11.14.50 port 43001. I must do:

    forward 43.11.14.50:43001 -> 192.168.2.1: 43001.

    Make sure your firewall allows these ports to go through.

    ALSO OPENING PORTS IS VERY DANGEROUS. YOU SHOULD RESEARCH INTO PROPER SECURITY IF YOUR DOING THIS!


    this was very helpful
    I knew the code for local IPs but I hadnt any information about forwarding
    really helped

  21. #21
    Hyperactive Member Vladamir's Avatar
    Join Date
    Feb 2012
    Location
    Miami, FL
    Posts
    486

    Re: [2008] Basic TCP Communication

    I'm attempting to run this in VS 2012 Express....but the server is showing 58 errors and the client is showing 50 errors. Is this just not compatible with VS 2012 or should I keep trying?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width