简易TCP
2015-11-08 19:50
344 查看
TCP客户端部分
InitDlg()中
CSocket派生类
TCP服务器部分
theApp成员变量
在InitDlg()中
第一个CSocket派生类
InitDlg()中
this->m_sock.Create(); if(!m_sock.Connect("192.168.1.100",8118)) { //连接服务器端运行的主机IP地址 CString str; str.Format("连接失败:%d",::GetLastError()); ::AfxMessageBox(str); this->GetDlgItem(IDC_STATIC1)->SetWindowTextA(str); } else { this->GetDlgItem(IDC_STATIC1)->SetWindowTextA("成功连接服务器192.168.1.100"); }发送按钮中
CString szText; this->GetDlgItemTextA(IDC_EDIT2,szText); if(this->m_sock.Send(szText,szText.GetLength())>0) { CEdit* pEdit=(CEdit*)this->GetDlgItem(IDC_HIST); pEdit->ReplaceSel("你对所有人说: \r\n"+szText+"\r\n"); }
CSocket派生类
class CConnectSocket : public CSocket { public: CConnectSocket(); virtual ~CConnectSocket(); virtual int Receive(void* lpBuf, int nBufLen, int nFlags = 0); virtual void OnReceive(int nErrorCode); };
void CConnectSocket::OnReceive(int nErrorCode) { // TODO: Add your specialized code here and/or call the base class char s[2048]; int nLen=this->Receive(s,sizeof(s)-1); if(nLen<=0) return; s[nLen]='\0'; CWnd* pWnd=::AfxGetMainWnd(); CEdit* pEdit=(CEdit*)pWnd->GetDlgItem(IDC_HIST); pEdit->ReplaceSel(s); CSocket::OnReceive(nErrorCode); }
TCP服务器部分
theApp成员变量
public: //指针链表型公有成员,用于存储客户端通信的CClientSocket对象地址 CPtrList m_list;
在InitDlg()中
//创建服务端socket并开始侦听 if(this->m_sock.Create(8118)) { this->m_sock.Listen(); this->GetDlgItem(IDC_STATIC1)->SetWindowTextA("服务端创建成功开始侦听..."); } else { CString str; str.Format("Socket创建失败 错误号:%d",::GetLastError()); this->GetDlgItem(IDC_STATIC1)->SetWindowTextA(str); ::AfxMessageBox(str); }
private: CListenSocket m_sock;
第一个CSocket派生类
class CListenSocket : public CSocket { public: CListenSocket(); virtual ~CListenSocket(); virtual void OnAccept(int nErrorCode); };
#include "ClientSocket.h" extern CServerApp theApp; void CListenSocket::OnAccept(int nErrorCode) { // TODO: Add your specialized code here and/or call the base class //新建一个CClientSocket对象,与新连接进入的客户端关联 CClientSocket *pSock=new CClientSocket; if(this->Accept(*pSock)) theApp.m_list.AddTail(pSock); else delete pSock; CSocket::OnAccept(nErrorCode); }第二个CSocket派生类
class CClientSocket : public CSocket { public: CClientSocket(); virtual ~CClientSocket(); virtual void OnReceive(int nErrorCode); virtual void OnClose(int nErrorCode); };
void CClientSocket::OnReceive(int nErrorCode) { // TODO: Add your specialized code here and/or call the base class char s[2048]; int nLen=this->Receive(s,sizeof(s)-1); if(nLen<=0) return; s[nLen]=0; CString szIP; UINT nPort; this->GetPeerName(szIP,nPort); CString str; str.Format("%s-%d 对所有人说: \r\n%s\r\n",szIP,nPort,s); CPtrList& list=::theApp.m_list; POSITION pos=list.GetHeadPosition(); //遍历m_list while(pos) { //将一个客户端发来的聊天文字,群发给所有其他客户端 CClientSocket* pSock=(CClientSocket*)list.GetAt(pos); //不对发消息者本人回发 if(pSock!=this) pSock->Send(str,str.GetAllocLength()); list.GetNext(pos); } CSocket::OnReceive(nErrorCode); }
void CClientSocket::OnClose(int nErrorCode) { // TODO: Add your specialized code here and/or call the base class //将与客户端断开连接的CClientSocket对象地址从链表中移除 CPtrList& list=::theApp.m_list; POSITION pos=list.GetHeadPosition(); while(pos) { if(list.GetAt(pos)==this) { list.RemoveAt(pos); break; } list.GetNext(pos); } //清理对象占用的堆空间 delete this; CSocket::OnClose(nErrorCode); }
相关文章推荐
- HttpClient3.0入门1
- linux网络配置
- GET http://test01.com/jquery-1.9.1.min.js [HTTP/1.1 404 Not Found 3ms]
- java网络编程(三):一个类似QQ的聊天程序
- 容量测试之tcpcopy引流模式
- TCP打洞技术
- Protocol Informatics【基于网络轨迹的协议逆向工程文献学习】
- We7<001>--We7 CMS之报错: HTTP 错误 404.0 - Not Found 您要找的资源已被删除、已更名或暂时不可用。
- Linux(Centos6.5)下安装svn服务器,并通过http访问
- 建立网络连接的基本步骤及NSJSONSerialization(苹果自带)——JSON解析
- Unix c 网络编程 UDP
- Unix c 网络编程 TCP
- Unix c 网络编程——编程基础
- Http和Https简介
- 重温OSPF——OSPF邻居建立
- 关于网络三 - XML 生成及解析、JSON 解析
- 网络编程简介(1)之JAVA网络编程
- 僵尸网络的相关研究文章
- POJ 3281 Dining(网络流最大匹配)
- 基于HTTPS协议的12306抢票软件设计与实现--相关接口以及数据格式