|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Winsock Question... Pretty Simple
I have a quick question. I am fairly new using the winsock API and I was curious how I would go about downloading a file off some specific server. Lets say I was given the exact URL of the file. How would I go about downloading that file?
I already specified the port, and the server that it is located using: ::... Assume Winsock has already been initialized...:: // Store information about the server LPHOSTENT hostEntry; hostEntry = gethostbyname("www.microsoft.com"); // Create the socket SOCKET theSocket; theSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); SOCKADDR_IN serverInfo; // Create new Socket Object serverInfo.sin_family = AF_INET; // Specify TCP protocol serverInfo.sin_addr = *((LPIN_ADDR)*hostEntry->h_addr_list); serverInfo.sin_port = htons(80); // Specify Port // Connect to the server nret = connect( theSocket, (LPSOCKADDR)&serverInfo, sizeof(struct sockaddr)); ::.. okay, I am now connected to microsoft using port 80..:: As a test, lets say I wanted to download their startup page... "index.html" How would I go about doing that? Thanks for the help -Jon |
|
#2
|
|||
|
|||
|
Not sure from your posting, why you are creating a socket at port 80 for file transfering.
You can create/connect a socket with standard port FTP (port 21), and issue GET/PUT for file transfering! |
|
#3
|
|||
|
|||
|
I wanted to use port 80 to emulate an http client. I am able to start downloading the default file ( "index.html ? index.htm ") using this:
::.. Recieving Module ..:: ofstream outfile; outfile.open("c:\\temp.html"); char aa[1000]; strcpy(aa,"GET / \r\n"); strcat(aa,"HTTP 1.0 \r\n\r\n"); send(theSocket,aa,sizeof(aa),0); int temp = 1; while( temp != 0){ nret = recv(theSocket, aa, 1000, 0); WriteToFile(aa); // Copies the aa string into the file } outfile.close(); ::.. .. but for some reason, it will keep recieving the bit stream and never stop. The file will continuously growing till 400+ megs, and If I check the contents of the file, the html code has been copied many times. Is that because of my while loop condition? Thanks -Jon |
![]() |
| Viewing: Dev Articles Community Forums > Programming > General Programming Help > Winsock Question... Pretty Simple |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|