最近在写一个windows下MT4平台的通信程序,其实也就是利用dll与服务端通信:
service:服务器端是接受MT4端的订单信息并解析放在界面上显示:
代码:service.cpp
#define WIN32_LEAN_AND_MEAN
#include "service.h"
#include "qtablewidget.h"
int n = -1;
user use[100];
char recvbuf[DEFAULT_BUFLEN];
void oop(int num)
{
int i;
for( i = num;i<n;i++)
{
use[i] = use[i+1];
use[i].c = use[i].c-1;
}
}
void op(int num)
{
int num1 = n;
while(num1-->-1)
{
if(use[num1].c == num)
{
oop(num1);
n--;
}
}
}
int search(int s)
{
int i = 0;
while(use[i].t!=0&&i<=n)
{
if(s == use[i].t)
return use[i].c;
i++;
}
return -1;
}
int socket_main(QTableWidget* p)
{
WSADATA wsaData;
int iResult;
SOCKET ListenSocket = INVALID_SOCKET;
SOCKET ClientSocket[64];
for(int i = 0;i<64;i++)ClientSocket[i]= INVALID_SOCKET;
SOCKET connfd = INVALID_SOCKET;
struct addrinfo *result = NULL;
struct addrinfo hints;
fd_set global_rdfs,current_rdfs;
int maxfd,i,d = 0;
int iSendResult;
int recvbuflen = DEFAULT_BUFLEN;
printf("from server:\n");
iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if (iResult != 0) {
printf("WSAStartup failed with error: %d\n", iResult);
return 1;
}
ZeroMemory(&hints, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
hints.ai_flags = AI_PASSIVE;
// Resolve the server address and port
iResult = getaddrinfo(NULL, DEFAULT_PORT, &hints, &result);
if ( iResult != 0 ) {
printf("getaddrinfo failed with error: %d\n", iResult);
WSACleanup();
return 1;
}
// Create a SOCKET for connecting to server
ListenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
if (ListenSocket == INVALID_SOCKET) {
printf("socket failed with error: %ld\n", WSAGetLastError());
freeaddrinfo(result);
WSACleanup();
return 1;
}
// Setup the TCP listening socket
iResult = bind( ListenSocket, result->ai_addr, (int)result->ai_addrlen);
if (iResult == SOCKET_ERROR) {
printf("bind failed with error: %d\n", WSAGetLastError());
freeaddrinfo(result);
closesocket(ListenSocket);
WSACleanup();
return 1;
}
freeaddrinfo(result);
iResult = listen(ListenSocket, 64);
if (iResult == SOCKET_ERROR) {
printf("listen failed with error: %d\n", WSAGetLastError());
closesocket(ListenSocket);
WSACleanup();
return 1;
}
FD_ZERO(&global_rdfs);
FD_SET(ListenSocket,&global_rdfs);
maxfd = ListenSocket;
do {
current_rdfs = global_rdfs;
if(select(maxfd+1,¤t_rdfs,NULL,NULL,0) < 0)
{
perror("select error");
exit(-1);
}
for(i=0;i<=maxfd;i++)
{
if(FD_ISSET(i,¤t_rdfs))
{
if(i == ListenSocket)
{
ClientSocket[d] = accept(ListenSocket, NULL, NULL);
if (ClientSocket[d] == INVALID_SOCKET) {
printf("accept failed with error: %d\n", WSAGetLastError());
closesocket(ListenSocket);
WSACleanup();
return 1;
}
FD_SET(ClientSocket[d],&global_rdfs);
maxfd = maxfd>ClientSocket[d]?maxfd:ClientSocket[d];
printf("maxfd=%d",maxfd);
d++;
}
else
{
if((iResult = recv(i, recvbuf, recvbuflen, 0))<=0)
{
perror("recv error");
}
else
{
show_massage(recvbuf,p);
}
}
}
}
} while (1);
for(int i = 0;i<64;i++)
{
if(ClientSocket[i] != INVALID_SOCKET){
iResult = shutdown(ClientSocket[i], SD_SEND);
if (iResult == SOCKET_ERROR) {
printf("shutdown failed with error: %d\n", WSAGetLastError());
closesocket(ClientSocket[i]);
WSACleanup();
}
closesocket(ClientSocket[i]);
}
}
closesocket(ListenSocket);
WSACleanup();
return 0;
}
void show_massage(char* buf,QTableWidget* p)
{
int row = p->columnCount();
int num;
int c = 0;
int ret = 0;
char *numch,*p1,*p2;
strcpy(p1,strtok(buf,":"));
strcpy(numch,strtok(NULL,":"));
if(*p1 == '@')
{
p->removeRow(search(atoi(numch)));
op(search(atoi(numch)));
}
else
{
if((ret = search(atoi(numch)))==-1)
{
n++;
c = n;
p->insertRow(n);
use[n].t = atoi(numch);
use[n].c = n;
}
else
{
c = ret;
}
p->setItem(c,0,new QTableWidgetItem(p1));
p->setItem(c,1,new QTableWidgetItem(numch));
p->setItem(c,2,new QTableWidgetItem(strtok(NULL,":")));
p->setItem(c,3,new QTableWidgetItem(strtok(NULL,":")));
p->setItem(c,4,new QTableWidgetItem(strtok(NULL,":")));
p->setItem(c,5,new QTableWidgetItem(strtok(NULL,":")));
p->setItem(c,6,new QTableWidgetItem(strtok(NULL,":")));
p->setItem(c,7,new QTableWidgetItem(strtok(NULL,":")));
p->setItem(c,8,new QTableWidgetItem(strtok(NULL,":")));
p->setItem(c,9,new QTableWidgetItem(strtok(NULL,":")));
p->setItem(c,10,new QTableWidgetItem(strtok(NULL,":")));
p->setItem(c,11,new QTableWidgetItem(strtok(NULL,":")));
p->setItem(c,12,new QTableWidgetItem(strtok(NULL,":")));
}
}
由于时间问题,程序有点繁琐,不够精简。大神路过不要见笑。。。。。