开发者

debug TcpClient and TcpListener on the same machine

开发者 https://www.devze.com 2023-04-06 14:58 出处:网络
New to network programming here. I have a two piece application. I am trying to debug it locally. Service listens for connections on new 开发者_如何学编程IPEndPoint(IPAddress.Any, 3000).

New to network programming here.

I have a two piece application. I am trying to debug it locally.

Service listens for connections on new 开发者_如何学编程IPEndPoint(IPAddress.Any, 3000).

Calling tcpClient.Connect(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 3000)) throws No connection could be made because the target machine actively refused it 127.0.0.1:3000. Windows firewall is off.

Am I doing something fundamentally stupid?


you should call the Start method on the TcpListener to get it working, or it will not accept any connection.

I have tested and this snippet works :)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            int port = 5124;

            var myListener = new TcpListener(new IPEndPoint(IPAddress.Any, port));
            myListener.Start();

            var tcpClient = new TcpClient();
            tcpClient.Connect(new IPEndPoint(IPAddress.Parse("127.0.0.1"), port));

            tcpClient.Close();

        }
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消