Question

Im trying to build a small compact telnet tool therefor I've decided to tackle the sending without waiting for response part first

the problem is that no matter which guide I use I just cant make it work

what am I missing here ??

public void SendTelnetCommand(string Command , string IPofAP)
{
    IPEndPoint endpoint = new IPEndPoint(IPAddress.Parse(IPofAP), 23);
    TcpClient tcpSocket;
    tcpSocket = new TcpClient(endpoint);

    if (!tcpSocket.Connected) return;
    byte[] buf = System.Text.ASCIIEncoding.ASCII.GetBytes(Command);
    tcpSocket.GetStream().Write(buf, 0, buf.Length);

    if (tcpSocket.Connected) tcpSocket.Close();
}

while debuging I get An unhandled exception of type 'System.Net.Sockets.SocketException' in System.dll

Was it helpful?

Solution

What's the message of the exception? Any inner exceptions? Are you using the correct IP address (IPv4 or IPv6?)? Also, you have to read from the stream as well.

However, your issue is most likely using the wrong TcpClient constructor. The one that takes the endpoint is a listener, not a client. You have to use the hostname + port overload.

That is, try this:

public void SendTelnetCommand(string Command, string IPofAP)
{
    TcpClient tcpSocket = new TcpClient(IPofAP, 23);

    if (!tcpSocket.Connected) return;
    byte[] buf = System.Text.ASCIIEncoding.ASCII.GetBytes(Command);
    tcpSocket.GetStream().Write(buf, 0, buf.Length);

    if (tcpSocket.Connected) tcpSocket.Close();
}

You can also use the IPEndPoint to connect to a server, however, you have to use the parameter-less constructor, and call tcpSocket.Connect(endpoint);

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top