telnet client in C - Programmer

This is a discussion on telnet client in C - Programmer ; What is the problem of this code? It compile, it run, but it print strange characters and then it waits. I don't understandhow I develop a telnet client in c (I compile with MS Visual c++). Help me. Excuse my ...

+ Reply to Thread
Results 1 to 3 of 3

Thread: telnet client in C

  1. telnet client in C

    What is the problem of this code? It compile, it run, but it print strange
    characters and then it waits. I don't understandhow I develop a telnet
    client in c (I compile with MS Visual c++). Help me. Excuse my english.
    Thanks.#include
    #include "winsock2.h"

    void main() {

    // Initialize Winsock.
    WSADATA wsaData;
    int iResult = WSAStartup( MAKEWORD(2,2), &wsaData );
    if ( iResult != NO_ERROR )
    printf("Error at WSAStartup()\n");

    // Create a socket.
    SOCKET m_socket;
    m_socket = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );

    if ( m_socket == INVALID_SOCKET ) {
    printf( "Error at socket(): %ld\n", WSAGetLastError() );
    WSACleanup();
    return;
    }

    // Connect to a server.
    sockaddr_in clientService;

    clientService.sin_family = AF_INET;
    clientService.sin_addr.s_addr = inet_addr("192.168.0.7");
    clientService.sin_port = htons(23);

    if ( connect( m_socket, (SOCKADDR*) &clientService,
    sizeof(clientService) ) == SOCKET_ERROR) {
    printf( "Failed to connect.\n" );
    WSACleanup();
    return;
    }

    // Send and receive data.
    int bytesSent;
    int bytesRecv = SOCKET_ERROR;
    char sendbuf[32] = "Client: Sending data.";
    char recvbuf[255] = "";


    while( true ) {
    bytesRecv = recv( m_socket, recvbuf, sizeof(recvbuf), 0 );
    if ( bytesRecv == 0 || bytesRecv == WSAECONNRESET ) {
    printf( "Connection Closed.\n");
    break;
    }
    if (bytesRecv < 0)
    return;
    printf( "Bytes: %ld\n", bytesRecv );
    printf( "Message: %s\n", recvbuf);

    }

    return;
    }



  2. Re: telnet client in C

    domsh wrote:

    > What is the problem of this code? It compile, it run, but it print strange
    > characters and then it waits. I don't understandhow I develop a telnet
    > client in c (I compile with MS Visual c++). Help me. Excuse my english.
    > Thanks.#include
    > #include "winsock2.h"
    >
    > void main() {
    >
    > // Initialize Winsock.
    > WSADATA wsaData;
    > int iResult = WSAStartup( MAKEWORD(2,2), &wsaData );
    > if ( iResult != NO_ERROR )
    > printf("Error at WSAStartup()\n");
    >
    > // Create a socket.
    > SOCKET m_socket;
    > m_socket = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
    >
    > if ( m_socket == INVALID_SOCKET ) {
    > printf( "Error at socket(): %ld\n", WSAGetLastError() );
    > WSACleanup();
    > return;
    > }
    >
    > // Connect to a server.
    > sockaddr_in clientService;
    >
    > clientService.sin_family = AF_INET;
    > clientService.sin_addr.s_addr = inet_addr("192.168.0.7");
    > clientService.sin_port = htons(23);
    >
    > if ( connect( m_socket, (SOCKADDR*) &clientService,
    > sizeof(clientService) ) == SOCKET_ERROR) {
    > printf( "Failed to connect.\n" );
    > WSACleanup();
    > return;
    > }
    >
    > // Send and receive data.
    > int bytesSent;
    > int bytesRecv = SOCKET_ERROR;
    > char sendbuf[32] = "Client: Sending data.";
    > char recvbuf[255] = "";
    >
    >
    > while( true ) {
    > bytesRecv = recv( m_socket, recvbuf, sizeof(recvbuf), 0 );
    > if ( bytesRecv == 0 || bytesRecv == WSAECONNRESET ) {


    recv() never returns WSAECONNRESET. If it returns -1, WSAGetLastError()
    may return WSAECONNRESET, you can check for that condition in your next
    if statement.

    > printf( "Connection Closed.\n");
    > break;
    > }
    > if (bytesRecv < 0)
    > return;
    > printf( "Bytes: %ld\n", bytesRecv );


    Ok.

    > printf( "Message: %s\n", recvbuf);


    Wrong. If you want to use printf() with the %s format you have to have a
    null terminated string, and recv() does not null terminate. Before your
    printf() call, add a manual termination, like:
    recvbuf[bytesRecv] = 0;
    ....but then you have to change sizeof(recvbuf) to sizeof(recvbuf)-1 in
    the parameter to recv() above so that you save some space for the null
    character.

    >
    > }
    >
    > return;
    > }
    >
    >



    --
    Olof Lagerkvist
    ICQ: 724451
    Web page: http://here.is/olof


  3. Re: telnet client in C

    In addition to the problem with using printf to print a possibly non-null
    terminated recvbuf[], telnet is a protocol, and most telnet servers will
    send "commands" to the telnet client and sometimes expect responses back.
    These start with "IAC" (Interpret As Command), which is a byte value of 255.
    Search the web for the telnet RFC.


    "domsh" wrote in message
    news:L_ZBc.11675$GQ3.314846@twister2.libero.it...
    > What is the problem of this code? It compile, it run, but it print strange
    > characters and then it waits. I don't understandhow I develop a telnet
    > client in c (I compile with MS Visual c++). Help me. Excuse my english.
    > Thanks.#include
    > #include "winsock2.h"
    >
    > void main() {
    >
    > // Initialize Winsock.
    > WSADATA wsaData;
    > int iResult = WSAStartup( MAKEWORD(2,2), &wsaData );
    > if ( iResult != NO_ERROR )
    > printf("Error at WSAStartup()\n");
    >
    > // Create a socket.
    > SOCKET m_socket;
    > m_socket = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
    >
    > if ( m_socket == INVALID_SOCKET ) {
    > printf( "Error at socket(): %ld\n", WSAGetLastError() );
    > WSACleanup();
    > return;
    > }
    >
    > // Connect to a server.
    > sockaddr_in clientService;
    >
    > clientService.sin_family = AF_INET;
    > clientService.sin_addr.s_addr = inet_addr("192.168.0.7");
    > clientService.sin_port = htons(23);
    >
    > if ( connect( m_socket, (SOCKADDR*) &clientService,
    > sizeof(clientService) ) == SOCKET_ERROR) {
    > printf( "Failed to connect.\n" );
    > WSACleanup();
    > return;
    > }
    >
    > // Send and receive data.
    > int bytesSent;
    > int bytesRecv = SOCKET_ERROR;
    > char sendbuf[32] = "Client: Sending data.";
    > char recvbuf[255] = "";
    >
    >
    > while( true ) {
    > bytesRecv = recv( m_socket, recvbuf, sizeof(recvbuf), 0 );
    > if ( bytesRecv == 0 || bytesRecv == WSAECONNRESET ) {
    > printf( "Connection Closed.\n");
    > break;
    > }
    > if (bytesRecv < 0)
    > return;
    > printf( "Bytes: %ld\n", bytesRecv );
    > printf( "Message: %s\n", recvbuf);
    >
    > }
    >
    > return;
    > }
    >
    >




+ Reply to Thread