download file only with sockets
I want to write a simple client on the iPhone that downloads a file from an http server using on开发者_JS百科ly bsd sockets. I searched the web but couldn't find anything helpful. Can you give me a direction?
Thanks
Alex
This depends on the nature of the server giving you the file. It could be FTP, HTTP, a network file-share, or even something like Gopher or scp. In any case, the basic nature of the problem will be the same:
- Connect the socket to the server that you want to contact using
connect
. - Transmit the request using the protocol that the server understands (FTP, HTTP, etc.)
read
the data returned by the server and save it to a local file
An example from http://docs.python.org/library/socket.html
The conversion to C and the handling of the file is left as an exercise to the reader.
# Echo server program
import socket
HOST = '' # Symbolic name meaning all available interfaces
PORT = 50007 # Arbitrary non-privileged port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(1)
conn, addr = s.accept()
print 'Connected by', addr
while 1:
data = conn.recv(1024)
if not data: break
conn.send(data)
conn.close()
# Echo client program
import socket
HOST = 'daring.cwi.nl' # The remote host
PORT = 50007 # The same port as used by the server
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
s.send('Hello, world')
data = s.recv(1024)
s.close()
print 'Received', repr(data)
精彩评论