网络通信控制设备动态库代码
//---------------------------------------------
//Description:网络通信动态库
//Author: Redsky
//Date: 2020.3
//Version: 0.1
//Copy right:Gigac
//---------------------------------------------
#ifndef __ANRITSU_NETWORK_COMM_DLL_H__
#define __ANRITSU_NETWORK_COMM_DLL_H__
#ifdef __cplusplus
extern "C" {
#endif
//-------------
#define DLLEXP
#ifdef DLLEXP
#define DLLAPI //__declspec(dllexport)
#else
#define DLLAPI __declspec(dllimport)
#endif
//typedef int BOOL;
//typedef unsigned char BYTE;
//typedef unsigned short WORD;
//typedef unsigned long DWORD;
//typedef void *HANDLE;
/*网络设备连接
acServerIP:服务端IP
wPort:端口号
acSend:发送字符串
acReceive:接收字符串
acInfo:返回信息字符串
返回值:0为成功;-1:连接服务器失败; -2:发送失败; -3:接收失败
*/
extern int __stdcall DLLAPI anritsu_network_comm(const char * acServerIP, unsigned short wPort,char acSend[265], char acReceive[265],char * acInfo);
/*网络设备断开
返回值:0为成功;
*/
int __stdcall DLLAPI anritsu_network_close();
/*网络设备发送
acServerIP:服务端IP
wPort:端口号
acSend:发送字符串
acInfo:返回信息字符串
返回值:0为成功;-1:连接服务器失败; -2:发送失败;
*/
extern int __stdcall DLLAPI anritsu_network_write(const char * acServerIP,unsigned short wPort, char acSend[265],char * acInfo);
/*网络设备接收
acServerIP:服务端IP
wPort:端口号
acReceive:接收字符串
acInfo:返回信息字符串
返回值:>=0:接收字符长度;-1:连接服务器失败; -2:发送失败; -3:接收失败
*/
extern int __stdcall DLLAPI anritsu_network_read(const char * acServerIP,unsigned short wPort, char acReceive[265],char * acInfo);
//-------------
#ifdef __cplusplus
}
#endif
#endif
-----------------------------------------------------------------------------------------------
// NetworkInstrumentDll.cpp : 定义 DLL 应用程序的入口点。
//
#include "stdafx.h"
#include "AnritsuNetworkCommDll.h"
#include
#include
#pragma comment(lib, "ws2_32.lib") //加载 ws2_32.dll
#ifdef _MANAGED
#pragma managed(push, off)
#endif
SOCKET sock;
BOOL bConnected;
WSADATA wsaData;
BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
bConnected=FALSE;
WSAStartup(MAKEWORD(2, 2), &wsaData);
return TRUE;
}
/*网络设备连接
acServerIP:服务端IP
acSend:发送字符串
acReceive:接收字符串
acInfo:返回信息字符串
返回值:0为成功;-1:连接服务器失败; -2:发送失败; -3:接收失败
*/
int __stdcall DLLAPI anritsu_network_comm(const char * acServerIP,unsigned short wPort, char acSend[265], char acReceive[265],char * acInfo)
{
int iRet;
strcpy(acInfo,"");
//初始化DLL
//WSADATA wsaData;
//WSAStartup(MAKEWORD(2, 2), &wsaData);
if(!bConnected){
//创建套接字
sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
int nNetTimeout=1000;//1秒
iRet=setsockopt(sock,SOL_SOCKET,SO_SNDTIMEO,(char *)&nNetTimeout,sizeof(int));
iRet=setsockopt(sock,SOL_SOCKET,SO_RCVTIMEO,(char *)&nNetTimeout,sizeof(int));
//向服务器发起请求
sockaddr_in sockAddr;
memset(&sockAddr, 0, sizeof(sockAddr)); //每个字节都用0填充
sockAddr.sin_family = PF_INET;
sockAddr.sin_addr.s_addr = inet_addr(acServerIP);
sockAddr.sin_port = htons(wPort);
iRet=connect(sock, (SOCKADDR*)&sockAddr, sizeof(SOCKADDR));
if(iRet<0){
strcpy(acInfo,"连接网络设备失败!");
return -1;
}
bConnected=TRUE;
}
//向服务端发送数据
acSend[256]=0;
acSend[strlen(acSend)+1]=0;
acSend[strlen(acSend)]='\n';
iRet=send(sock, acSend, strlen(acSend)/*+sizeof(char)*/, NULL);
if(iRet<0){
strcpy(acInfo,"向网络设备发送失败!");
return -2;
}
//接收服务器传回的数据
char szBuffer[MAXBYTE+1] = {0};
iRet=recv(sock, szBuffer, MAXBYTE, NULL);
if(iRet<0){
strcpy(acInfo,"从网络设备接收失败!");
return -3;
}
szBuffer[iRet]=0;
//输出接收到的数据
strcpy(acReceive,szBuffer);
//关闭套接字
//closesocket(sock);
//终止使用 DLL
//WSACleanup();
return 0;
}
/*网络设备断开
返回值:0为成功;
*/
int __stdcall DLLAPI anritsu_network_close(){
bConnected=FALSE;
//关闭套接字
return closesocket(sock);
}
/*网络设备发送
acServerIP:服务端IP
acSend:发送字符串
acInfo:返回信息字符串
返回值:0为成功;-1:连接服务器失败; -2:发送失败;
*/
int __stdcall DLLAPI anritsu_network_write(const char * acServerIP,unsigned short wPort, char acSend[265],char * acInfo)
{
int iRet;
strcpy(acInfo,"");
if(!bConnected){
//创建套接字
sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
int nNetTimeout=1000;//1秒
iRet=setsockopt(sock,SOL_SOCKET,SO_SNDTIMEO,(char *)&nNetTimeout,sizeof(int));
iRet=setsockopt(sock,SOL_SOCKET,SO_RCVTIMEO,(char *)&nNetTimeout,sizeof(int));
//向服务器发起请求
sockaddr_in sockAddr;
memset(&sockAddr, 0, sizeof(sockAddr)); //每个字节都用0填充
sockAddr.sin_family = PF_INET;
sockAddr.sin_addr.s_addr = inet_addr(acServerIP);
sockAddr.sin_port = htons(wPort);
iRet=connect(sock, (SOCKADDR*)&sockAddr, sizeof(SOCKADDR));
if(iRet<0){
strcpy(acInfo,"连接网络设备失败!");
return -1;
}
bConnected=TRUE;
}
//向服务端发送数据
acSend[256]=0;
acSend[strlen(acSend)+1]=0;
acSend[strlen(acSend)]='\n';
iRet=send(sock, acSend, strlen(acSend)/*+sizeof(char)*/, NULL);
if(iRet<0){
strcpy(acInfo,"向网络设备发送失败!");
return -2;
}
return 0;
}
/*网络设备接收
acServerIP:服务端IP
acReceive:接收字符串
acInfo:返回信息字符串
返回值:>=0:接收字符长度;-1:连接服务器失败; -2:发送失败; -3:接收失败
*/
int __stdcall DLLAPI anritsu_network_read(const char * acServerIP,unsigned short wPort, char acReceive[265],char * acInfo)
{
int iRet;
strcpy(acInfo,"");
if(!bConnected){
strcpy(acInfo,"没有连接网络设备!");
return -1;
}
//接收服务器传回的数据
char szBuffer[MAXBYTE+1] = {0};
iRet=recv(sock, szBuffer, MAXBYTE, NULL);
if(iRet<0){
strcpy(acInfo,"从网络设备接收失败!");
return -3;
}
//输出接收到的数据
for(int i=0;i
}
acReceive[iRet]=0;
return iRet;
}
#ifdef _MANAGED
#pragma managed(pop)
#endif