send a CTRL-C over a network - c++

I want to replicate a condition where in if I did a CTRL-C after I connecting to a server using telnet, the server would crash. I want to write a C++ program which does it. What does telnet send over the network when we do a CTRL-C . I read that CTRL-C is '0x03'. Does it send the same thing or does it send something else and how should I send it using a C++ application.

Telnet really does send 0x03. Telnet is just a regular TCP connection with some escape codes that do various things.
But to test a server over telnet, you probably really want to use the "Expect" utility: http://en.wikipedia.org/wiki/Expect

Related

nc -L disconnect after transmission

I'm currently preparing a unit test and therefore I need to emulate a client server connection.
I tried doing it like this:
nc -l 6543 < dummy-result.txt
But netcat does not close the connection after returning the content of the file. So my client is waiting for the server to close the connection endlessly.
Does anyone know how to get ncat to close the connection after serving the file? Also it would be useful to have ncat serve this file (and close the connection) for multiple requests (aka. -k).
Actually this was a bug in the openbsd implementation of netcat.
The option -N (server side) did not terminate the connection if the client was another netcat. If the client was telnet, it terminated as expected (Now I'm using the nmap netcat and it works).
For handling multiple connections I used a simple "while true" loop around it.

boost asio notify server of disconnect

I was wondering if there is any way to notify a server if a client side application was closed. Normally, if I Ctrl+C my client side terminal an EOF-signal is sent to the server side. The server side async_read function has a handle which has boost::system::error_code ec argument fed into it. The handle is called when the server side receives EOF-signal which I can happily process and tell the server to start listening again.
However, if I try to cleanly close my client application using socket.shutdown() and socket.close() nothing happens and the server side socket remains open.
I was wondering, is there a way to somehow send an error signal to the server-side socket so I could then process it using the error code?
The approaches described in comments covers 99% of cases. It doesn't work when client machine was (not gracefully) turned off, or network problems.
To get reliable notification of disconnected client you need to implement "ping" feature: to send ping packets regularly and to check that you received pong packets.

Send Close File Signal to FTP Server

I am implementing FTP Client in C++ using Windows Sockets. I have successfully connected to Server on Port 21, and transmitted the file in PASV mode using "STOR Sample.txt" command on the data port. The problem is that I am unable to tell the server about transfer completion (I want to send the signal to close data connection) so that i can receive the "226 Transfer OK" from the server on my control connection.
Further, I am not receiving anything from the server via recv(). I think that is because server is still listening on the data connection.
Have a look at this:
proper user of STOR command
You get back the port to connect to when you send PASV and then you tell the server where to store the data using STOR and the you connect to the port the PASV command returned and send the data - when you are done you close this second socket and continue sending commands with the original one.

client socket sends data but server socket does not receive them. c++ buffered stream?

I am working on a project where a partner provides a service as socket server. And I write client sockets to communicate with it. The communication is two way: I send a request to server and then receive a response from server.
The problem is that I send the data to the server but apparently the server cannot receive the data.
From my side I just use very simple implementation just like the example from http://www.linuxhowtos.org/C_C++/socket.htm
#include <sys/socket.h>
socket_connect();
construct_request_data();
send(socket, request_data, request_length, 0/*flag*/); // I set flag as 0
// now the server should receive my request and send response to me
recv(socket, response_data, response_length, 0);
socket_close();
And it seems that the server socket is implemented with a "binding" to std::iostream and it is buffered stream. (i.e. the socket send/recv is done in iostream::write/read.)
server_socket_io >> receive_data;
server_socket_io << response_data;
Btw, I got a test client from my partner and it is wrapped in a iostream as well. The test socket client can communicate with the server without problem, but it must do iostream::flush() after every socket send.
But I want to just keep it simple not to wrap my socket client in iostream.
I just wonder whether the buffered iostream results in the problem: the data is not processed since the data the client socket sent is just in very small amount and still buffered.
Or could it be my problem? how can I know if I really send out the data? does my client socket also buffer the data?
I have tried some "bad" workaround with TCP_NODELAY but it didn't help!
How can I solve the problem? from client side? or server side?
Should I close the socket after sending request and before receiving response, so that the data will be "flushed" and processed?
or should I wrap my socket in iostream and do flush?
or the server socket should use a "unbuffered" stream?
thanks for any suggestion and advice!
Further to Jay's answer, you can try any network packet sniffer and check whether your packets are getting to the server or not. Have a look at wireshark or tcpdump.
Let's use "divide and conquer" to solve the problem.
First, does the server work?
From your code look up the port number that your server is listening on.
Start your server program.
Run the following command line program to see if the server is really listening:
netstat -an -p tcp
It will produce a list of connections. You should see a connection on your selected port when the server is running. Stop the server and run the command again to ensure the port is no longer in use.
Once you've verified the server is listening try to connect to it using the following command:
telnet your-server-address-here your-port-number-here
telnet will print what your server sends to you on the screen and send what you type back to the sever.
This should give you some good clues.
I had a similar issue once before. My problem was that I never 'accepted' a connection (TCP) on the server inorder to create the stream between server/client. After I accepted the connection on the server side, everything worked as designed.
You should check the firewall settings for both systems. They may not be passing along your data.

Communicating over telnet

I can't use Boost ASIO (reasons not related to programming) and I looked around and libcurl looks good. I'm building the binaries at the moment and decided to get some feedback from the community.
Any comments regarding libcurl/alternatives?
UPDATE:
libcurl's not great for telnet. It's 2010! Shouldn't there be a simple way to do this? Winsock?
Telnet is a very simple protocol. Some wonky stuff to negotiate the terminal type but I'm sure your robot doesn't care where the cursor ends up. Just use a socket to open a TCP/IP connection on port 23 and send the command strings, terminated with a '\n'.
RFC854 is referring to it as a protocol, so perhaps it is one.
When I think of telnet I think of connecting to port 23 on a VT100 to get a terminal window to a remote UNIX host. You can also use telnet to other ports to get a TCP/IP connection which we used to use years ago on MUD/Talker servers, but this is simply a regular TCP/IP connection that is used in a connection-based client-server application. Actually "connectionless" is a misnomer as you do connect to the remote server, just in the connectionless model you do not retain the connection throughout the session, whereas in a connection-based model, the session begins when the client connects and ends when it disconnects.