您的位置:首页 > 理论基础 > 计算机网络

简易TCP

2015-11-08 19:50 344 查看
TCP客户端部分

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);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: