本工程类似于oicq的消息发送机制,不过他只能够发送简单的字符串。虽然简单,但他也是一个很好的vc网络学习例子。
本例通过vc带的socket类,重载了他的一个接受类mysock类,此类可以吧接收到的信息显示在客户区理。以下是实现过程:
建立一个mfc 单文档工程,工程名为oicq,在第四步选取windows sockets支持,其它取默认设置即可。为了简单,这里直接把about对话框作些改变,作为发送信息界面。
这里通过失去对话框来得到发送的字符串、获得焦点时把字符串发送出去。创建oicq类的窗口,获得view类指针,进而可以把接收到的信息显示出来。extern cstring bb;
void caboutdlg::onkillfocus(cwnd* pnewwnd)
{
// todo: add your message handler code here
cdialog::onkillfocus(pnewwnd);
bb=m_edit;
}
对于oicqview类
char aa[100];
cstring mm;
cdc* pdc;
class mysock:public csocket //派生mysock类,此类既有接受功能
{public:void onreceive(int nerrorcode) //可以随时接收信息
{
csocket::receive((void*)aa,100,0);
mm=aa;
cstring ll=" ";//在显示消息之前,消除前面发送的消息
pdc->textout(50,50,ll);
pdc->textout(50,50,mm);
}
};
mysock sock1;
cstring bb;
bool coicqview::onsetcursor(cwnd* pwnd, uint nhittest, uint message)
{
cview::onsetfocus(poldwnd);
// todo: add your message handler code here and/or call default
bb="besting:"+bb; //确定发送者身份为besting
sock1.sendto(bb,100,1060,"192.168.0.255",0); //获得焦点以广播形式发送信息,端口号为1060
return cview::onsetcursor(pwnd, nhittest, message);
}
int coicqview::oncreate(lpcreatestruct lpcreatestruct)
{
if (cview::oncreate(lpcreatestruct) == -1)
return -1;
sock1.create(1060,sock_dgram,null);//以数据报形式发送消息
static cclientdc wdc(this); //获得当前视类的指针
pdc=&wdc;
// todo: add your specialized creation code here
return 0;
}
运行一下,打开about对话框,输入发送信息,enter键就可以发送信息了,是不是有点像qq啊?
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器