Jump to content
  • Advertisement
Sign in to follow this  
AIdeveloper

TCP Listener

This topic is 2535 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, I'm tryinyg to make a TCP listener in visual basic 2010 requested at college. I manage to create the TCPclient but haven't been able to create the TCP listener class. I have some examples, but I don't know how to set the code. I'm using the visual studio 2010 windows form application to program the TCP, but I'm not sure how to set the design, I don't know if the code goes in a button, a text box, label, etc. It will be really helpful if someone help me get an example of the design. Here is the code I have and I'm trying to use to create a good and simple TCP server. If someone can help me with these I'll be really grateful.

[color="Blue"]Imports System [color="Blue"]Imports System.IO [color="Blue"]Imports System.Net [color="Blue"]Imports System.Net.Sockets [color="Blue"]Imports System.Text [color="Blue"]Imports Microsoft.VisualBasic [color="Blue"]Class MyTcpListener [color="Blue"]Public [color="Blue"]Shared [color="Blue"]Sub Main() [color="Blue"]Dim server [color="Blue"]As TcpListener server=[color="Blue"]nothing [color="Blue"]Try [color="Green"]' Set the TcpListener on port 13000. [color="Blue"]Dim port [color="Blue"]As Int32 = 13000 [color="Blue"]Dim localAddr [color="Blue"]As IPAddress = IPAddress.Parse([color="#A31515"]"127.0.0.1") server = [color="Blue"]New TcpListener(localAddr, port) [color="Green"]' Start listening for client requests. server.Start() [color="Green"]' Buffer for reading data [color="Blue"]Dim bytes(1024) [color="Blue"]As [color="Blue"]Byte [color="Blue"]Dim data [color="Blue"]As [color="Blue"]String = [color="Blue"]Nothing [color="Green"]' Enter the listening loop. [color="Blue"]While [color="Blue"]True Console.Write([color="#A31515"]"Waiting for a connection... ") [color="Green"]' Perform a blocking call to accept requests. [color="Green"]' You could also user server.AcceptSocket() here. [color="Blue"]Dim client [color="Blue"]As TcpClient = server.AcceptTcpClient() Console.WriteLine([color="#A31515"]"Connected!") data = [color="Blue"]Nothing [color="Green"]' Get a stream object for reading and writing [color="Blue"]Dim stream [color="Blue"]As NetworkStream = client.GetStream() [color="Blue"]Dim i [color="Blue"]As Int32 [color="Green"]' Loop to receive all the data sent by the client. i = stream.Read(bytes, 0, bytes.Length) [color="Blue"]While (i <> 0) [color="Green"]' Translate data bytes to a ASCII string. data = System.Text.Encoding.ASCII.GetString(bytes, 0, i) Console.WriteLine([color="#A31515"]"Received: {0}", data) [color="Green"]' Process the data sent by the client. data = data.ToUpper() [color="Blue"]Dim msg [color="Blue"]As [color="Blue"]Byte() = System.Text.Encoding.ASCII.GetBytes(data) [color="Green"]' Send back a response. stream.Write(msg, 0, msg.Length) Console.WriteLine([color="#A31515"]"Sent: {0}", data) i = stream.Read(bytes, 0, bytes.Length) [color="Blue"]End [color="Blue"]While [color="Green"]' Shutdown and end connection client.Close() [color="Blue"]End [color="Blue"]While [color="Blue"]Catch e [color="Blue"]As SocketException Console.WriteLine([color="#A31515"]"SocketException: {0}", e) [color="Blue"]Finally server.[color="Blue"]Stop() [color="Blue"]End [color="Blue"]Try Console.WriteLine(ControlChars.Cr + [color="#A31515"]"Hit enter to continue....") Console.Read() [color="Blue"]End [color="Blue"]Sub [color="Green"]'Main [color="Blue"]End [color="Blue"]Class [color="Green"]'MyTcpListener

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!