Thread: Help with telnet and tcp

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    9

    Unhappy Help with telnet and tcp

    Hi,
    I have been given an application that when running outputs its results through a simple char buffer into a local telnet session. I need to use these results in an application I am developing, but I don't understand how I retrieve them.

    I have built a tcp client which creates a socket, binds it to the local port used by the telnet application, and then connects to the telnet session. It seems to do this fine, so far so good. My client then loops constantly using the recv() method:

    Code:
    int main(){
    
    	WSADATA wsaData;
    	struct sockaddr_in clientService;
    	iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
    
    	system("cls");
    
    	//create SOCKET for connecting to server
    	ConnectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    	if(ConnectSocket == INVALID_SOCKET){
            printf("ERROR Invalid Socket: %ld\n", WSAGetLastError());
            WSACleanup();
            return 1;
    	}
    	printf("CLIENT: Socket created...\n");
    
    	//bind port: address family, IP address, and port
    	clientService.sin_family = AF_INET;
    	clientService.sin_addr.s_addr = inet_addr("127.0.0.1");
    	clientService.sin_port = htons(51966);
    	printf("CLIENT: Port bound...\n");
    
    	//connect to server
    	iResult = connect( ConnectSocket, (SOCKADDR*) &clientService, sizeof(clientService) );
    	if(iResult == SOCKET_ERROR){
    		closesocket (ConnectSocket);
    		printf("ERROR Cannot Connect: %ld\n", WSAGetLastError());
    		WSACleanup();
    		return 1;
    	}
    
    	printf("CLIENT: Connected to localhost...\n");
    
    	//receive messages
    	while(TRUE){
    		iResult = recv(ConnectSocket,recvbuf,recvbuflen,0);
    		if(iResult > 0){
    			//printf("Bytes received: %d\n", iResult);
    			printf("Recieved: %s\n", &recvbuf);
    		}	
    		else if(iResult == 0){
    			printf("Connection closed.\n");
    		}
    		else{
    			printf("ERROR Message Lost: %d\n", WSAGetLastError());
    		}
    	}
    My problem is that it never recieves anything . I built a server app to test the send() method and my client recieves a char buffer just fine, but I think the problem is the app I am connecting to only sends a buffer to the telnet, and not to my client. Is there anyway to make my client recieve the telnet messages, without reprogramming the original application?

    Cheers for taking the time to read all this!

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> I have been given an application that when running outputs its results through a simple char buffer into a local telnet session
    Are you saying that this app makes an active connection to a telnet server, and then outputs data? Or the app is a telnet server, which you can telnet into and see data being spit out?

    Describe how you actually use this application you've been given.

    gg

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    9
    the application i have been given uses webcams to obtain co-ordinate data from frame to frame transitions. i thought i knew how it worked until you asked that question- i know for a fact the application sends data to a telnet session. i also know the only way i can see that information is to connect to a telnet port on the localhost, for example:

    telnet localhost 51966

    when i do that i can see the information appearing in real time as the webcams capture it. from looking at the applications code i can see that it creates sockets, but i wouldnt recognise how it would create a telnet session (i'm quite new to C/C++), what sort of code am i looking for? it certainly at the very least connects to the port i have to connect to in order to see the telnet output, 51966.

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Looks like it implements a server.

    When you telnet in, do you have to supply a login and password?

    gg

  5. #5
    Registered User
    Join Date
    Mar 2008
    Posts
    9
    no not at all, i just type in that telnet command and im in

  6. #6
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Well, the good news is that you probably don't have to implement a TELNET protocol. Sounds like a simple socket client should be able to connect and read the data.

    So - back to your posted code. What we'll need is a more complete posting - preferably, something that compiles.

    I'm no network programming guru - so hopefully someone who is will jump in and fix you up

    In the meantime, I can point you to tutorials that may point out where you've gone wrong.
    http://beej.us/guide/bgnet/output/ht...age/index.html

    gg

  7. #7
    Registered User
    Join Date
    Mar 2008
    Posts
    9
    Hi, I've figured out a lot from before. As it turns out, the telnet client is just getting in the way, and blocking the port I needed to listen to. So after not logging into telnet, I made some improvements to my code, and here should be a compilable version:

    Code:
    #include <winsock2.h>
    #include <stdio.h>
    
    #define DEFAULT_BUFLEN 100
    #define DEFAULT_PORT "0xCAFE"
    
    int main(){
    
    	char recvbuf[DEFAULT_BUFLEN];
    	int recvbuflen = DEFAULT_BUFLEN;
    	
    	int iResult;
    	SOCKET ConnectSocket;
    	WSADATA wsaData;
    	struct sockaddr_in clientService;
    	iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
    	memset(recvbuf, 0, DEFAULT_BUFLEN);
    	system("cls");
    
    	//create SOCKET for connecting to server
    	ConnectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    	if(ConnectSocket == INVALID_SOCKET){
            printf("ERROR Invalid Socket: &#37;ld\n", WSAGetLastError());
            WSACleanup();
            return 1;
    	}
    	printf("CLIENT: Socket created...\n");
    
    	//bind port: address family, IP address, and port
    	clientService.sin_family = AF_INET;
    	clientService.sin_addr.s_addr = inet_addr("127.0.0.1");
    	clientService.sin_port = htons(0xCAFE);
    	printf("CLIENT: Port bound...\n");
    
    	//connect to server
    	iResult = connect( ConnectSocket, (SOCKADDR*) &clientService, sizeof(clientService) );
    	if(iResult == SOCKET_ERROR){
    		closesocket (ConnectSocket);
    		printf("ERROR Cannot Connect: %ld\n", WSAGetLastError());
    		WSACleanup();
    		return 1;
    	}
    
    	printf("CLIENT: Connected to Firefly 2D...\n");
    
    	//receive messages
    	while(TRUE){
    		iResult = recv(ConnectSocket,recvbuf,recvbuflen,0);
    		if(iResult > 0){
    			printf("Bytes received: %d\n", iResult);
    			printf("Recieved: %s\n", &recvbuf);
    		}	
    		else if(iResult == 0){
    			printf("Connection closed.\n");
    		}
    		else{
    			printf("ERROR Message Lost: %d\n", WSAGetLastError());
    		}
    		//printf("Reached end!");
    	}
    
    	//shutdown the connection since no more data will be sent
    	iResult = shutdown(ConnectSocket,SD_SEND);
    	if(iResult == SOCKET_ERROR){
    		printf("ERROR Closing Connection: %d\n", WSAGetLastError());
    		closesocket(ConnectSocket);
    		WSACleanup();
    		return 1;
    	}
    
    	//cleanup
    	closesocket(ConnectSocket);
    	WSACleanup();
    	return 0;
    }
    You need to add Ws2_32.lib as an Additional Dependancy under the Linker options in VS2005 to compile it. This code mostly works, it recieves the data I want, the only issue is, it recieves lots of it! In an infinite loop, as shown in this screenshot:

    http://b.imagehost.org/view/0221/output.jpg

    Does anyone know why? The sending code definitely only sends the message shown in the screenshot once.

    Edit: the code will simply quit the cmd window if that port does not exist
    Last edited by hoAx; 03-08-2008 at 10:17 AM. Reason: missed something

  8. #8
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> printf("Recieved: &#37;s\n", &recvbuf);
    Two problems here. First, you don't need the "&". Second, "%s" prints null-terminated string. But recv() doesn't null-terminate anything. It just copies bytes and tells you how many.

    If you know you'll always be receiving text, then you can null terminate the string yourself. First, you'll need set recvbuflen to DEFAULT_BUFLEN - 1, to ensure there is room for the null. Then for a positive iResult, you can simply do "recvbuf[iResult] = 0". Then you'll have a null-terminated string suitable for "%s".

    recv() returns 0, a positive int, or SOCKET_ERROR. On SOCKET_ERROR, you may want to "break" out of your while loop.

    gg

  9. #9
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    or use
    Code:
    printf("Recieved: &#37;*.*s\n", iResult ,iResult ,recvbuf);
    So you do not need that extra byte
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  10. #10
    Registered User
    Join Date
    Mar 2008
    Posts
    9
    Thank you both it works just fine now! Much appreciated!

  11. #11
    Registered User t3chn0n3rd's Avatar
    Join Date
    Dec 2007
    Location
    kansas city
    Posts
    25
    that is an interesting problem

  12. #12
    Registered User
    Join Date
    Mar 2008
    Location
    Algeria
    Posts
    1
    Quote Originally Posted by hoAx View Post
    Thank you both it works just fine now! Much appreciated!
    Hi hoaAX/ All, Sorry but i'm trying to compile your programme using Builder C++ version 5, i got error saying :[C++ Erreur] Unit1.cpp(63): E2467 '_fastcall TForm1::Button1Click(TObject *)' ne peut pas renvoyer une valeur( can't return a value).

    and when i discomment all the returns ( // return 1; ..... etc ) i get nothing ( only my Form1); and after replacing printf by Showmessage, i get first : Client : "socket created ..." then "client port bound" and then nothing. its an error

    i need to established a telnet session using suh as application could you help doing this please !

    Thanks a lot!

Popular pages Recent additions subscribe to a feed