constantly reading and writing to a tcp server
continually sending commands and writing commands to a tcp server.
is the following code faulty:
after connection was successful:
Code:
while(1) {
ret = write(sock, CMD, sizeof(CMD));
printf("sent: %d: %s\n", ret, CMD);
ret = read(sock, buff, sizeof(buff));
if (ret > 0) {
fprintf(stdout, "recv: %d: %s\n", ret, buff);
}
[ ... do something with buff ... ]
memset(buff, 0, MAXBUFFER-1);
sleep(1);
}
what seems to happen is that the server receives the first CMD fine but after
the first time it seems not to receive it or it gets corrupted somehow.
I've made sure with tcpdump that the packet arrives and the CMD also arrives
at the server so I'm not sure what could be wrong.
in anyway, if this is not the way to continually process returned values based on
commands sent to the server, I'd be happy if you can correct me on that.
thanks.
continous client read/write
That was the client code.
The server is basically a netcat tcp server which spawns a bash script:
Code:
#!/bin/bash
while read cmd
do
echo $cmd
done
again, what happens is that at the first attempt the c client sends the command,
and is receiving it back fine but after that it receives nothing, as if it didn't send
the command or it got messed up but with a sniffer I can see that it doesn't.
to rule out possibilities I started netcat in plain mode tcp server and connected
to it with the c client and that resulted in a normal "telnet" session where both
server/client read and write just fine.
that seems to mean that the problem is with the bash script but if I use netcat
as a client to connect to netcat as the tcp server spawning the bash script it
also works fine.
I'm assuming the problem lies with my piece of code which is as you said assuming
a whole lot of things but I would guess that for the simplest tasks it would just
"work".