Menu
Forums
All threads
Latest threads
New posts
Trending threads
New posts
Search forums
Trending
What's new
New posts
New profile posts
Latest activity
Members
Current visitors
New profile posts
Search profile posts
Upgrades
Log in
Register
What's new
Search
Search
Search titles only
By:
All threads
Latest threads
New posts
Trending threads
New posts
Search forums
Menu
Log in
Register
Navigation
Install the app
Install
More options
Contact us
Close Menu
Forums
Software Development
Programming
Tutorials
[C#] How to create a simple TCP Listener
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
<blockquote data-quote="TheRealMoonman" data-source="post: 339920" data-attributes="member: 52707"><p>Hello, im going to show you how to create a simple TCP listener in C#, this is a very basic way on how to do it, anyways enjoy <img src="/styles/default/xenforo/smilies/emojione/smile.png" class="smilie" loading="lazy" alt=":)" title="Smile :)" data-shortname=":)" /></p><p></p><p><strong>DOWNLOADS:</strong></p><p>Visual Express: <a href="https://www.visualstudio.com/en-us/products/visual-studio-express-vs.aspx" target="_blank">https://www.visualstudio.com/en-us/products/visual-studio-express-vs.aspx</a></p><p></p><p>1. Create a console application</p><p>And put this code in</p><p>[CODE]using System;</p><p>using System.Collections.Generic;</p><p>using System.Linq;</p><p>using System.Text;</p><p>using System.Threading.Tasks;</p><p>using System.Net.Sockets;</p><p>using System.IO;</p><p></p><p>namespace MyLocal</p><p>{</p><p> class Program</p><p> {</p><p> static void Main(string[] args)</p><p> {</p><p> TcpListener listener = new TcpListener(8080);</p><p> listener.Start();</p><p></p><p> while (true)</p><p> {</p><p> Console.WriteLine("Waiting for a connection");</p><p> TcpClient client = listener.AcceptTcpClient();</p><p> StreamReader sr = new StreamReader(client.GetStream());</p><p> StreamWriter sw = new StreamWriter(client.GetStream());</p><p> try</p><p> {</p><p> // Request</p><p> string request = sr.ReadLine();</p><p> Console.WriteLine(request);</p><p> string[] tokens = request.Split(' ');</p><p> string page = tokens[1];</p><p> if (page == "/")</p><p> {</p><p> page = "/default.htm";</p><p> }</p><p></p><p> //Find File</p><p> StreamReader file = new StreamReader("../../web" + page);</p><p> sw.WriteLine("HTTP/1.0 200 OK\n");</p><p></p><p> //Send File</p><p> string data = file.ReadLine();</p><p> while (data != null)</p><p> {</p><p> sw.WriteLine(data);</p><p> sw.Flush();</p><p> data = file.ReadLine();</p><p> }</p><p> }</p><p> catch (Exception e)</p><p> {</p><p> // error</p><p> sw.WriteLine("HTTP/1.0 404 OK\n");</p><p> sw.WriteLine("<h1> Sorry, but your file was not found!</h1>");</p><p> sw.Flush();</p><p> }</p><p> client.Close();</p><p> }</p><p> }</p><p> }</p><p>}</p><p>[/CODE]</p><p><strong>NOTE: THIS IS NOT A HELP THREAD, ITS SIMPLE</strong></p><p><strong></strong></p><p><strong>Change directory it reads from here: <a href="http://prntscr.com/7gbb52" target="_blank">http://prntscr.com/7gbb52</a></strong></p></blockquote><p></p>
[QUOTE="TheRealMoonman, post: 339920, member: 52707"] Hello, im going to show you how to create a simple TCP listener in C#, this is a very basic way on how to do it, anyways enjoy :) [B]DOWNLOADS:[/B] Visual Express: [URL]https://www.visualstudio.com/en-us/products/visual-studio-express-vs.aspx[/URL] 1. Create a console application And put this code in [CODE]using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Net.Sockets; using System.IO; namespace MyLocal { class Program { static void Main(string[] args) { TcpListener listener = new TcpListener(8080); listener.Start(); while (true) { Console.WriteLine("Waiting for a connection"); TcpClient client = listener.AcceptTcpClient(); StreamReader sr = new StreamReader(client.GetStream()); StreamWriter sw = new StreamWriter(client.GetStream()); try { // Request string request = sr.ReadLine(); Console.WriteLine(request); string[] tokens = request.Split(' '); string page = tokens[1]; if (page == "/") { page = "/default.htm"; } //Find File StreamReader file = new StreamReader("../../web" + page); sw.WriteLine("HTTP/1.0 200 OK\n"); //Send File string data = file.ReadLine(); while (data != null) { sw.WriteLine(data); sw.Flush(); data = file.ReadLine(); } } catch (Exception e) { // error sw.WriteLine("HTTP/1.0 404 OK\n"); sw.WriteLine("<h1> Sorry, but your file was not found!</h1>"); sw.Flush(); } client.Close(); } } } } [/CODE] [B]NOTE: THIS IS NOT A HELP THREAD, ITS SIMPLE Change directory it reads from here: [URL]http://prntscr.com/7gbb52[/URL][/B] [/QUOTE]
Insert quotes…
Verification
Post reply
Forums
Software Development
Programming
Tutorials
[C#] How to create a simple TCP Listener
Top