client server sockets and file transfer - sockets

I have a client serever application,
My server accepts connections from more than one clients.
After a client is connected to server it sends command to the server and the sever sends replies
the replies are either strings or files.
On the server side after accepting connection,
there is a socket (seperate from listening socket) which is responsible for communication with client.
On the client side after a client sends a command to server, I start reading for the response on the same socket.
Now my problem is with files,
client sends a command to server asking for file, the server starts responding by sending binary data of file, if file is all good it transfers fine,
But if on the server side in the middle of file transfer the server gets a read problem, it has no way to infrom that problem to client, because this is a one to one socket communication... the client will treat any incoming data as if it is a file data untill the file size sent in the start is not complete.,
I am sure this could be a recurring pattern how to can I resolve this ?

FTP does this by having two connections: a command connection and a data connection.

As long as these are TCP/IP sockets, all you need is an agreement between the server and the client that the first eight bytes(for example) sent() and recv()ed, respectively, represents the size of the binary data to follow. TCP/IP will make sure that all the pieces arrive and are in order for you. If you have a variety of files that could be transferred, then you agree that the next four bytes after that represent characters for the file type. So you basically keep recv()ing until you have 12 bytes, which will probably take only one recv(). Then keep using recv() until you have all the bytes you expected to receive.

Related

Sending and receiving data over Internet

This question is not for a concrete implementation of how this is done. It is more about the concept and design of sending information over Internet with some kind of protocol - either TCP or UDP. I know only that sockets are needed, but I am wondering about the rest. For example after a connection is made and you send the information through that, but how does the other end listen for a specific port and does it listen constantly?
Is listening done in a background thread waiting for information to be received? (In order to be able to do other things/processing while waiting for information)
So in essence, I think a real world example of how such an application works on a high level would be enough to explain the data flow. For example sending files in Skype or something similar.
P.S. Most other questions on similar topics are about a concrete implementation or a bug that someone has.
What I currently do in an application is the following using POSIX sockets with the TCP Protocol:
Most important thing is: The most function are blocking functions. So when you tell your server to wait for client connection, the function will block until a connection is established (if you need a server that handles multiple clients at once, you need to use threading!)
Server listens for specific port until a client connects. After the connect, you will get a new socket file descriptor to communicate with the client whilst the initial socket can listen to new connections. My server then creats a new thread to handle that client whilst waiting for new connections on the initial socket. In the new thread the server waits for a request command from the Client (e.g. Request Login Token). After a request was received by the server, the server will gather its informations, packs it together using Googles Protocol Buffers and sends it to the client. The client now either tells the server to terminate the session (if every data is received by the client that it needs) or send another request.
Thats basically the idea in my server. The bigger problem is the way you transmit and receive data. E.g. you cant send structs or classes (at least not via C++) over the wire, you need some kind of serializer and you have to make sure the other part knows how much to receive. So what i do is, first send a 4byte integer over the wire containing the size of the incomming package, then send the package itself using a serializer (in my case Googles Protocol buffers). The other side waits for 4 byte to be available, knowing that this will be the size of the incomming package. After 4 bytes are received, the program waits for exact that amount of data being available on the socket, when available, read the data out of the buffer and deserialize it. When the socket is not receiving data for 30 seconds, trigger a timeout and terminate the connection.
What you always need to be aware of is the endianess of the systems. E.g. a big endian system (e.g. PowerPC) and a little endian system (e.g. x86) will have problems when you send an integer directly over the wire. For example a
0001
on the x86, is a
1000
on the Power PC, thus making a 8 out of a 1. So you should always use functions like ntohl, an htonl, which will convert data from and to host byte order from and to network byte order (network byte order is always big endian).
Hope this kind of helps. I could also provide some code to you if that would help.

Transferring files over a network: Send from client or from server?

I am presently working on a client-server solution to transfer files to another machine via a socket network connection. I am fairly new to the whole client-server thing and therefore have the following - admittedly very basic - question:
For the file transfer, does it make any difference if I am sending the file from a client to a server or from a server to a client?
Any qualified insight into this will be much appreciated!
For the file transfer, does it make any difference if I am sending the file from a client to a server or from a server to a client?
Basically, no it does not matter. Once you have the connection made you are free to send data in both directions. Although you have to consider that a server won't accept data that is sent to it unless it explicitly reads from the socket.
To be a bit more general, server and client are completely arbitrary for a home brewed implementation of data transfer. If you boil this down to the simplest concept then you are just opening a socket and writing data to it on one side, and on the other side you are reading from a different socket.
You might choose to implement a single client program capable of connecting other clients (P2P) and sending files back and forth. In that case you could call the "server" the program that is currently sending the file, and the "client" is the program currently receiving.
Alternatively, you could implement two programs, one for client and one for server. Your server will listen for connections and the client will decide when it wants to connect to the server.
Remember that there are network limitations for connecting. If the program that is listening for connections is behind a firewall then you have to be sure you are forwarding the correct ports. If you are connecting machines within a LAN then you probably have nothing to worry about.

Sending file on separate connection

I have a server program that spawns a thread for every incoming connection. This thread then handles the request by receiving it and sending a response. For some kinds of connections I have to respond first with a file and then with a text response.
The problem is that, if I send the textual response after sending the file, the response gets written inside the file, because the client has no way of knowing where the file ends and where the response beings. So I need to close the connection after sending the file and then send a response on other connection or, alternatively, send the file on a separate connection and then send the response on the current connection. How can I accomplish this?
Use the technique that FTP uses to keep the data connection separate from the control connection. The server starts listening on an ephemeral port -- the OS will assign an unused port to it. It sends this port number to the client on the main connection. The client then connects to the ephemeral port, and the server sends the file on this new connection.
If you need to deal with multiple sockets concurrently, you can use select() or epoll() to wait for data on either of them.

is it possible for http server to respond http client on same connection fd each time?

I want to write iterative HTTP server code that accepts one HTTP Client on the same conn_fd (file descriptor) every time, but for different clients it should create new_fd, based on checking the client address. Is the possible?
I'm not sure I understand your question, but this is basically how sockets works: you create a master socket and set it to a listening state. Then, everytime you accept a new client, a new socket is created for that client, while the master socket remains the same.
For a nice intro about Unix sockets, see http://beej.us/guide/bgnet/
Each new connection will result in a new socket. So if the same client connects multiple times it will be a new socket (and file descriptor), but if it connects one time and sends multiple requests over the same connection (HTTP keep alive) it will be the same fd.

Raw sockets and addressing

I need to write a ftp program linking two, only two, computers, but this must be done using raw sockets on promiscuous mode. Since the socket will receive any data that come across the network device, my problem is how to address the correct program. For example: Imagine I have a server on a machine and two clients on the other. The server sends a message, how can the two clients know which of them will receive the message? Another problematic situation would be if I open a client and a server in the same machine, both may start to commune with each other.