Watching Socket Port

The below code is used to listen the socket signal by watching the port.

Namespace part

using System;
using System.Net;
using System.Text;
using System.Net.Sockets;

Code Part

namespace MySocket
public class SocketListener
static void Main(string[] args)
TcpListener MyTCPListener = new TcpListener(8085);

TcpClient MyTCPClient = MyTCPListener.AcceptTcpClient();
NetworkStream MyNetworkStream = MyTCPClient.GetStream();
String MyResponse = "Received Data";
Byte[] ResponseBytes = Encoding.ASCII.GetBytes(MyResponse);

MyNetStream.Write(ResponseBytes, 0, ResponseBytes.Length);

Code Explanation

1. Create instance of TcpListener on the particular port
2. Start listening on the port.
3. Once data comes, accept the data and read it
4. Make it reply on the port
5. Stop monitoring the socket port



Author: Chandan10 May 2010 Member Level: Bronze   Points : 0

* This is a strictly moderated forum. All resources submitted are manually reviewed by our editors.
* Do not post *work from home*, *make money from home* etc kind of messages. They will not appear in the site.
* Only the content which maintain a very high quality will be eligible for revenue sharing program.
* Please do spell check before you submit.

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: