views:

47

answers:

2

Hi

I want to write a simple client on the iPhone that downloads a file from an http server using only bsd sockets. I searched the web but couldn't find anything helpful. Can you give me a direction?

Thanks

Alex

A: 

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:

  1. Connect the socket to the server that you want to contact using connect.
  2. Transmit the request using the protocol that the server understands (FTP, HTTP, etc.)
  3. read the data returned by the server and save it to a local file
JSBangs
yeah I forgot to mention - it's http. Is there a code snippet/tutorial out there that can help me?
Alex1987
A: 

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)
fabrizioM
I already have a code for hello world client-server, but I was hoping to see a client downloads a file from http server. Thanks anyway
Alex1987
you didn't say that the server was an HTTP one :PThen you have to send a HTTP request with the socketand parse the HTTP header Response and see if is a multipart response or not and the encoding and quite a bit of http-compliant stuff. I think your best bet is to look into the source code of wget http://www.gnu.org/software/wget/ and see how is done.
fabrizioM