C
C#16mo ago
iskander

✅ TcpClient is connected to TcpListener but TcpListener isn't connected...

so this is how i accept clients which passes through with no issues. i've made to sure to catch any errors that could result from function and none is thrown...
public static async Task<bool> AcceptClient(CancellationToken Token)
{
if (!IsServer())
{
return false;
}
Server.Start();
TcpClient JoinedClient = await Server.AcceptTcpClientAsync(Token);
Server.Stop();

if (JoinedClient != null)
{
Clients.Add(JoinedClient);
return true;
}
else
{
return false;
}
}
public static async Task<bool> AcceptClient(CancellationToken Token)
{
if (!IsServer())
{
return false;
}
Server.Start();
TcpClient JoinedClient = await Server.AcceptTcpClientAsync(Token);
Server.Stop();

if (JoinedClient != null)
{
Clients.Add(JoinedClient);
return true;
}
else
{
return false;
}
}
this is how the client attempts to connect to the server. this result in a successful connection.
public static async Task<bool> JoinServer(string IpAddress)
{
Client = new TcpClient();

if (IPEndPoint.TryParse(IpAddress, out IPEndPoint Result))
{
Result.Port = Port;
await Client.ConnectAsync(Result);
return true;
}
else
{
Client = null;
return false;
}
}
public static async Task<bool> JoinServer(string IpAddress)
{
Client = new TcpClient();

if (IPEndPoint.TryParse(IpAddress, out IPEndPoint Result))
{
Result.Port = Port;
await Client.ConnectAsync(Result);
return true;
}
else
{
Client = null;
return false;
}
}
my pc is the server while my phone is the client. they're in the same network but i havent touched the firewall settings in my pc yet but i assume that wont cause this issue...
16 Replies
Kouhai
Kouhai16mo ago
AcceptClient returns false?
iskander
iskanderOP16mo ago
nope it returns a valid TcpClient
Kouhai
Kouhai16mo ago
Sorry so what's the problem then 😅 ?
iskander
iskanderOP16mo ago
TcpListen.server.conneced is false which prevents me from sending data to clients even an error is thrown when i try to use NetworkStream.write about socket not being connected tbf i should've took a screenshot when i saw that 😅
Kouhai
Kouhai16mo ago
Tcplistener.Server.Connected will return false because you do close the server here Server.Stop();
iskander
iskanderOP16mo ago
oh i thought that would only block any outside connection attempts while already accepted clients remain connected
Kouhai
Kouhai16mo ago
Afaik yes, already accepted clients will remain connected, you have to close them seperatly, but still Tcplistener.Server.Connected will return false when the listener server is stopped
iskander
iskanderOP16mo ago
hmmm ig that makes sense i'll test rq. can i ping you back when something comes up?
Kouhai
Kouhai16mo ago
Yea feel free to
iskander
iskanderOP16mo ago
and i doxxed my self...cool just a sec 😅
iskander
iskanderOP16mo ago
@.kouhai. here we go
iskander
iskanderOP16mo ago
so yeah connected is still false 🫤
Kouhai
Kouhai16mo ago
it seems like TcpListener.Server.Connected will always return false because Socket.Connected determins whether the socket is connected to remote host or not, but the actual listener isn't connected to the client. The TcpClient returned from AcceptTcpClientAsync should be checked instead of the TcpListener itself
iskander
iskanderOP16mo ago
hmmm but dont you need both sockets to be connected to send information? oh yeah that makes sense! the tcplisten cant be connected to it's own socket yep that's probably it! thanks a lot kouhai 😅
Kouhai
Kouhai16mo ago
Np ThumbsUpSmile
iskander
iskanderOP16mo ago
i just checked and the remote client is indeed connected. i appreciate your help 🙏
Want results from more Discord servers?
Add your server