(Lviv community of .NET developers)

TCP ports forwarding, tunneling, proxies

April 25, 2008 05:32 by alexk
Hi Everyone,

Today I want to share a small piece of code that demonstrate TCP Sockets usage in .NET.

This is not very advanced example of sockets programming, but at the same time it show a lot of technics that you can use in programming, like:
- debugging of the windows services
- async sockets
- async operations and there synchronization
- events used for service controlling
- registry access
- advanced tracing/logs
- and etc.

Introduction

The idea was to write a simple TCP proxy class that will help us to forward ports (or better to say create a tunnel) from one source to destination.

In my case I want to use this functinality to open direct access to my computer in network shown below (ADSL modem connected by lan to the Vista computer; Vista publich AdHoc connection to the other computer Windows Xp. I setup service on Vista PC that helps me to open port directly for the Windows XP computer!!!):


In more simple way it's looks like this (configuration for testing):


How does it works?

In general I can describe it in terms of proxy class that take care to connect INPUT and OUTPUT sockets, transferring data between them and keeping in mind performance issues...


I create a simple windows service that has two threads: main and worker. In main thread I put code that helping us to control worker threads states: Running, Pause, Continue and Stop. In worker thread I put code that begin accepting of the incomming connections and restarting acceptance when its really needed.
Worker thread working with async sockets that hiddes from us that we are creating one more thread (I'll name it ASYNCSOCKETS in text below). That helps to deal with performance issues, due to non-blocking code executing everywhere. :) I recommend to look into code, because I don't want to put many technical  details here.

At the moment of accepting of incomming connection I create special CommunicationContext class that store information about incomming and outgoing sockets, buffers and communication state. You can use this context class for collecting and storing statistics information on sockets, control different states of communication and etc. - its depends on your needs.

Service now allowing to create up to 5 tunnels (this is hardcoded, but you can simply change that).

Issues

1) One from issues that I catch that with .NET sockets I have to deal a lot with exceptions handling. In Windows API sockets layers it was much simplier, you just have to check the state code after each API call. But at the end of the development I found a good point of using exceptions handling (look into NoDataSocketException class usage).

2) CommunicationContext destroying I give to do GC, but if you are developing high performance proxy server, then better to free resources by own hands then waiting for GC (buffers that used by sockets eating memory very fast!).

3) I try to use Disconnect logic for sockets, but it does not work as I expect that is why I Close sockets instead of Shutdowing and Diconnecting.

4) Proxy works only in one direction, I mean server/deestination host can not initialize connection, but you can easily repeat that logic based on code in archive.

Debugging technics

1) please make a look on files _switches.cs and _tracer.cs - this is helper classes that enhance .NET System.Diagnostics functionality and give us easy interface for tracing activitied of our code; in APP.CONFIG file you can find how to configure tracing in more advanced way.
2) debugging of the windows service is a little tricky part. Please make a look into service_debug.reg file. It force window OS to start debugger when we starting windows service. So to debug service you have to do several simple steps:
- run *.reg file for updating windows registry;
- compile service source code;
- open service control panel and select service in it. Press Start;
- in window that will ask you about debugging select existing instance on Visual Studio with opened source code.
- place break points in source code where you need them;
3) It's not always so easy to debug windows service that is why I implement additional functionality that should help in service debugging: command line options. Service understand several commands: -install, -uninstall, -help, -debug
DEBUG option force service to start in console mode! that is much easier to debug!!

References

At the moment when I mostly complete my code writting I found a good article in code project that demostrate mostly the same "business issue", but programming and architcture done in differen way. So I recommend to look into that code too if you are interesting in Proxy/Sockets programming: HERE

tcp_port_mapper_service.zip (70.71 kb)


Currently rated 5.0 by 4 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Comments

January 19. 2009 18:57

Este tipo de correo que contengan realmente apreciada y que puede dar idea y el conocimiento para hacerlo .. gracias por compartir este tipo de correo. best regards, busbys

Busby SEO Test

March 23. 2009 22:40

Grazie per la vostra consulenza. Prima di questo, non ha potuto riorientare porti sulla mia connessione ADSL.

Cash Loan

June 7. 2009 22:04

It's interesting, the blog engine platform seems very variable in form. My design skills are not so good as my C coding though, I would be interested in seeing what additional skins you can get for it. Nice blog btw, best wishes for it and keep up the posts. :) Kind regards, Peter sims.

Franchises for sale

September 22. 2009 14:16

Useful information like this one must be kept and maintained so I will put this one on my bookmark list! Thanks for this wonderful post and hoping to post more of this!

http://www.apuestasjuego.com/

September 30. 2009 16:53

Thanks! After you post I began to understanf at least something....

http://rapid4me.com

October 27. 2009 23:42

I always wanted to write in my site something like that but I guess you'r faster :)

online cash loans

November 23. 2009 19:44

Keep up the good work bro.Your article is really great and I truly enjoyed reading it.Waiting for some more great articles like this from you in the coming days.

proxy sites

December 2. 2009 17:34

Hmmm interesting stuff

payday loans

December 5. 2009 00:21

I'm not yet into the in-depths of TCP ports forwarding, tunneling, proxies, etc. But i have basic knowledge of these things. Thanks for giving us the idea on how to write a simple TCP proxy class that will help us to forward ports or create tunnel from one source to destination. I believe this post will help in many ways. Looking forward to more of our posts.

Philadelphia Tummy Tuck Surgeon

December 5. 2009 00:35

I'm not yet into the in-depths of TCP ports forwarding, tunneling, proxies, etc. But i have basic knowledge of these things. Thanks for giving us the idea on how to write a simple TCP proxy class that will help us to forward ports or create tunnel from one source to destination. I believe this post will help in many ways. Looking forward to more of our posts.

Philadelphia Tummy Tuck Surgeon

December 5. 2009 12:34

It's very true. There are very few people in this world who gives such tremendous views. I appreciate your work and hopping for some more informative posts.

Acai Berry

December 6. 2009 23:25

I like what I see. keep it going

payday loans

December 15. 2009 18:00

Great insights. I loved to read your article. You must be putting a lot of time into your blog!

anonymous proxy

December 17. 2009 21:13

Great share. Keep up the good work.

proxy

December 17. 2009 21:14

Great share. Keep up the good work.

proxy

December 18. 2009 09:20

hey your blog seems to be quite knowledgeable..i was searching for this information from some days and was glad to find it here..its really a nice work..

Acai Berry

December 19. 2009 00:24

<P>this truly assists, today i happen the problems and i donot know how to work out,
i research bing and found your blog,
thanks once again</P><P>one thing, can i post this entry on my blog? i will add the source.</P><P>regards!</P>

how to fix blue screen of death

December 21. 2009 22:04

This is a very nice post.

?????

December 22. 2009 11:57

Great blog, this could be the best blog I ever visited thi month. Never stop to write something useful dude!.

unblock myspace

December 24. 2009 10:40

I use to read your posts.They are highly informative and helpful.Thanks for giving us such quality knowledge.Waiting for your next interesting posts.

Jenny Craig

December 24. 2009 11:08

Thanks for the information. It is really nice of you. Please keep sharing such type of blogs in the future also.

Jenny Craig

December 24. 2009 13:09

Thanks for the information, it was really useful. I was looking for it and I'll be waiting for your next post.

Jenny Craig

December 24. 2009 13:34

I wish you a successful implementation of your idea and will be waiting for more interesting ideas from you. Thank you very much for sharing your ideas.

Blockbuster

December 24. 2009 13:38

Thanks for the valuable information. It was very useful for me. Keep sharing such ideas in the future as well.

Blockbuster

December 25. 2009 10:40

TCP would like to establish an alternate method of establishing unit shipment data to theEnergy Star program. Also of concern is the potential overlap of information that would be provided by TCP and our large retailers

sites de jeux de hasard gratuits

December 25. 2009 19:46

thanks for shared this with us.i found this informative and interesting blog so i think so its very useful and knowledge able.I would like to thank you for the efforts you have made in writing this article

Web Hosting

December 28. 2009 08:07

A very happy new years wish to all and thanks for making this blog avialable, back to solving sudoku now.

solving sudoku

December 29. 2009 12:57

I have been reading your posts regularly. I must say that you are doing a great job. Please keep up the good work.

Omaha Steaks

December 29. 2009 23:22

Do you make money out of this blog? just curious

KY Payday Loans

December 29. 2009 23:24

Just try to smile for about 2-3 mins then you can get back to work

OR Payday Loans

December 30. 2009 05:49

You are doing a wonderful job. You are providing very nice piece of information, I would love to know more about it.

Omaha Steaks

December 30. 2009 06:49

Thanks for the information. It is really nice of you. Please keep sharing such type of blogs in the future also.

Blockbuster

December 30. 2009 06:58

I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog posts. I will catch up your updates in future.

Acai

December 30. 2009 07:09

post was very informative and its look and feel is appreciable .. keep updating such articles.

Payday Loans

December 30. 2009 07:19

Intimately, the post is actually the best on this laudable topic. I harmonize with your conclusions and will eagerly look forward to your future updates. Saying thanks will not just be adequate, for the fantastic lucidity in your writing.

Acai

December 30. 2009 07:48

Thank you for this useful information.I will share it with my friends.

Gamefly

December 30. 2009 08:24

Very perfect and clear demonstration. Its is clear to everyone I think.

How to get Pregnant

December 30. 2009 08:34

I am very thankful to you. In fact your creative writing abilities has inspired me to start my own BlogEngine blog now. Really the blogging is spreading its wings rapidly. Keep up the good work.

Extenze

December 30. 2009 08:47

Looking forward to see your new post.Hope your next post will be more awesome and enrich's us.

Gamefly

December 30. 2009 08:47

What an informative post this is ! This is really very impressive. Keep posting such nice articles.

Payday Loans

January 4. 2010 07:57

I would like to thank you for the efforts you have made in writing this article. I am desiring the same best work from you in the future as well.

Omaha Steaks

January 6. 2010 08:56

Thank you very much. Really the blogging is spreading its wings rapidly. Keep up the good work.

Extenze

January 7. 2010 12:09

thanks for sharing...Found quite helpful so far

poor credit loans

January 8. 2010 08:34

Your tutorial provide a great knowledge about TCP proxy class.

Liposuction

January 8. 2010 20:51

While surfing Yahoo I clicked on your link...very informative! I enjoy staying up-to-date about this kind of info. I'll definitely bookmark your site for additional review.

Tourism Country Visited

January 14. 2010 09:27

Your post is amazing. your way of telling things is great.I would like to read more from you. Thanks for sharing information.

Extenze

January 15. 2010 11:00

You always pass failure on the way to success.

payday loans

January 16. 2010 06:34

Nice post, thanks for sharing this wonderful and useful information with us.

Green Tea

January 17. 2010 20:28

I learned that it is the weak who are cruel, and that gentleness is to be expected only from the strong.

cash advance

January 18. 2010 06:50

Your post is very informative..I was looking for such articles and would like to know in detail about this topic....You are doing great job..Keep sharing such nice articles with us..

Payday Loans

January 18. 2010 13:05

I really appreciate posts, which might be of very useful for beginners in blogging as I am. I already have a small collection of blog posts and other articles, from which I step by step learn various aspects of life. Thank you for your resource.

messenger online chat

January 20. 2010 10:10

I have surfed the net more than three hours today, yet I never found any interesting article like yours. It's worth enough for me. Thanks.

mma pound for pound ratings

January 21. 2010 11:22

well

friendly themes code structure

January 24. 2010 08:42

I totally don't get what you are talking about.

lezdom

January 24. 2010 17:39

I'm really impressed about the quality of the content, thanks and keep on like this!

Buenos Aires real estate

January 27. 2010 08:16

Thank you for this useful information.I will share it with my friends.I have done it bookmark.

colon cleanse

January 28. 2010 05:02

thank you so much for the valuable information!

how to backup xbox 360 games

February 1. 2010 18:50

TCP proxy can you tell me more about it?

ppo plans

February 8. 2010 13:04

Thanks for sharing such an interesting and informative post.

Teeth Whitening

February 11. 2010 09:22

You made some good points here. A very informative one I was looking for it. Keep up the good work and would like to hear more from you.

Extenze

February 11. 2010 09:28

I just bookmarked your blog and wanted to say that I have really enjoyed reading your blog. Thanks for sharing.

Colon Cleanse

February 16. 2010 22:41

Fantastic I hope you dont mind if I link to it from my wildfox couture vampire love website.

mapquest driving directions usa

February 17. 2010 00:58

Thanks for good information.

emekli

February 17. 2010 19:40

It's help me and all work.

driving directions usa

February 18. 2010 16:31

Thanks for good information.

cinsellik

February 19. 2010 00:26

i love to read articles that are informative and beneficial in nature.

ssk sorgulama

February 19. 2010 04:19

I am very thankful to you. In fact your creative writing abilities has inspired me to start my own BlogEngine blog now. Really the blogging is spreading its wings rapidly. Keep up the good work.

auto insurance

February 20. 2010 18:38

Awesome! Some really helpful information in there. Bookmarked. Excellent source.

online scheduler

February 21. 2010 03:06

I really enjoyed read your article, very interesting ...

ehliyet

February 21. 2010 10:59

The explanation is very good.

Lung Cancer

February 21. 2010 22:58

nice template and great article.thanks this is great information.

bagkur sorgulama

February 23. 2010 04:15

Your post is very useful. it make me understand about Tcp ports

hero marketing

February 23. 2010 16:59

nice template and great article.thanks this is great information.

bagkur

February 24. 2010 00:57

Excellent post.I want to thank you for this informative read, I really appreciate sharing this great post. Keep up your work.

zerrin egeliler

February 24. 2010 23:14

Fantastic I hope you dont mind if I link to it from my wildfox couture vampire love website.

driving directions usa

February 26. 2010 08:45

Mattresses Mattresses Mattresses Mattresses Mattresses Mattresses .

Beds

February 26. 2010 13:57

Thanks for taking the time to discuss this, I feel strongly about it and love learning more on this topic. If possible, as you gain expertise, would you mind updating your blog with more information? It is extremely helpful and beneficial to your readers.

MMA pound for pound

March 1. 2010 15:00

I feel strongly about it and love learning more on this topic. If possible, as you gain expertise, would you mind updating your blog with more information? It is extremely helpful and beneficial to your readers.

GHD MK4 IV Pure

March 2. 2010 12:04

nice template and great article.thanks this is great information. <a href="http://www.mark5inc.com";>teeth whitening</a>

teeth whitening

March 5. 2010 20:17

My design skills are not so good as my C coding though, I would be interested in seeing what additional skins you can get for it. Nice blog btw, best wishes for it and keep up the posts. :) Kind regards, Peter sims.

forex software system scam

March 6. 2010 11:08

In fact your creative writing abilities has inspired me to start my own BlogEngine blog now.

xbox 360 game copy software

March 8. 2010 13:17

Nice structure, thanks for all the above matter.

Colon Cleanse

March 9. 2010 08:47

It is very interesting post and very informative as well. I was not aware of this truth. Thanx a lot for sharing this post with us.

Colon Cleanse

March 9. 2010 16:38

Nice post, thanks for sharing this wonderful and useful information with us. <a href='http://natural-cure-for-yeast-infection1.blogspot.com' title='Natural Cure For Yeast Infection' target= '_blank' >Natural Cure For Yeast Infection</a>

12 hour yeast infection cure

March 10. 2010 09:14

I got the connection happening. However; the client doesnt seem to send back messages? I can send messages from Server to Client no problems. But from Client to Server, it just does NOTHING. HELP!

huntsville web design

March 10. 2010 11:17

Its the 1st time i am on your site and i would like to congratulate you for the big effort you are doing to write such excellent posts.

MMA pound for pound ratings

March 11. 2010 08:26

Some kind of interesting facts and I like it that's why I bookmarked it for future reference.

Jewish book

Add comment


(Will show your Gravatar icon)