📄 tcpip_com.cpp
字号:
// TCPIP_COM.cpp : implementation file
//
#include "stdafx.h"
#include "Demo_01.h"
#include "TCPIP_COM.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CTCPIP_COM dialog
CTCPIP_COM::CTCPIP_COM(CWnd* pParent /*=NULL*/)
: CDialog(CTCPIP_COM::IDD, pParent)
{
//{{AFX_DATA_INIT(CTCPIP_COM)
m_ip_addr = _T("");
m_data = _T("");
m_portno = 0;
//}}AFX_DATA_INIT
}
void CTCPIP_COM::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CTCPIP_COM)
DDX_Text(pDX, IDC_IP_ADDRESS, m_ip_addr);
DDV_MaxChars(pDX, m_ip_addr, 16);
DDX_Text(pDX, IDC_TCP_DATA, m_data);
DDV_MaxChars(pDX, m_data, 256);
DDX_Text(pDX, IDC_IP_PORTNO, m_portno);
DDV_MinMaxInt(pDX, m_portno, 0, 65535);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CTCPIP_COM, CDialog)
//{{AFX_MSG_MAP(CTCPIP_COM)
ON_BN_CLICKED(IDC_CONNECT, OnConnect)
ON_BN_CLICKED(IDC_TCP_SEND, OnTcpSend)
ON_BN_CLICKED(IDC_DISCONNECT, OnDisconnect)
ON_WM_CLOSE()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CTCPIP_COM message handlers
BOOL CTCPIP_COM::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: Add extra initialization here
//----------------- Demo_01 add -------------------
m_ip_addr.Format(_T("192.168.127.254"));
m_portno= 9600;
UpdateData(FALSE);
socket_hd= NULL;
//--------------- end of add ----------------------
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
//****************************************************************************
void CTCPIP_COM::OnConnect()
{
// TODO: Add your control notification handler code here
//----------------- Demo_01 add -------------------
UpdateData(TRUE);
SOCKADDR_IN Addr;
int tcp_port, len;
char ip_addr[20];
CString msg;
socket_hd=socket(AF_INET,SOCK_STREAM,0);
if(socket_hd == INVALID_SOCKET)
{
socket_hd= NULL;
msg.Format(_T("Can't create Socket Handle !!!!!!!!"));
AfxMessageBox(msg, MB_OK);
return;
}
memset(ip_addr, 0, sizeof(ip_addr));
len= m_ip_addr.GetLength();
wcstombs(ip_addr, m_ip_addr, len);
tcp_port= m_portno;
// connect to remote server
memset( &Addr, 0, sizeof( SOCKADDR_IN ) );
Addr.sin_family = AF_INET;
Addr.sin_port = htons( tcp_port );
Addr.sin_addr.s_addr = inet_addr(ip_addr);
if(connect(socket_hd, (LPSOCKADDR) &Addr, sizeof(SOCKADDR_IN)) == SOCKET_ERROR)
{
closesocket(socket_hd);
socket_hd= NULL;
msg.Format(_T("Can't connect remote server !!!!!!!!"));
AfxMessageBox(msg, MB_OK);
return;
}
else
{
msg.Format(_T("Connect remote server OK......."));
AfxMessageBox(msg, MB_OK);
}
//--------------- end of add ----------------------
}
//****************************************************************************
void CTCPIP_COM::OnTcpSend()
{
// TODO: Add your control notification handler code here
//----------------- Demo_01 add -------------------
UpdateData(TRUE);
CString msg;
char outbuff[512];
int len;
memset(outbuff, 0, sizeof(outbuff));
len= m_data.GetLength();
wcstombs(outbuff, m_data, len);
len= strlen(outbuff);
if(( send(socket_hd, (char *)outbuff, len, 0)) == SOCKET_ERROR)
{
msg.Format(_T("Send data to remote server Error !!!!!!!!!"));
AfxMessageBox(msg, MB_OK);
return;
}
else
{
msg.Format(_T("Send data to remote server OK......."));
AfxMessageBox(msg, MB_OK);
}
//--------------- end of add ----------------------
}
//****************************************************************************
void CTCPIP_COM::OnDisconnect()
{
// TODO: Add your control notification handler code here
//----------------- Demo_01 add -------------------
if(socket_hd != NULL)
{
closesocket(socket_hd);
socket_hd= NULL;
}
//--------------- end of add ----------------------
}
//****************************************************************************
void CTCPIP_COM::OnClose()
{
// TODO: Add your message handler code here and/or call default
//----------------- Demo_01 add -------------------
if(socket_hd != NULL)
{
closesocket(socket_hd);
socket_hd= NULL;
}
//--------------- end of add ----------------------
CDialog::OnClose();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -