zoukankan
html css js c++ java
一个HTTP.二进制POST和HTTP指定长度接收的C++实现
//
CppSocket.cpp : Defines the entry point for the console application.
//
#include
"
stdafx.h
"
#include
<
cstdlib
>
#include
<
string
>
#include
<
algorithm
>
#include
<
iostream
>
#include
<
fstream
>
#include
<
iterator
>
#include
<
Winsock2.h
>
using
namespace
std;
#define
MS_SOCKET 1
#ifdef MS_SOCKET
#define
NULLCHAR
#define
userlog printf
#endif
int
FindContentLength(
string
header);
int
RecvHttpHeader(
int
socket,
string
&
header);
int
RecvHttpBody(
int
socket,
string
&
body,
int
contentLength);
long
Post(
const
char
*
RemoteHostIP,
int
RemoteHostPort,
const
char
*
lpURL,
const
char
*
lpExtraHeaderInfo,
string
&
strRecvBuf);
int
_tmain(
int
argc,
char
*
argv[])
{
if
(argc
<
5
)
{
cout
<<
"
usage:cppsocket ip port url bodyFile
"
<<
endl
<<
"
eg: cppsockeet 127.0.0.1 80 /a.asp a.xml
"
<<
endl;
return
1
;
}
string
ip(argv[
1
]);
string
port(argv[
2
]);
string
url(argv[
3
]);
string
bodyFile(argv[
4
]);
string
headerFile;
if
(argc
>
5
)
{
headerFile
=
argv[
5
];
}
fstream fsbody(bodyFile.c_str());
if
(fsbody.good())
{
fsbody.unsetf(ios::skipws);
istream_iterator
<
char
>
iterbody(fsbody);
string
strbody(iterbody,istream_iterator
<
char
>
());
string
strRecv;
#ifdef MS_SOCKET
WSADATA wsaData;
int
iResult
=
WSAStartup(MAKEWORD(
2
,
2
),
&
wsaData);
if
(iResult
!=
NO_ERROR)
cout
<<
"
Error at WSAStartup()
"
<<
endl;
#endif
Post(ip.c_str(),(
int
)std::strtol(port.c_str(),NULL,
10
),url.c_str(),strbody.c_str(),strRecv);
#ifdef MS_SOCKET
WSACleanup();
#endif
cout
<<
strRecv;
//
cin.get();
}
else
{
cout
<<
"
can't open file
"
<<
bodyFile
<<
"
to read
"
<<
endl;
return
2
;
}
return
0
;
}
long
Post(
const
char
*
RemoteHostIP,
int
RemoteHostPort,
const
char
*
lpURL,
const
char
*
lpExtraHeaderInfo,
string
&
strRecvBuf)
{
int
SocketId,Result,optval
=
1
;
struct
sockaddr_in sServerAddr;
struct
linger mylinger;
sServerAddr.sin_family
=
AF_INET;
sServerAddr.sin_addr.s_addr
=
inet_addr( RemoteHostIP );
//
sServerAddr->sin_addr.s_addr = INADDR_ANY;
sServerAddr.sin_port
=
htons( RemoteHostPort );
#ifdef MS_SOCKET
if
((SocketId
=
socket(AF_INET,SOCK_STREAM,IPPROTO_TCP))
<
0
)
#else
if
((SocketId
=
socket(AF_INET,SOCK_STREAM,
0
))
<
0
)
#endif
{
return
-
1
;
}
mylinger.l_onoff
=
1
;
mylinger.l_linger
=
0
;
setsockopt(SocketId, SOL_SOCKET, SO_LINGER,
(
char
*
)
&
mylinger,
sizeof
(
struct
linger));
#ifndef MS_SOCKET
sigset(SIGPIPE,SIG_IGN);
#endif
optval
=
1
;
setsockopt(SocketId, SOL_SOCKET, SO_KEEPALIVE,
(
char
*
)
&
optval,
sizeof
(
int
));
Result
=
connect(SocketId,(
struct
sockaddr
*
)
&
sServerAddr,
sizeof
(
struct
sockaddr_in));
if
( Result
!=
0
)
{
shutdown( SocketId,
2
);
#ifdef MS_SOCKET
::closesocket(SocketId);
#else
close( SocketId );
#endif
userlog(
"
function connect error, return Value is %d
"
,Result);
return
-
1
;
}
//
//
Format the HTTP request
//
//
request buffer
char
szGetBuffer[
8096
];
//
input http command into request buffer and format it;
sprintf(szGetBuffer,
"
POST %s HTTP/1.0\r\n
""
Content-Type:text/html;charset=gb2312;\r\n
""
Content-Length:%u\r\n
"
"
\r\n
"
"
%s
"
"
\r\n\0
"
,
lpURL, strlen(lpExtraHeaderInfo), lpExtraHeaderInfo);
//
发送Post请求
Result
=
send(SocketId, szGetBuffer, strlen(szGetBuffer),
0
);
userlog(
"
In Post function , the szGetBuffer is %s
"
,szGetBuffer);
if
( Result
==
-
1
)
{
shutdown( SocketId,
2
);
#ifdef MS_SOCKET
::closesocket(SocketId);
#else
close( SocketId );
#endif
userlog(
"
function send error, return Value is %d
"
,Result);
return
-
1
;
}
//
userlog("function send success, the send string is \n %s \n return Value is %d",szGetBuffer,Result);
/**/
/*
* 接受Post请求的返回结果
*/
//
recv http header
string
header;
int
headerLength
=
RecvHttpHeader(SocketId,header);
if
(headerLength
>
0
)
{
int
contentLength
=
FindContentLength(header);
if
(contentLength
>
0
)
{
string
strBody;
RecvHttpBody(SocketId,strBody,contentLength);
userlog(
"
the sRecvBuf is %s
"
,strBody.c_str());
}
}
else
{
userlog(
"
the return string is not http protptype
"
);
}
#ifdef MS_SOCKET
::closesocket(SocketId);
#else
close( SocketId );
#endif
return
0
;
//
正常返回
}
int
FindContentLength(
string
header)
{
std::transform(header.begin(),header.end(),header.begin(),(
int
(
*
)(
int
)) tolower);
string
::size_type pos
=
header.find(
"
content-length
"
,
0
);
if
(pos
!=
string
::npos)
{
string
::size_type posEnd
=
header.find(
"
\r\n
"
,pos);
string
contentString
=
header.substr(pos,posEnd
-
pos);
userlog(contentString.c_str());
pos
=
contentString.find(
"
:
"
,
0
);
string
strLength
=
contentString.substr(pos
+
1
);
return
(
int
)std::strtol(strLength.c_str(),NULL,
10
);
}
return
0
;
}
int
RecvHttpHeader(
int
socket,
string
&
header)
{
header.clear();
char
chRecvBuf[
1
];
char
endBytes[]
=
{
13
,
10
,
13
,
10
}
;
int
posCompare
=
0
;
while
(
true
)
{
int
b
=
recv(socket,chRecvBuf,
1
,
0
);
if
(b
==
-
1
)
return
-
1
;
header.append(chRecvBuf,
1
);
if
(endBytes[posCompare]
==
chRecvBuf[
0
])
{
posCompare
++
;
if
(posCompare
==
sizeof
(endBytes))
{
break
;
}
}
else
{
posCompare
=
0
;
}
}
return
header.length();
}
int
RecvHttpBody(
int
socket,
string
&
body,
int
contentLength)
{
body.clear();
char
chRecvBuf[
1024
];
while
(body.length()
<
contentLength)
{
memset(chRecvBuf,
0
,
sizeof
(chRecvBuf));
int
b
=
recv(socket,chRecvBuf,
sizeof
(chRecvBuf)
-
1
,
0
);
if
(b
==
-
1
)
return
-
1
;
body.append(chRecvBuf);
}
return
body.length();
}
QQ:273352165 evlon#126.com 转载请注明出处。
查看全文
相关阅读:
基于 HTML5 + WebGL 的 3D 风力发电场
基于HTML5 WebGL的工业化3D电子围栏
基于 HTML5 WebGL 和 VR 技术的 3D 机房数据中心可视化
基于 HTML5 Canvas 的 Web SCADA 组态电机控制面板
基于 HTML5 WebGL 与 WebVR 3D 虚拟现实的可视化培训系统
基于 HTML5 WebGL 的 3D 服务器与客户端的通信
什么是 SUID, SGID 和 Sticky bit
贝塞尔曲线切割圆角
iOS-获取当前View所在的控制器
block(八)作用域
原文地址:https://www.cnblogs.com/evlon/p/853145.html
最新文章
我的程序员之路——2015年和2016年至今
我的程序员之路——2013年和2014年
我的程序员之路——大学和2012年
Vue躬行记(9)——Vuex
Vue躬行记(8)——Vue Router
Vue躬行记(7)——渲染函数和JSX
Vue躬行记(6)——内容分发
Got timeout reading communication packets解决方法
用xtrabackup2.4备份mysql5.6.30一直显示log scanned up to
升级my.cnf注意
热门文章
mysql mpm
从完整备份恢复单个innodb表
误删除innodb ibdata数据文件 文件句柄 文件描述符 proc fd
mysql命令行各个参数解释
分布式一致性解决分布式一致性问题用二阶段提交一个没有高可用组件的数据库不能依靠外部软件来实现高可用
在Linux下禁用IPv6的方法小结
Unix时间戳
基于 HTML5 WebGL + WebVR 的 3D 虚拟现实可视化培训系统
基于 HTML5 WebGL + WebVR 的 3D 虚实现实可视化系统
HTML5 + WebGL 实现的垃圾分类系统
Copyright © 2011-2022 走看看