这是我以前写的翻译的源码,由于时间有点久....我也不太记得当时怎么写的了.....但能获取数据。
其实更简便的方法是直接从数据库拿数据,只是我不知道哪里有数据库。还请那位头像很牛叉的同学指点一下~~~~
#include<iostream>
#include<stdio.h>
#include<winsock2.h>
#include<stdlib.h>
#include<math.h>
#pragma comment(lib,"ws2_32.lib")
using namespace std;
int* control(int,char*);
int main()
{
int i=1;
char buf[20];
char *addr_1=buf;
struct sockaddr_in my_addr;
WSADATA wsaData;
WORD wVersionRequested=MAKEWORD(1,1);
if(WSAStartup(wVersionRequested,&wsaData))
{
printf("Winsock Initialization failed.\n");
exit(1);
}
SOCKET sk;
sk=socket(AF_INET,SOCK_STREAM,0);
if(sk==SOCKET_ERROR)
{
printf("socket error");
}
my_addr.sin_family=AF_INET;
my_addr.sin_port=htons(10001);
my_addr.sin_addr.s_addr=inet_addr("127.0.0.1");
bind(sk,(struct sockaddr*)&my_addr,sizeof(struct sockaddr));
listen(sk,20);
sk=accept(sk,(struct sockaddr*)&my_addr,NULL);
while(1)
{
if(sk<0)
{
// send(sk,addr_1,8,0);
closesocket(sk);
WSACleanup();
}
else
{
control(sk,addr_1);
Sleep(1);
}
}
return 0;
}
int* control(int sk,char*addr_1)
{
int i=0;
int data[20];
unsigned int Ra=0;
int De=0;
char status=recv(sk,addr_1,24,0);
if(status==SOCKET_ERROR)
{
cout<<"error in receiving"<<endl;
}
else
{
for(i=1;i<=20;i++)
{
if(int(*addr_1)<0)
{
data[i-1]=int(*addr_1)+256;}
else
{
data[i-1]=int(*addr_1);}
addr_1++;
}
for(i=1;i<=4;i++)
{
Ra=Ra+data[i+11]*pow(16,2*(i-1));
De=De+data[i+15]*pow(16,2*(i-1));
}
Ra=Ra/49710;
De=De/3314;
cout<<Ra<<" "<<De;
}
return 0;
} |