// TCP.cpp : Defines the entry point for the console application.
//
//#include "stdafx.h"
#include <stdlib.h>
#include <Winsock.h>
#include <stdio.h>
#include <string>
using namespace std;
#pragma comment(lib,"Ws2_32.lib")
//#define IP "74.125.71.106"//baidu
#define IP "127.0.0.1"
char buf[4096]={0};
const char EnBase64Tab[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
const char DeBase64Tab[] =
{
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
62, // '+'
0, 0, 0,
63, // '/'
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, // '0'-'9'
0, 0, 0, 0, 0, 0, 0,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, // 'A'-'Z'
0, 0, 0, 0, 0, 0,
26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38,
39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, // 'a'-'z'
};
int EncodeBase64(const unsigned char* pSrc, char* pDst, int nSrcLen)
{
unsigned char c1, c2, c3; // 输入缓冲区读出3个字节
int nDstLen = 0; // 输出的字符计数
int nLineLen = 0; // 输出的行长度计数
int nDiv = nSrcLen / 3; // 输入数据长度除以3得到的倍数
int nMod = nSrcLen % 3; // 输入数据长度除以3得到的余数
// 每次取3个字节,编码成4个字符
for (int i = 0; i < nDiv; i ++)
{
// 取3个字节
c1 = *pSrc++;
c2 = *pSrc++;
c3 = *pSrc++;
// 编码成4个字符
*pDst++ = EnBase64Tab[c1 >> 2];
*pDst++ = EnBase64Tab[((c1 << 4) | (c2 >> 4)) & 0x3f];
*pDst++ = EnBase64Tab[((c2 << 2) | (c3 >> 6)) & 0x3f];
*pDst++ = EnBase64Tab[c3 & 0x3f];
nLineLen += 4;
nDstLen += 4;
}
// 编码余下的字节
if (nMod == 1)
{
c1 = *pSrc++;
*pDst++ = EnBase64Tab[(c1 & 0xfc) >> 2];
*pDst++ = EnBase64Tab[((c1 & 0x03) << 4)];
*pDst++ = '=';
*pDst++ = '=';
nLineLen += 4;
nDstLen += 4;
}
else if (nMod == 2)
{
c1 = *pSrc++;
c2 = *pSrc++;
*pDst++ = EnBase64Tab[(c1 & 0xfc) >> 2];
*pDst++ = EnBase64Tab[((c1 & 0x03) << 4) | ((c2 & 0xf0) >> 4)];
*pDst++ = EnBase64Tab[((c2 & 0x0f) << 2)];
*pDst++ = '=';
nDstLen += 4;
}
// 输出加个结束符
*pDst = '\0';
return nDstLen;
}
SOCKET Socket()
{
WORD wVersionRequested = MAKEWORD( 2, 2 );
WSAData wsaData;
int bRet=WSAStartup(wVersionRequested,&wsaData);
if(bRet!=0)
{
printf("WSAStartup errno");
}
SOCKET fd=socket(AF_INET,SOCK_STREAM,0);
if(INVALID_SOCKET == fd)
{
printf("Create socket errno");
}
return fd;
}
int Connect(SOCKET socket)
{
sockaddr_in Sockaddr={0};
Sockaddr.sin_addr.S_un.S_addr=inet_addr(IP);
Sockaddr.sin_family=AF_INET;
Sockaddr.sin_port=htons(8081);
int bRet;
// bRet=bind(socket,(sockaddr*)&Sockaddr,sizeof(Sockaddr));
// if(SOCKET_ERROR==bRet)
// {
// printf("bind errno");
// exit(-1);
// }
bRet=connect(socket,(sockaddr*)&Sockaddr,sizeof(Sockaddr));
if(SOCKET_ERROR==bRet)
{
printf("connect errno\n");
exit(-1);
}
return 0;
}
void Send(SOCKET socket,char* buf)
{
int bRet;
bRet=send(socket,buf,1024,0);
if(SOCKET_ERROR==bRet)
{
printf("send errno\n");
}
}
void Read(SOCKET socket,char* buf)
{
int bRet;
bRet=recv(socket,buf,4096,0);
if(SOCKET_ERROR==bRet)
{
printf("Read errno");
}
}
int main(int argc, char* argv[])
{
SOCKET Rsocket=Socket();
Connect(Rsocket);
// sprintf(buf,
// "GET /index.html HTTP/1.0\r\n"
// "Host: 127.0.0.1\r\n"
// "User-Agent: Tarena/1.0\r\n"
// "Accept: text/html,text/plain,image/png\r\n"
// "Accept-Language: zh-cn\r\n"
// "Accept-Charset: gb2312,utf-8\r\n"
// "Keep-Alive: 300\r\n"
// "Connection: keep-alive\r\n"
// "\r\n");
//
//
// sprintf(buf,
// "MIME-Version: 1.0\r\n"
// "Content-Type: text/plain; boundary=qwertyuiop\r\n"
//
// "From: [email protected]\r\n"
// "To: [email protected]\r\n"
//
// "--qwertyuiop\r\n"
// "Content-Transfer-Encoding: 7bit\r\n"
// "kzh:hello\r"
// "hello"
// "--qwertyuiop--");
//
//
string m_vas_user;
string m_vas_password;
string authorization;
authorization= m_vas_user+":"+m_vas_password;
char szTempAuthorization[256]={0};
int base64len=EncodeBase64((unsigned char *)authorization.c_str(), szTempAuthorization,authorization.size());
char* p=buf;
sprintf(p,
"POST /vas HTTP/1.1\r\n"
"Content-Type: text/xml; charset=utf-8\r\n"
"Host: 127.0.0.1:8081\r\n"
"SOAPAction: \"\"\r\n"
"Content-Length: 1024\r\n"
"Authorization:Basic %s\r\n"
"Content-Length:%d\r\n\r\n"
,szTempAuthorization,1999
);
p+=sprintf(p,"%s",
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
"<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" "
"xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchemainstance\">"
"<mm7:SubmitReq xmlns:mm7=\"http://www.3gpp.org/ftp/Specs/archive/23_series/23.140/schema/REL-5-MM7-1-0\">"
"<MM7Version>5.3.0</MM7Version>"
"<SenderIdentification>"
"<VASPID>910006</VASPID>"
"<VASID>10658001</VASID>"
"</SenderIdentification>"
"<Recipients><To><Number>7255441234</Number></To></Recipients>"
"<ServiceCode>123456</ServiceCode>"
"<LinkedID>mms00016666</LinkedID>"
"<MessageClass>Informational</MessageClass>"
"<DeliveryReport>True</DeliveryReport>"
"<Priority>Normal</Priority>"
"<ChargedParty>Sender</ChargedParty>"
"<Subject>Newsfortoday</Subject>"
"</mm7:SubmitReq>"
"</soapenv>"
);
Send(Rsocket,buf);
Read( Rsocket, buf);
printf("%s",buf);
closesocket(Rsocket);
WSACleanup ();
return 0;
}
评论0