# VBForums CodeBank > CodeBank - Visual Basic .NET >  [2008] Basic TCP Communication

## perito

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

----------


## NagpurDaMunda

thanks

----------


## talmeeno

thnx

----------


## masfenix

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!

----------


## softwareguy74

Very nice, thanks for the sample code!

----------


## vip_scarface_vip

*Thanks alot Have nice day*

----------


## lolita777

Hi,

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

----------


## Atheist

Could you elaborate on your problem? Perhaps post a code snippet of what is causing it?

----------


## BassTeQ2

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

----------


## want a pie

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

----------


## korae

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()

----------


## standard

hi

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

----------


## standard

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

----------


## stefanACM

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:


```
  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

----------


## stefanACM

Any one ?

----------


## Reynomit

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
 :Big Grin: 

hope it helps

----------


## sherrel

I'm a newbie to VB 2010 Express and I just wanted to say a  :Thumb: *huge* thank you to both  :wave: *Atheist* and  :wave: *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!

----------


## ibennz

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

----------


## lecfox

how can i turn this code into a messenger?  :Big Grin:  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?

----------


## loving3232

> 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.
> ...




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

----------


## Vladamir

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?

----------

