1

I am trying to build a program that will connect to an IP address (preferably that of a router) to a specific port (mainly 80) and will try to authenticate and then go on with further actions.

I started without knowing how to communicate with the router/server so i did this:

while (tcpSocket.Available > 0)
{
  int input = tcpSocket.GetStream().ReadByte();

But it always gets a tcpSocket.Available = 0 So then i found out that i have to send a specific cmd for it to talk to me. http://msdn.microsoft.com/en-us/library/cc247846.aspx

and made this

var client = new TcpClient(ip, port);
var data = Encoding.GetEncoding(1252).GetBytes(cmd);
var stm = client.GetStream();
stm.Write(data, 0, data.Length);

Now I dont understand how to format the cmds the cmd based on this http://www.ietf.org/rfc/rfc2941.txt Would be 37 - 1?

Thank you for reading P.S dont know if i should point this to SuperUser or ServerFault

czioutas
  • 1,032
  • 2
  • 11
  • 37
  • Why would you reinvent the wheel? There is already a [library](http://stackoverflow.com/questions/390188/c-sharp-telnet-library) for it. – Alex Butenko Apr 28 '13 at 18:26
  • have you used the library? the first piece of code is actually from there and also i have a problem with how to format the cmds and which ones to use which a library any library wont cover – czioutas Apr 28 '13 at 18:28
  • No, never used. When Brian answered, I better understand your question. – Alex Butenko Apr 28 '13 at 18:44

1 Answers1

2

I think you need to go back to simpler questions and investigations.

First: What protocol is actually running on the server you are connecting to? Port 80 suggests it is HTTP (port 80 is typically reserved for HTTP). Telnet typically runs on port 23.
If it is HTTP you need to follow the protocol defined in RFC 2616 (with the authentication options defined in RFC 2617).

Even simpler yet: connect to the server using PuTTY (or other preferred telnet client). What do you need to do in order to log in? If it is a telnet server then it will probably show a banner followed by a login prompt. You will type the username followed by return, then it will show you a password prompt. If it is a HTTP server then it will probably show you nothing at all, but type HTTP/1.0 (return) HEAD / (return) and you should see a HTTP message response. Whatever you need to do using PuTTY, your program will need to do exactly the same thing.

Brian O''Byrne
  • 550
  • 2
  • 10
  • well i actually trying to communicate with a router so i thought that a basic tcp connection would work in order to authenticate me. so what i wanted to do is mainly the same thing as when you go in the browser and type in the routers ip and it prompts you for a username and pass. – czioutas Apr 28 '13 at 18:38
  • If the normal client is a web browser then the server is using HTTP. Some routers will use HTTP Basic Authentication as defined in RFC 2617, so that is what your client will need to do. Some routers will use a custom form-based authentication, so you will need to work with that. In any case since it is HTTP there are good libraries (including the System.Net namespace in the .NET framework) that do all the simple stuff including basic authentication without you having to use sockets at all. – Brian O''Byrne Apr 28 '13 at 18:42