SOCKET m_sockClient;
unsigned short portNum;
-----------------------------------------------------------------------------------------
bool connect(unsigned short portNum);
void DisConnectHost();
short SendCmd(AnsiString sendData);
short ReceiveData(AnsiString *recvData);
-----------------------------------------------------------------------------------------
bool connect(unsigned short portNum)
{
//加载套接字库
WORD wVersionRequested;
WSADATA wsaData;
int err=0;
wVersionRequested=MAKEWORD(1,1);
err=WSAStartup(wVersionRequested,&wsaData);
if(err!=0)
{
return false;
}
if(LOBYTE(wsaData.wVersion)!=1 || HIBYTE(wsaData.wVersion)!=1)
{
WSACleanup();
return false;
}
//创建套接字
m_sockClient=socket(AF_INET,SOCK_STREAM,0);
SOCKADDR_IN addrSrv;
addrSrv.sin_addr.S_un.S_addr=inet_addr("192.168.3.230");
addrSrv.sin_family=AF_INET;
addrSrv.sin_port=htons(portNum);
///////////////////////////////////
//向服务器发出连接请求
err=connect(m_sockClient,(SOCKADDR*)&addrSrv,sizeof(SOCKADDR));
if(err!=0)
{
return false;
}
return true;
}
-----------------------------------------------------------------------------------------
void DisConnectHost()
{
closesocket(m_sockClient); //close(m_sockClient);
WSACleanup();
}
-----------------------------------------------------------------------------------------
short SendCmd(AnsiString sendData)
{
int sendLength=0;
int len=sendData.Length();
sendLength = send(m_sockClient,sendData.c_str(),len,0);
if(sendLength!=len)
{
return NETERROR;
}
return CMDOK;
}
-----------------------------------------------------------------------------------------
short ReceiveData(AnsiString *recvData)
{
int recvLen=0;
char p[256] = {0};
recvLen = recv(m_sockClient,p,256,0);
if(recvLen==SOCKET_ERROR)
{
return NETERROR;
}
recvData->sprintf("%s",p);
return CMDOK;
}