Simone
Simone

Reputation: 2430

Script that open telnet connection and send message via socket to a server

I must do a script that run a connection telnet and send to a server two numbers. I am sure that server work because if I send values directly from telnet everything works correctly. If I run the script that do exactly what I do something does not work. I can see client has been connected, but I don't see on screen the numbers that should be had sent via telnet.

Here the script (really simple):

{ echo "3"; echo "6"; sleep 1; } | telnet localhost 8887

and part of the server :

server.sin_family = AF_INET;
server.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
server.sin_port = htons(8887);

...
//bind and listen
...

printf("connesso\n");

//Riceve un emssaggio dal client
for(i=0; i<2;) {

    bzero(client_message,2000);

    if((read_size = recv(client_sock , client_message , 2000 , 0)) > 0)
    {
        //Rimanda indietro il messaggio al client
        write(client_sock , client_message , strlen(client_message));

        numeri[i]=atoi(client_message);
        i++;            
    }
}

...
// some check
...

if(numeri[1] % numeri[0] == 0) {
    printf("multiplo\n");
}
else {
    printf("negativo\n");
}

So, I should see on server some messages:

  1. "Connesso"
  2. "multiplo" or "negativo"

But i see just "Connesso"... It's like i do not receive nothing.. Obviously i see "connesso" after I run my script.

Any idea?

Thanx

Upvotes: 3

Views: 2509

Answers (1)

GMaster
GMaster

Reputation: 1521

This code will work

{ echo "3"; echo "6"; sleep 1; } | telnet localhost 8887

How I tested it? Install netcat. Then from one terminal

nc -l 8887

And from another terminal type

{ echo "3"; echo "6"; sleep 1; } | telnet localhost 8887

You will see the number 3 and 6 in the first terminal running nc

Upvotes: 2

Related Questions