Connecting Tech Pros Worldwide Help | Site Map

Connecting via TCP/IP in C#

Member
 
Join Date: Sep 2007
Posts: 52
#1: 3 Weeks Ago
Hi Guys,

I am trying to learn how to use TCP/IP with C#. When I try to run my code, I get this error

No connection could be made because the target machine actively refused it 121.218.51.204:3000



My code for TCP server is:

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Net.Sockets;
  6. using System.Threading;
  7. using System.IO;
  8. using System.Net;
  9.  
  10.  
  11. namespace TCP_Client
  12. {
  13.     class TCPServer
  14.     {
  15.         private TcpListener tcpListener;
  16.         private Thread listenThread;
  17.  
  18.         //Int32 port = 3000;
  19.  
  20.  
  21.         public TCPServer()
  22.         {
  23.             this.tcpListener = new TcpListener(IPAddress.Any,3000);
  24.             this.listenThread = new Thread(new ThreadStart(ListenForClient));
  25.         }
  26.  
  27.         private void ListenForClient()
  28.         {
  29.             this.tcpListener.Start();
  30.  
  31.             while (true)
  32.             {
  33.                 TcpClient client = this.tcpListener.AcceptTcpClient();
  34.  
  35.                 Thread clientThread = new Thread(new ParameterizedThreadStart(HandleClientComm));
  36.                 clientThread.Start(client);
  37.             }
  38.         }
  39.  
  40.         public void SendData(TcpClient client, String message)
  41.         {
  42.             NetworkStream clientStream = client.GetStream();
  43.             ASCIIEncoding encoder = new ASCIIEncoding();
  44.  
  45.             byte[] buffer = encoder.GetBytes(message);
  46.             clientStream.Write(buffer, 0, buffer.Length);
  47.             clientStream.Flush();
  48.         }
  49.  
  50.  
  51.         private void HandleClientComm(object client)
  52.         {
  53.             TcpClient tcpClient = (TcpClient)client;
  54.             NetworkStream clientStream = tcpClient.GetStream();
  55.  
  56.             byte[] message = new byte[4096];
  57.             int bytesRead;
  58.  
  59.             while (true)
  60.             {
  61.                 bytesRead = 0;
  62.  
  63.                 try
  64.                 {
  65.                     //blocks until a client sends a message
  66.                     bytesRead = clientStream.Read(message, 0, 4096);
  67.                 }
  68.                 catch
  69.                 {
  70.                     //a socket error has occured
  71.                     break;
  72.                 }
  73.  
  74.                 if (bytesRead == 0)
  75.                 {
  76.                     //the client has disconnected from the server
  77.                     break;
  78.                 }
  79.  
  80.                 //message has successfully been received
  81.                 ASCIIEncoding encoder = new ASCIIEncoding();
  82.                 System.Diagnostics.Debug.WriteLine(encoder.GetString(message, 0, bytesRead));     
  83.             }
  84.  
  85.             tcpClient.Close();
  86.         }
  87.     }
  88. }
  89.  
  90.  
My client is in the same program:

Expand|Select|Wrap|Line Numbers
  1. namespace TCP_Client
  2. {
  3.     public partial class Form1 : Form
  4.     {
  5.  
  6.         string CommandText;
  7.  
  8.  
  9.         public Form1()
  10.         {
  11.             InitializeComponent();
  12.             // start up ur server
  13.             //TCPServer server = new TCPServer();
  14.  
  15.  
  16.             // Set up client to connect to server
  17.             TcpClient client = new TcpClient();
  18.  
  19.  
  20.             IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse("121.218.51.204"), 3000);
  21.             client.Connect(serverEndPoint);
  22.  
  23.             NetworkStream clientStream = client.GetStream();
  24.             ASCIIEncoding encoder = new ASCIIEncoding();
  25.             byte[] buffer = encoder.GetBytes("Hello Server");
  26.             clientStream.Write(buffer,0,buffer.Length);
  27.             clientStream.Flush();
  28.  
  29.         }
  30.  
I get an error on line 21 of the second code. Basically I am running them on the same application using my own IP address. I dont know if this is the right way to go on about it of if I am missing something. I am totally new to the language and the whole concept of TCP/IP.

I did turn off my firewall but to no avail.

The code borrows heavily from this tutorial: http://www.switchonthecode.com/tutor...ded-tcp-server

Many thanks for looking at it.
Expert
 
Join Date: Jun 2008
Location: Pretoria, South Africa
Posts: 410
#2: 3 Weeks Ago

re: Connecting via TCP/IP in C#


I had a similar problem though it was with a web service running via WCF, so I don't know if this will be of use to you:

The issue was that our anti-virus (NOD 32) was blocking access to the port even after we told the firewall to allow access to the port. We fixed this by configuring the anti-virus to never scan/block our application or the Visual Studio DevEnv executable.
Reply