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:
-
using System;
-
using System.Collections.Generic;
-
using System.Linq;
-
using System.Text;
-
using System.Net.Sockets;
-
using System.Threading;
-
using System.IO;
-
using System.Net;
-
-
-
namespace TCP_Client
-
{
-
class TCPServer
-
{
-
private TcpListener tcpListener;
-
private Thread listenThread;
-
-
//Int32 port = 3000;
-
-
-
public TCPServer()
-
{
-
this.tcpListener = new TcpListener(IPAddress.Any,3000);
-
this.listenThread = new Thread(new ThreadStart(ListenForClient));
-
}
-
-
private void ListenForClient()
-
{
-
this.tcpListener.Start();
-
-
while (true)
-
{
-
TcpClient client = this.tcpListener.AcceptTcpClient();
-
-
Thread clientThread = new Thread(new ParameterizedThreadStart(HandleClientComm));
-
clientThread.Start(client);
-
}
-
}
-
-
public void SendData(TcpClient client, String message)
-
{
-
NetworkStream clientStream = client.GetStream();
-
ASCIIEncoding encoder = new ASCIIEncoding();
-
-
byte[] buffer = encoder.GetBytes(message);
-
clientStream.Write(buffer, 0, buffer.Length);
-
clientStream.Flush();
-
}
-
-
-
private void HandleClientComm(object client)
-
{
-
TcpClient tcpClient = (TcpClient)client;
-
NetworkStream clientStream = tcpClient.GetStream();
-
-
byte[] message = new byte[4096];
-
int bytesRead;
-
-
while (true)
-
{
-
bytesRead = 0;
-
-
try
-
{
-
//blocks until a client sends a message
-
bytesRead = clientStream.Read(message, 0, 4096);
-
}
-
catch
-
{
-
//a socket error has occured
-
break;
-
}
-
-
if (bytesRead == 0)
-
{
-
//the client has disconnected from the server
-
break;
-
}
-
-
//message has successfully been received
-
ASCIIEncoding encoder = new ASCIIEncoding();
-
System.Diagnostics.Debug.WriteLine(encoder.GetString(message, 0, bytesRead));
-
}
-
-
tcpClient.Close();
-
}
-
}
-
}
-
-
My client is in the same program:
-
namespace TCP_Client
-
{
-
public partial class Form1 : Form
-
{
-
-
string CommandText;
-
-
-
public Form1()
-
{
-
InitializeComponent();
-
// start up ur server
-
//TCPServer server = new TCPServer();
-
-
-
// Set up client to connect to server
-
TcpClient client = new TcpClient();
-
-
-
IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse("121.218.51.204"), 3000);
-
client.Connect(serverEndPoint);
-
-
NetworkStream clientStream = client.GetStream();
-
ASCIIEncoding encoder = new ASCIIEncoding();
-
byte[] buffer = encoder.GetBytes("Hello Server");
-
clientStream.Write(buffer,0,buffer.Length);
-
clientStream.Flush();
-
-
}
-
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.