/
-
#include <stdlib.h>
-
#include <errno.h>
-
#include <string.h>
-
#include <unistd.h>
-
#include <sys/socket.h>
-
#include <netinet/in.h>
-
#include <arpa/inet.h>
-
#include <netdb.h>
-
#include <stdio.h>
-
-
void posturl()
-
{
-
int sockfd;
-
struct sockaddr_in addr;
-
struct hostent *purl;
-
-
char *phost = 0, *ppost = 0;
-
static char text[bufsiz];
-
char header[bufsiz] = "";
-
int i;
-
-
/*
-
* 设定socket参数,并未真正初始化
-
*/
-
sockfd = socket(af_inet, sock_stream, 0);
-
-
addr.sin_family = af_inet;
-
inet_aton("192.168.10.200",&addr.sin_addr);
-
addr.sin_port = htons(80);
-
-
/*
-
* 组织发送到web服务器的信息
-
* 为何要发送下面的信息请参考http协议的约定
-
*/
-
strcat(header, "post ");
-
strcat(header, "/pass");
-
strcat(header, " http/1.1\r\n");
-
strcat(header, "*/*\r\n");
-
strcat(header, "referer: \r\n");
-
strcat(header, "accept-language: zh-cn\r\n");
-
strcat(header,"content-type: application/x-www-form-urlencoded\r\n");
-
strcat(header, "host: ");
-
strcat(header, "192.168.10.200\r\n");
-
strcat(header, "content-length: 26\r\n");
-
strcat(header, "connection: keep-alive\r\n");
-
strcat(header, "cache-control: no-cache\r\n\r\n");
-
strcat(header, "password=1234&submit=apply");
-
strcat(header, "\r\n");
-
-
/*
-
* 连接到服务器,发送请求header,并接受反馈(即网页源代码)
-
*/
-
connect(sockfd,(struct sockaddr *)&addr,sizeof(struct sockaddr_in));
-
-
i = send(sockfd, header, strlen(header), 0);
-
-
printf("%d\n",i);
-
-
while ( recv(sockfd, text, bufsiz, 0) > 0)
-
{
-
printf("%s", text);
-
for(i = 0; i < bufsiz; i)
-
text[i]='\0';
-
//strnset(text, '\0', bufsiz);
-
}
-
-
close(sockfd);
-
-
-
}
-
-
int main()
-
{
-
posturl();
-
return 0;
-
}
-
//*****************************************//
-
//*********利用c实现自动发包***************//
-
//*********作者:马 犇******************//
-
//*********时间:2011-6-25*****************//
-
//*****************************************//
-
-
#include "stdafx.h"
-
#include "stdio.h"
-
#include "stdlib.h"
-
#include "winsock2.h"
-
#pragma comment(lib,"ws2_32.lib")
-
int main(int argc, char* argv[])
-
{
-
-
socket hsocket;
-
sockaddr_in saserver;
-
wsadata wsadata;
-
lphostent lphostent;
-
int nret;
-
char* host_name="www.********.com";
-
char* reqhead="post ********* http/1.1\r\n" //此为要发送的数据包
-
"accept: */*\r\n"
-
"referer: *********\r\n"
-
"accept-language: zh-cn\r\n"
-
"user-agent: mozilla/5.0 (compatible; msie 9.0; windows nt 6.1; trident/5.0; boie9;zhcn; twchrome)\r\n"
-
"content-type: application/x-www-form-urlencoded\r\n"
-
"accept-encoding: gzip, deflate\r\n"
-
"host: www.*******.com\r\n"
-
"content-length: 375\r\n"
-
"connection: keep-alive\r\n"
-
"cache-control: no-cache\r\n"
-
"cookie: ************** \r\n\r\n"
-
"user=test&pass=123";
-
-
// 初始化套接字
-
if(wsastartup(makeword(2,2),&wsadata))
-
printf("初始化socket出错!");
-
lphostent=gethostbyname(host_name);
-
if(lphostent==null)
-
printf("lphostent为空!");
-
hsocket = socket(af_inet,sock_stream,ipproto_tcp);
-
saserver.sin_family = af_inet;
-
saserver.sin_port = htons(80);
-
saserver.sin_addr = *((lpin_addr)*lphostent->h_addr_list);
-
// 利用socket连接
-
nret = connect(hsocket,(lpsockaddr)&saserver,sizeof(sockaddr_in));
-
if(nret == socket_error)
-
{
-
printf("建立连接时出错!");
-
closesocket(hsocket);
-
return 0;
-
}
-
// 利用socket发送
-
-
nret = send(hsocket,req,strlen(req),0);
-
if(nret==socket_error)
-
{
-
printf("发送数据包时出错!");
-
closesocket(hsocket);
-
}
-
char dest[3000];
-
nret=1;
-
while(nret>0)
-
{
-
// 接收返回数据包
-
nret=recv(hsocket,(lpstr)dest,sizeof(dest),0);
-
if(nret>0)
-
dest[nret]=0;
-
else
-
dest[0]=0;
-
// 显示返回数据包的大小、内容
-
printf("\nreceived bytes:%d\n",nret);
-
printf("result:\n%s",dest);
-
}
-
}
-
return 0;
-
}
c语言开发linux下web服务器(支持get/post,ssl,目录显示等)
阅读(4229) | 评论(0) | 转发(0) |