I am currently trying to read in messages from a port and for some reason I am able to do this using a TCPClient however I am recieving an error when trying to use a TCPListener. I was wondering if anyone could tell me why, or where I may be going wrong?
This is the code which works, and I am able to read messages from:
public static NetworkStream nwStream;
public static TcpClient client = new TcpClient();
public const string address = "10.10.10.151";
public const int port = 9004; //both never change
public MainWindow()
{
InitializeComponent();
client.Connect(address, port);
connected = true;
if (connected == true)
{
readInTxtBox.Text = "Connected";
}
}
And this is the code which throws an error:
static void Main(string[] args)
{
IPAddress ipAddress = IPAddress.Parse("10.10.10.151");
TcpListener Listener = new TcpListener(ipAddress, 9004);
Listener.Start(); //this is where the error is!
Socket Client = Listener.AcceptSocket();
using (NetworkStream PMS_NS = new NetworkStream(Client))
{
StreamReader sr = new StreamReader(PMS_NS);
Console.WriteLine(sr.ReadLine());
}
Console.ReadLine();
}
The error says:
An unhandled exception of type 'System.Net.Sockets.SocketException' occurred in System.dll Additional information: The requested address is not valid in its contexts