Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include <stdio.h>
00028 #include <string.h>
00029 #include <stdlib.h>
00030 #include <curl/curl.h>
00031
00032 #ifdef WIN32
00033 #include <windows.h>
00034 #include <winsock2.h>
00035 #include <ws2tcpip.h>
00036 #define close closesocket
00037 #else
00038 #include <sys/types.h>
00039 #include <sys/socket.h>
00040 #include <netinet/in.h>
00041 #include <arpa/inet.h>
00042 #include <unistd.h>
00043 #endif
00044
00045 #include <errno.h>
00046
00047
00048 #define IPADDR "127.0.0.1"
00049 #define PORTNUM 80
00050
00051 #ifndef INADDR_NONE
00052 #define INADDR_NONE 0xffffffff
00053 #endif
00054
00055 static size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream)
00056 {
00057 size_t written = fwrite(ptr, size, nmemb, (FILE *)stream);
00058 return written;
00059 }
00060
00061 static curl_socket_t opensocket(void *clientp,
00062 curlsocktype purpose,
00063 struct curl_sockaddr *address)
00064 {
00065 curl_socket_t sockfd;
00066 (void)purpose;
00067 (void)address;
00068 sockfd = *(curl_socket_t *)clientp;
00069
00070
00071 return sockfd;
00072 }
00073
00074 static int sockopt_callback(void *clientp, curl_socket_t curlfd,
00075 curlsocktype purpose)
00076 {
00077 (void)clientp;
00078 (void)curlfd;
00079 (void)purpose;
00080
00081 return CURL_SOCKOPT_ALREADY_CONNECTED;
00082 }
00083
00084 int main(void)
00085 {
00086 CURL *curl;
00087 CURLcode res;
00088 struct sockaddr_in servaddr;
00089 curl_socket_t sockfd;
00090
00091 #ifdef WIN32
00092 WSADATA wsaData;
00093 int initwsa = WSAStartup(MAKEWORD(2, 0), &wsaData);
00094 if(initwsa != 0) {
00095 printf("WSAStartup failed: %d\n", initwsa);
00096 return 1;
00097 }
00098 #endif
00099
00100 curl = curl_easy_init();
00101 if(curl) {
00102
00103
00104
00105
00106 curl_easy_setopt(curl, CURLOPT_URL, "http://99.99.99.99:9999");
00107
00108
00109 sockfd = socket(AF_INET, SOCK_STREAM, 0);
00110 if(sockfd == CURL_SOCKET_BAD) {
00111 printf("Error creating listening socket.\n");
00112 return 3;
00113 }
00114
00115 memset(&servaddr, 0, sizeof(servaddr));
00116 servaddr.sin_family = AF_INET;
00117 servaddr.sin_port = htons(PORTNUM);
00118
00119 servaddr.sin_addr.s_addr = inet_addr(IPADDR);
00120 if(INADDR_NONE == servaddr.sin_addr.s_addr)
00121 return 2;
00122
00123 if(connect(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr)) ==
00124 -1) {
00125 close(sockfd);
00126 printf("client error: connect: %s\n", strerror(errno));
00127 return 1;
00128 }
00129
00130
00131 curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1L);
00132
00133
00134 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
00135
00136
00137 curl_easy_setopt(curl, CURLOPT_OPENSOCKETFUNCTION, opensocket);
00138 curl_easy_setopt(curl, CURLOPT_OPENSOCKETDATA, &sockfd);
00139
00140
00141 curl_easy_setopt(curl, CURLOPT_SOCKOPTFUNCTION, sockopt_callback);
00142
00143 curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
00144
00145 res = curl_easy_perform(curl);
00146
00147 curl_easy_cleanup(curl);
00148
00149 if(res) {
00150 printf("libcurl error: %d\n", res);
00151 return 4;
00152 }
00153 }
00154 return 0;
00155 }