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);