TCP.cpp ( File view )

  • By nwpurain 2014-10-21
  • View(s):717
  • Download(s):20
  • Point(s): 3
			// TCP.cpp : implementation file
//

#include "stdafx.h"
#include "NetAssist.h"
#include "TCP.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CTCP dialog




CTCP::CTCP(CWnd* pParent /*=NULL*/)
	: CDialog(CTCP::IDD, pParent)
{

	//{
{
AFX_DATA_INIT(CTCP)
		// NOTE: the ClassWizard will add member initialization here
	//
}
}AFX_DATA_INIT
	DestPort = 9685;
	ServerType = 5;
	m_pConnectSocket = NULL;

}

CTCP::~CTCP()
{

	if(m_pConnectSocket != NULL)
	{

		delete m_pConnectSocket;
	
}

}


void CTCP::DoDataExchange(CDataExchange* pDX)
{

	CDialog::DoDataExchange(pDX);
	//{
{
AFX_DATA_MAP(CTCP)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	m_pConnectSocket = NULL;
	DDX_Control(pDX, IDC_LIST1, m_tcpReceive);
	//
}
}AFX_DATA_MAP

}


BEGIN_MESSAGE_MAP(CTCP, CDialog)
	//{
{
AFX_MSG_MAP(CTCP)
	ON_BN_CLICKED(IDC_tcpStart, OnStart)
	ON_BN_CLICKED(IDC_tcpOperate, OntcpOperate)
	ON_BN_CLICKED(IDC_tcpClear, OntcpClear)
	ON_BN_CLICKED(IDC_tcpSend, OntcpSend)
	ON_MESSAGE(WM_RECEIVE, CTCP::OnReceive)
	//
}
}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTCP message handlers
BOOL CTCP::OnInitDialog()
{

	CDialog::OnInitDialog();
	GetDlgItem(IDC_tcpOperate)->EnableWindow(false);
	GetDlgItem(IDC_tcpClear)->EnableWindow(false);
	GetDlgItem(IDC_tcpSend)->EnableWindow(false);
	
	// TODO:  在此添加额外的初始化
	WSADATA wsd;
	sockaddr_in LocalAddr;
	WSAStartup(MAKEWORD(2,2),&wsd);
	char szName[20];
	gethostname(szName,20);
	hostent *pHost = gethostbyname(szName);
	
	memcpy(&LocalAddr.sin_addr.S_un.S_addr,pHost->h_addr_list[0],pHost->h_length);
	
	CString str(inet_ntoa(LocalAddr.sin_addr));
	SetDlgItemText(IDC_EDIT1,str);
	SetDlgItemText(IDC_EDIT3,_T("9685"));
	GetDlgItem(IDC_EDIT1)->EnableWindow(false);
	GetDlgItem(IDC_EDIT3)->EnableWindow(false);
	return TRUE;  // return TRUE unless you set the focus to a control
	// 异常: OCX 属性页应返回 FALSE

}
void CTCP::OnStart() 
{

	// TODO: Add your control notification handler code here
	GetDlgItemText(IDC_EDIT2,DestAddr);
	
	if(IsDlgButtonChecked(IDC_RADIO1))
	{

		ServerType = 0;
		if(DestAddr.IsEmpty())
			AfxMessageBox(_T("请输入目标地址!"),0,0);	
		else
		{

			GetDlgItem(IDC_tcpOperate)->EnableWindow(true);
			//GetDlgItem(IDC_tcpClear)->EnableWindow(true);
			//GetDlgItem(IDC_tcpSend)->EnableWindow(true);
			GetDlgItem(IDC_EDIT2)->EnableWindow(false);
			
			SetDlgItemText(IDC_tcpOperate,_T("连接"));
			GetDlgItem(IDC_EDIT2)->EnableWindow(false);
		
}
	
}
	else if(IsDlgButtonChecked(IDC_RADIO2))
	{

		ServerType = 1;
		GetDlgItem(IDC_tcpOperate)->EnableWindow(true);
		//GetDlgItem(IDC_tcpClear)->EnableWindow(true);
		//GetDlgItem(IDC_tcpSend)->EnableWindow(true);
		GetDlgItem(IDC_EDIT2)->EnableWindow(false);
		
		SetDlgItemText(IDC_tcpOperate,_T("监听"));
		GetDlgItem(IDC_EDIT2)->EnableWindow(false);
	
}		
	else
		AfxMessageBox(_T("请选择服务类型:客户端--服务器"),0,0);

}

void CTCP::OntcpOperate() 
{

	// TODO: Add your control notification handler code here
	
	GetDlgItem(IDC_tcpClear)->EnableWindow(true);
	GetDlgItem(IDC_tcpSend)->EnableWindow(true);
	if(ServerType == 0)
	{

		if (m_pConnectSocket != NULL)
		{

			delete m_pConnectSocket;
			m_pConnectSocket = NULL;
		
}
		m_pConnectSocket = new CConnectSocket(m_hWnd);
		
		//创建套接字
		if (!m_pConnectSocket->Create())
		{

			AfxMessageBox(_T("套接字创建失败!"),0,0);
			return;
		
}
		
		//建立连接
		if (!m_pConnectSocket->Connect(DestAddr,DestPort))
		{

			AfxMessageBox(_T("建立连接失败!"),0,0);
			return;
		
}
	
}
	else
	{

		if (m_pListenSocket != NULL)
		{

			//delete m_pListenSocket;
			m_pListenSocket = NULL;
		
}
		m_pListenSocket = new CListenSocket(m_hWnd);
		
		//创建套接字
		if (!m_pListenSocket->Create(DestPort))
		{

			AfxMessageBox(_T("套接
...
...
(Please download the complete source code to view)
			
...
Expand> <Close

Want complete source code? Download it here

Point(s): 3

Download
0 lines left, continue to read
Sponsored links

File list

Tips: You can preview the content of files by clicking file names^_^
Name Size Date
AcceptSocket.cpp899.00 B14-04-12|15:31
AcceptSocket.h1.24 kB15-06-14|22:31
Broadcast.cpp5.17 kB16-06-14|22:24
Broadcast.h1.45 kB16-06-14|22:24
ConnectSocket.cpp871.00 B14-04-12|15:26
ConnectSocket.h1.31 kB15-06-14|22:31
AcceptSocket.obj5.96 kB16-06-14|21:40
Broadcast.obj23.60 kB16-06-14|22:24
ConnectSocket.obj6.00 kB16-06-14|21:40
ListenSocket.obj7.41 kB16-06-14|21:40
Multicast.obj35.04 kB16-06-14|21:40
MulticastSocket.obj20.98 kB16-06-14|21:40
NetAssist.exe2.11 MB16-06-14|23:06
NetAssist.ilk2.48 MB16-06-14|23:06
NetAssist.obj12.44 kB16-06-14|22:29
NetAssist.pch5.27 MB16-06-14|21:40
NetAssist.pdb4.05 MB16-06-14|23:06
NetAssist.res10.88 kB16-06-14|22:29
NetAssistDlg.obj30.00 kB16-06-14|22:29
StdAfx.obj103.86 kB16-06-14|21:40
TCP.obj25.63 kB16-06-14|22:57
Unicast.obj22.99 kB16-06-14|23:06
vc60.idb233.00 kB15-10-14|23:04
vc60.pdb364.00 kB16-06-14|23:06
复件0.00 B17%|16-06-14
ListenSocket.cpp1.13 kB14-04-12|15:31
ListenSocket.h1.27 kB15-06-14|22:31
Multicast.cpp7.64 kB16-06-14|00:07
Multicast.h1.96 kB16-06-14|00:09
MulticastSocket.cpp6.81 kB16-06-14|00:00
MulticastSocket.h2.33 kB10-06-14|19:54
NetAssist.aps40.26 kB16-06-14|22:29
NetAssist.clw4.55 kB15-10-14|23:19
NetAssist.cpp2.15 kB15-06-14|22:26
NetAssist.dsp4.91 kB16-06-14|21:12
NetAssist.dsw541.00 B15-06-14|22:26
NetAssist.h1.33 kB15-06-14|22:26
NetAssist.ncb113.00 kB16-10-14|00:11
NetAssist.opt58.50 kB16-10-14|00:11
NetAssist.plg869.00 B16-06-14|23:06
NetAssist.rc10.41 kB16-06-14|22:29
NetAssistDlg.cpp5.98 kB16-06-14|22:29
NetAssistDlg.h1.63 kB16-06-14|22:29
ReadMe.txt3.55 kB15-06-14|22:26
NetAssist.ico1.05 kB15-06-14|22:26
NetAssist.rc2401.00 B15-06-14|22:26
resource.h3.09 kB16-06-14|22:29
StdAfx.cpp211.00 B15-06-14|22:26
StdAfx.h1.08 kB15-06-14|22:26
TCP.cpp5.92 kB16-06-14|21:09
TCP.h1.54 kB15-06-14|23:32
Unicast.cpp5.00 kB16-06-14|23:06
Unicast.h1.49 kB16-06-14|21:01
Debug0.00 B16-06-14|23:06
res0.00 B15-06-14|22:26
NetAssist06160.00 B16-10-14|00:11
...
Sponsored links

TCP.cpp (3.87 MB)

Need 3 point
Your Point(s)

Your Point isn't enough.

Get point immediately by PayPal

More(Debit card / Credit card / PayPal Credit / Online Banking)

Submit your source codes. Get more point

LOGIN

Don't have an account? Register now
Need any help?
Mail to: support@codeforge.com

切换到中文版?

CodeForge Chinese Version
CodeForge English Version

Where are you going?

^_^"Oops ...

Sorry!This guy is mysterious, its blog hasn't been opened, try another, please!
OK

Warm tip!

CodeForge to FavoriteFavorite by Ctrl+D