sochix / TLSharp

Telegram client library implemented in C#
1k stars 380 forks source link

app crash at create "new TelegramClient" object #859

Closed ram-sh closed 5 years ago

ram-sh commented 5 years ago

Hello,

using TLSharp 0.1.0.390

app crash at create new TelegramClient object :(

Unhandled Exception: System.Net.Sockets.SocketException: The requested address is not valid in its context at System.Net.Sockets.Socket.DoBind(EndPoint endPointSnapshot, SocketAddress socketAddress) at System.Net.Sockets.Socket.Bind(EndPoint localEP) at System.Net.Sockets.TcpClient..ctor(IPEndPoint localEP) at TLSharp.Core.Network.TcpTransport..ctor(String address, Int32 port, TcpClientConnectionHandler handler) at TLSharp.Core.TelegramClient..ctor(Int32 apiId, String apiHash, ISessionStore store, String sessionUserId, TcpClientConnectionHandler handler)

what is it i'm missing here?

thank you in advance!!

emreordukaya commented 5 years ago

I get the same exception. I was using working code. but todey I get this exception.

CloneDeath commented 5 years ago

Are you able to get what IP Address it's trying to use? What does Socket.OSSupportsIPv6 return? What OS are you running on?

I suspect it's IPv6, and your computer doesn't support it. Answering the above question may shed some light on that theory.

emreordukaya commented 5 years ago
public TcpTransport(string address, int port, TcpClientConnectionHandler handler = null)
 {
   if (handler == null)
   {
       var ipAddress = IPAddress.Parse(address);
       var endpoint = new IPEndPoint(ipAddress, port);

       _tcpClient = new TcpClient(endpoint);
       _tcpClient.Connect(endpoint);
   }
   else
   _tcpClient = handler(address, port);
 }

I get an ip adress like 149.134.145.100:443 I m using win10.

the exception is in TcpTransport.cs file and in this line _tcpClient = new TcpClient(endpoint);

Ekran Alıntısı

ram-sh commented 5 years ago

is it just us? everyone else doesn't have this error?

knocte commented 5 years ago

@eordukaya can you be more specific? do you get 149.134.145.100:443 in the address variable? including the colon and the port?

emreordukaya commented 5 years ago

after this statement I get exception var endpoint = new IPEndPoint(ipAddress, port); I looked endpoint and variable is 149.134.145.100:443

knocte commented 5 years ago

@eordukaya I didn't ask you to look at ipAddress variable but to address variable.

Anyway, seems correct. Can you revert commit 3224113e4ce827319c049f44ee5a759ad1591fa0 and tell us if it connects successfully to the same IP address?

emreordukaya commented 5 years ago

I update this commit but get exception again in this line. Ekran Alıntısı

knocte commented 5 years ago

That's not a proper revert, you're creating a TcpClient instance twice. Do git revert 3224113e4ce827319c049f44ee5a759ad1591fa0 instead of doing it manually.

emreordukaya commented 5 years ago

Ok I understand. I download fixed TLSharp with 3224113e4ce827319c049f44ee5a759ad1591fa0 . But I get exception again. I added my debug photos.

1

2

3

4

1111

2222

3333

44444

knocte commented 5 years ago

Man, you're not reverting the commit I'm telling you. If you see a variable endpoint in the code, then it means you're not testing what I'm telling you. You're just testing latest master.

byguid commented 5 years ago

@knocte , had the same problem, reverting your suggested commit 3224113e4ce827319c049f44ee5a759ad1591fa0 fixed the issue.

knocte commented 5 years ago

@byguid and what's the IP address that you connected and tried to connect (when not reverting)? Same?

byguid commented 5 years ago

@knocte, yes, the address was the same in both cases.

emreordukaya commented 5 years ago

@ram-sh @knocte I reverted code block in edited file (for IPv6) TcpTransport.cs I deleted this block;

if (handler == null) { var ipAddress = IPAddress.Parse(address); var endpoint = new IPEndPoint(ipAddress, port); _tcpClient = new TcpClient(endpoint); _tcpClient.Connect(endpoint); } else _tcpClient = handler(address, port);

I added this block instead of before

if (handler == null) { _tcpClient = new TcpClient(); var ipAddress = IPAddress.Parse(address); _tcpClient.Connect(ipAddress, port); } else _tcpClient = handler(address, port);

Then it works.

knocte commented 5 years ago

@CloneDeath so apparently your commit broke this, did you test it?

emreordukaya commented 5 years ago

@CloneDeath @knocte if commit works on ipV6, can be done; if ipv6 use new else use old then you can new commit.

knocte commented 5 years ago

But @CloneDeath's patch was supposed to work with both, we checked in the MSDN documentation.

emreordukaya commented 5 years ago

ok, I reported that old version works but new doesnt. @CloneDeath can check again. thanks for everything.

ram-sh commented 5 years ago

@eordukaya solution worked for me as well! thanks much!!

knocte commented 5 years ago

This issue is duplicate of https://github.com/sochix/TLSharp/issues/862 , which has just been fixed.