#include <WinSock2.h>
#include <MSWSock.h>
#include <stdio.h>
struct price
{
int _buy;
int _sell;
int _clos;
};
int get_price(char *_buff,price *p_price);
int start_trading(price *p_price);
//LPFN_CONNECTEX ConnectEx;
//LPFN_DISCONNECTEX DisconnectEx;
#pragma comment(lib,"ws2_32.lib")
char get_buff[]="GET /list=RB0 HTTP/1.1\r\nHost:hq.sinajs.cn\r\n\r\n";
char rec_buff[400];
int main()
{
WORD wVersionRequested;
WSADATA wsaData;
wVersionRequested=MAKEWORD(1,1);
WSAStartup(wVersionRequested,&wsaData);
//SOCKET command_sock=socket(AF_INET, SOCK_STREAM,0);
SOCKADDR_IN server_addr;
memset((char*)&server_addr,0x00,sizeof(server_addr));
server_addr.sin_family=AF_INET;
server_addr.sin_port=htons(80);
//解析域名
hostent* remoteHost;
remoteHost=gethostbyname("hq.sinajs.cn");
server_addr.sin_addr.s_addr=*((unsigned long*)remoteHost->h_addr);//inet_addr("183.232.24.241");
/*
//获取函数指针
DWORD dwBytes;
GUID guidDisconnectEx=WSAID_DISCONNECTEX;
WSAIoctl(command_sock,
SIO_GET_EXTENSION_FUNCTION_POINTER,
&guidDisconnectEx,sizeof(guidDisconnectEx),
&DisconnectEx,
sizeof(DisconnectEx),
&dwBytes,
NULL,NULL);
*/
int ret;
int _show=0;
price _price;
COORD old_point;
COORD new_point;
new_point.X=0;
new_point.Y=0;
HANDLE h_out=GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO screen_buffer_info;
GetConsoleScreenBufferInfo(h_out,&screen_buffer_info);
printf("\n");
do
{
SOCKET command_sock=socket(AF_INET, SOCK_STREAM,0);
ret=1;
do
{
ret=connect(command_sock,(SOCKADDR*)&server_addr,sizeof(SOCKADDR));
}while (ret!=0);
//MessageBoxA(NULL,"已经连接上了","",MB_OK);
send(command_sock,get_buff,strlen(get_buff),0);
//MessageBoxA(NULL,"发送完毕","",MB_OK);
memset(rec_buff,0x00,400);
recv(command_sock,rec_buff,400,0);
closesocket(command_sock);
ret=get_price(rec_buff,&_price);
if (ret!=0)
{
//printf("最新价:%d\t买 价:%d\t卖 价:%d\n",_price._clos,_price._buy,_price._sell);
//MessageBoxA(NULL,"获取成功","",MB_OK);
switch (_show)
{
case 0:
GetConsoleScreenBufferInfo(h_out,&screen_buffer_info);
old_point.X=screen_buffer_info.dwCursorPosition.X;
old_point.Y=screen_buffer_info.dwCursorPosition.Y;
SetConsoleCursorPosition(h_out,new_point);
printf("- -最新价:%d 买 价:%d 卖 价:%d\n\n",_price._clos,_price._buy,_price._sell);
SetConsoleCursorPosition(h_out,old_point);
break;
case 1:
GetConsoleScreenBufferInfo(h_out,&screen_buffer_info);
old_point.X=screen_buffer_info.dwCursorPosition.X;
old_point.Y=screen_buffer_info.dwCursorPosition.Y;
SetConsoleCursorPosition(h_out,new_point);
printf(" / 最新价:%d 买 价:%d 卖 价:%d\n\n",_price._clos,_price._buy,_price._sell);
SetConsoleCursorPosition(h_out,old_point);
break;
case 2:
GetConsoleScreenBufferInfo(h_out,&screen_buffer_info);
old_point.X=screen_buffer_info.dwCursorPosition.X;
old_point.Y=screen_buffer_info.dwCursorPosition.Y;
SetConsoleCursorPosition(h_out,new_point);
printf("- -最新价:%d 买 价:%d 卖 价:%d\n\n",_price._clos,_price._buy,_price._sell);
SetConsoleCursorPosition(h_out,old_point);
break;
case 3:
GetConsoleScreenBufferInfo(h_out,&screen_buffer_info);
old_point.X=screen_buffer_info.dwCursorPosition.X;
old_point.Y=screen_buffer_info.dwCursorPosition.Y;
SetConsoleCursorPosition(h_out,new_point);
printf(" \\ 最新价:%d 买 价:%d 卖 价:%d\n\n",_price._clos,_price._buy,_price._sell);
SetConsoleCursorPosition(h_out,old_point);
_show=0;
break;
}
_show++;
}
start_trading(&_price);
Sleep(100);
//DisconnectEx(command_sock,NULL,TF_REUSE_SOCKET,0);
} while (1);
WSACleanup();
return 0;
}
int get_price(char *_buff,price *p_price)
{
int i=0;
int j=0;
char _temp[5];
int *p_temp;
memset(_temp,0x00,5);
do
{
if (*(_buff+i)==0x00)
{
return 0;
}
if (*(_buff+i)==0x2c)
{
j++;
if (j==6)
{
memcpy(_temp,_buff+i+1,4);
p_price->_buy=atoi(_temp);
}
if (j==7)
{
memcpy(_temp,_buff+i+1,4);
p_price->_sell=atoi(_temp);
}
if (j==8)
{
memcpy(_temp,_buff+i+1,4);
p_price->_clos=atoi(_temp);
return 1;
}
}
i++;
} while (i<400);
return 0;
}
int start_trading(price *p_price)
{
//2B模式
int new_price;
static int old_price=0;
static bool _s=false;//状态,上涨还是下跌
//int _money=0;
new_price=p_price->_clos;
if (old_price==0)
{
old_price=p_price->_clos;
}
if (!_s)
{
if (new_price>old_price)
{
if (new_price-old_price>2)
{
//printf("%d买入---->\n",p_price->_sell);
printf("%d卖出---->\n",p_price->_buy);
_s=true;
}
}
else
{
old_price=new_price;
}
}
else
{
if (new_price<old_price)
{
if (new_price-old_price<-2)
{
//printf("%d卖出---->\n",p_price->_buy);
printf("%d买入---->\n",p_price->_sell);
_s=false;
}
}
else
{
old_price=new_price;
}
}
return old_price;
}
从新浪事实获取期货行情实例
4星 · 超过85%的资源 需积分: 50 79 浏览量
2015-09-04
12:44:03
上传
评论 2
收藏 2KB ZIP 举报
q11923090
- 粉丝: 0
- 资源: 1
最新资源
- STM32单片机FPGA毕设电路原理论文报告汽车电动助力转向单片机控制系统设计与试验研究
- STM32单片机FPGA毕设电路原理论文报告气压传感器神经网络算法及单片机实现
- STM32单片机FPGA毕设电路原理论文报告频率的测量在单片机设计中的应用
- 音频转码工具(用于将微信语音 amr 格式转换为 mp3 格式以便在 html5 的 audio 标签中进行播放).zip
- RDK-Web-Performance-Node
- STM32单片机FPGA毕设电路原理论文报告片式电容器浪涌及老化测试系统的设计与实现
- 一个简易的贪吃蛇小游戏(C语言作业).zip
- 国家游戏防沉迷系统接口(php)
- STM32单片机FPGA毕设电路原理论文报告片剂硬度测试仪的液晶显示界面设计
- STM32单片机FPGA毕设电路原理论文报告偏磁式消弧线圈的动态调谐装置
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈