How To Send A File In Winsock (http Way) - c

I mean, if i have a winsock in window environmentand i set it to listen on port 80 and ip of server.then i have a file abc.txt or abc.xml .i have to send file from client to http server through http link. how can we send it .I don't know how to send text files.
Please..I need this...so badly.
Thanks in advance!

Setting up a server basically requires the following steps:
Open socket with socket()
Bind socket to an address (usually INADDR_ANY) and a specific port with bind()
Listen to the socket with listen()
Accept a connection with accept(). This returns a new socket number (the client socket)
Now you can receive data from the client socket with recv() and send data with send().
To send a file to the client, just read from the file for example line by line and then send it to the socket with send().
Of course you need to communicate the file name and size before you start sending. (Or use EOF character at the end of file).
For more information, see:
msdn: Getting Started with Winsock

Related

Socket, how to find local address when binded with INADDR_ANY

I'm recoding a simple ftp server and I'm stuck of the implemetation of the PASV command.
In fact, when the client send a PASV command, I have to create a new server socket and send back the infos (address and port) to the client so that he can connect to it to create the data connection.
What I do step by step is:
Create a server socket with INADDR_ANY and random port (port 0)
listen() with this socket
getsockname() this socket to get it's infos
send back the infos to the client on the command connection
the accept() is done later, when the user need to use the data con
(Is that the right way to do it ?)
The thing is, the server socket that I create on the server side is binded with INADDR_ANY so getsockname() on it always return 0.0.0.0 (cause it is binded to virtually all the ips off the system).
Is that case, what address should I send back to the client and how to find it ?
How real ftp servers handle this ?
Thanks :)
What I do step by step is:
Create a server socket with INADDR_ANY and random port (port 0)
listen() with this socket
getsockname() this socket to get it's infos
send back the infos to the client on the command connection
the accept() is done later, when the user need to use the data con
(Is that the right way to do it ?)
In general, yes, however steps #1 and #3 tend to be more complicated than that.
For one thing, getsockname() cannot get the true IP until the socket is actually connected to someone (which, in this case, means you would have to call getsockname() on the socket returned by accept(), not on the socket that you call accept() on). So, when binding to INADDR_ANY, you should just report the IP of the interface that the command socket is connected to. In which case, it is better to just bind the listening socket to only that interface directly and not to INAADDR_ANY at all.
For another thing, even if the server machine only has 1 interface installed, if the server is behind a NAT router, and the client is outside the NAT, then you would have to report the router's public IP instead of your server's listening IP. You would have to know the router's public IP ahead of time, either by storing it in your app's configuration, or by dynamically querying the router itself via uPNP, or by querying an outside service like http://iplookup.flashfxp.com/. Unless the router is FTP-aware (some are) and is smart enough to replace the reported IP for you when passing through the router, in which case you do have to report your listening IP instead.
You should call getsockname() on the command socket. That gives you the IP address the client used to connect to you. It can be different for each client on a multi-homed host. If you're behind a NAT device you should use its public IP address, which you will have to obtain via configuration.

FTP implementation: close data socket every time

I'm implementing in c a sort of FTP protocol.
I' ve a server running.
I start the client, connect to the server, and then send a GET file.txt request.
The client parse the command, see it's a GET command and starts a server socket.
The server recieves the command, and starts the data connection with the client and start sending file.txt on this connection.
When the server sent the file, it closes the client socket (Data).
When i want to GET another file, the port is already in use. How can i prevent this? Should i keep the data-connection open for all the command-connection session? In this case, how can my client know when the file is over?
Thanks
When a socket is closed, it enters the TIME WAIT state (see here for the possible TCP states) and no other socket can be bound to the same address/port pair until the socket leaves TIME WAIT and is in the CLOSED state.
You can go around this by setting the SO_REUSEADDR socket option, that will allow two sockets to be bound to the same address if one of the sockets is in the TIME WAIT state.
you need to open socket for transfer each time as the server will close it when transfer finish.
you will know that the file is downloaded/uploaded by reading response from FTP Server for status code (226 or 250) - check List of FTP server return codes:
https://en.wikipedia.org/wiki/List_of_FTP_server_return_codes
In my project, I use apache-commons-net,
just keep the command connection alive with heartbeat command,
and enter local passive mode every time to do file tranfer.
The principle is same for your situation, I suggest send
EPSV
command before GETTING a file.txt.
refer: https://commons.apache.org/proper/commons-net/

UDP - Multithreaded server is unable to distinguish between an ack msg and a connection msg

I am trying to implement a multithreaded UDP server where each thread services one client.
So far, the clients are being registered correctly and the data is being received by the clients. In order to make it reliable, i tried sending a negative acknowledgement when the message was not received within a particular time interval.
This acknowledgement is being treated as a request from a new client in the server side recvfrom() function. How do i distinguish the two at the server side? Are there any functions related to send and receive that can do the same?
The code is in c and i have used pthreads to implement the threading function.I have used the basic sendto() and recvfrom() socket functions to implement the same.
Code outline:
At the server side:
recvfrom()
when received:
Add to client list
create thread:
send data in the thread function
exit thread
At the client side:
sendto() -> to initiate the request to the server
when time < timeout
recvfrom() -> receive the data from the server
when timeout occurs
sendto() -> send negative acknowledgement.
recvfrom() will fill out a socket address structure of the correct type with the address of the client.
You could maintain a data structure that maps client addresses (including the port number) to threads. You can then use this to pass subsequent messages from the client to the correct thread.

raw_socket communication using udp protocol

Am just beginner of this socket programming ...
i tried to make a UDP client_server program using SOCK_RAW .I can send message from the server part.But in client part am getting garbage values. And the receiving message packet size also different.Can U help me to do this client server program using SOCK_RAW.
I tried this client -server with eth0 & eht1,i tried to send 1 packet from eth0 to eth1 sending side is showing "OK" message.. but received data is garbage..
before i done normal client server program using UDP protocol.I got correct output from normal udp client server .When i tried with SOCK_ROW its showing error in sending part.
Make your life simpler and use SOCK_DGRAM - Kernel will take care of datagram headers, you only need to provide valid IP address and data. It is user's responsibility to append (before transmitting) and parse (upon reception) UDP headers using SOCK_RAW.
Most likely you are seeing IP and UDP headers and calling them "garbage". That is what SOCK_ROW does. You should parse and skip protocol headers if you want to get to your message content while using SOCK_ROW.

Forwarding UDP packets in C using the socket API

I am writing a content filter in C using the socket API that will intercept DNS requests, and only return the response if the domain is allowed. The pseudocode to describe this is:
Redirect all DNS queries to the content filter program which is listening on UDP port X.
Content filter program extracts domain being queried and determines if it is allowed or not.
If it is allowed, then the content filter program forwards the original DNS request packet to the original destination DNS server while maintaining the original source and IP+port so that the DNS server can send the reply directly back to the client.
If the domain is not allowed, then no reply is sent.
I currently have the program listening on UDP port X but the problem is that I can't access the IP headers, and therefore can't simply forward the DNS request to the original server while maintaining the original headers.
I have tried using socket(AF_INET, SOCK_RAW, IPPROTO_UDP) but that doesn't bind on port X (understandably), and doesn't receive any traffic.
What is the best way to go about listening on UDP port X, while still being able to access the IP headers of incoming packets?
I think recvfrom on an UDP socket should give you the correct source address. You still probably need a raw socket for forwarding the message.
The functionality for SOCK_RAW based sockets varies depending on the platform you are on. Generally, when you want to get access to the full IP datagram information, then I would recommend using the Berkeley Packet Filter to tap the data-link layer frames addressed to UDP port of interest.

Resources