#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/ip.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <unistd.h>
#include <string.h>
int main(int argc,char *argv[])
{
int sockfd,new_socket
int sock_value
char buf[] = "hello! China!I Love You\n"
struct sockaddr_in client_
struct sockaddr_in server_
int SIZE = sizeof(struct sockaddr_in)
if(argc != 2){
fprintf(stderr,"The two number!\n")
exit(1)
}
if((sock_value = atoi(argv[1])) <0){
fprintf(stderr,"socket error!\n")
exit(1)
}
if((sockfd = socket(PF_INET,SOCK_STREAM, 0)) == -1){
perror("socket")
exit(1)
}
bzero(&server_,SIZE)
server_.sin_family = PF_INET
server_.sin_port = htons(sock_value)
server_.sin_addr.s_addr = INADDR_ANY
if(bind(sockfd,(struct sockaddr *)(&server_),SIZE) == -1){
perror("bind")
exit(1)
}
if(listen(sockfd, 12) == -1){
perror("listen")
exit(1)
}
printf("Waiting ... ...\n")
while(1){
if((new_socket = accept(sockfd,(struct sockaddr *)(&client_),&SIZE)) == -1){
perror("accept")
exit(1)
}
printf("The client IP is %s\n",inet_ntoa(client_.sin_addr))
printf("The socket is %d\n",ntohs(client_.sin_port))
if(write(new_socket,buf,strlen(buf)) == -1){
perror("write")
exit(1)
}
int my
char mybuf[1024]
if((my = read(new_socket, mybuf,1024)) == -1){
perror("read")
exit(1)
}
mybuf[my] = '\0'
printf("#++++#++++#:%s\n",mybuf)
close(new_socket)
}
close(sockfd)
return 0
}
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/ip.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <unistd.h>
int main(int argc,char *argv[])
{
int sockfd
int sock_value
char buf[1024]
char mybuf[] = "Linux\n"
int read_count
struct sockaddr_in client_
struct sockaddr_in server_
int SIZE = sizeof(struct sockaddr_in)
if(argc != 3){
fprintf(stderr,"The two number!\n")
exit(1)
}
if((sock_value = atoi(argv[2])) <0){
fprintf(stderr,"socket error!\n")
exit(1)
}
if((sockfd = socket(PF_INET,SOCK_STREAM, 0)) == -1){
perror("socket")
exit(1)
}
bzero(&client_,SIZE)
bzero(&server_,SIZE)
client_.sin_family = PF_INET
client_.sin_port = htons(52252)
client_.sin_addr.s_addr = INADDR_ANY
server_.sin_family = PF_INET
server_.sin_port = htons(sock_value)
server_.sin_addr.s_addr = inet_addr(argv[1])
if(connect(sockfd,(struct sockaddr *)(&server_),SIZE) == -1){
perror("connect")
exit(1)
}
if((read_count = read(sockfd,buf,1024)) == -1){
perror("read")
exit(1)
}
buf[read_count] = '\0'
printf("#----#----#:%s\n",buf)
if(write(sockfd, mybuf,6) == -1){
perror("write")
exit(1)
}
close(sockfd)
exit(0)
return 0
}
以下方法用CURL提交post表单1. 编译环境.
安装vs2010或其他版本. vs2010 express版也可以。不要低于vc6.
2. 搜索curl-7.25.0.zip,下载。
解压到c:\curl-7.25.0
打开Visual Studio Command Prompt (2010)
cd \curl-7.25.0\winbuild
nmake /f Makefile.vc mode=dll USE_SSSPI=no ENABLE_IDN=no
编译成功后 cd ..\builds
到一个名字为libcurl-....lib的子目录里找到libcurl.dll和libcurl.lib, 保存到一个目录下备份,下面要用。
3. 打开vc++ 2010, File->New project,选Win32 Project, 输入一个项目名。下面点Next,勾上Console Application和Empty Project.
4. 配置项目
到我的文档下找到vs2010 projects目录,找到 solution名字\项目名字 目录,
把curl-7.25.0目录下的include目录拷贝到项目目录下
把2备份好的libcurl.dll和libcurl.lib拷贝到项目目录.
在vc++中右键点击项目名(或Alt+F7), 点开Configuration Properties, 点vc++directories
点Include Directories, 点Edit, 添加$(ProjectDir)include 确定
在点击左侧的Linker, 点Input,点Additional Dependences, 点Edit, 添加一行$(ProjectDir)\libcurl.lib 确定
5. 代码。
右键点项目名字,Add New Item->C++ File, name写main.c, 输入代码:
/* 抱歉,这里不好贴链接,版权没法贴,版权去看http-post.c */
#include <stdio.h>
#include <curl/curl.h>
#include <stdlib.h>
int main(void)
{
CURL *curl
CURLcode res
curl = curl_easy_init()
if(curl) {
/* First set the URL that is about to receive our POST. This URL can
just as well be a https:// URL if that is what should receive the
data. */
curl_easy_setopt(curl, CURLOPT_URL, "这里写网址")
/* Now specify the POST data */
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "name=daniel&project=curl")
/* Perform the request, res will get the return code */
res = curl_easy_perform(curl)
/* always cleanup */
curl_easy_cleanup(curl)
system("pause")
}
return 0
}
点vc++绿色的三角编译运行。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)