基于套接字的聊天程序设计内容摘要:

break。 } } else { break。 } } closesocket(sockConn)。 //关闭套接字 coutServer waiting:...endl。 } WSACleanup()。 return 0。 } 客户端设计 : include include iostream using namespace std。 include int main(int argc, char *argv[]) { const int DEFAULT_PORT = 5000。 WORD wVersionRequested。 WSADATA wsaData。 int err,iLen。 wVersionRequested=MAKEWORD( 2, 2 )。 err = WSAStartup( wVersionRequested, amp。 wsaData )。 if ( err != 0 ) { cout加载 WinSock失败。 return 0。 10 / 39 } //创建用于连接的套接字 SOCKET sockClient = socket(AF_INET,SOCK_STREAM,0)。 if (sockClient == INVALID_SOCKET) { coutsocket() fail:WSAGetLastError()endl。 return 0。 } SOCKADDR_IN addrSrv。 //string ip。 char cip[20]=。 int port。 char type。 cout 服务器基本设置为 ip: 端口: 5000endl。 cout 是否从新设置: Y or Nendl。 cintype。 if(type==39。 Y39。 ) { coutIP:。 cincip。 //ip=cip。 cout端口:。 cinport。 = AF_INET。 = i_addr(cip)。 = htons(port)。 } else { = AF_INET。 = i_addr()。 = htons(DEFAULT_PORT)。 } err = connect(sockClient,(SOCKADDR*)amp。 addrSrv,sizeof(SOCKADDR))。 //请求连接服务器进程 if ( err == INVALID_SOCKET ) { coutconnect() fail:WSAGetLastError()endl。 return 0。 } 11 / 39 char sendBuf[1024],hostname[100]。 if (gethostname(hostname,100) != 0) //获取主机名称 strcpy(hostname,None)。 strcpy(sendBuf,hostname)。 strcat(sendBuf, have coned to you!)。 err = send(sockClient,sendBuf,strlen(sendBuf)+1,0)。 //发送数据 if (err == SOCKET_ERROR) { coutsend() fail:WSAGetLastError()endl。 return 0。 } char recvBuf[1024]。 iLen = recv(sockClient,recvBuf,1024,0)。 //接收数据 if (iLen == 0) return 0。 else if (iLen == SOCKET_ERROR) { coutrecv() fail:WSAGetLastError()endl。 return 0。 } else { recvBuf[iLen] = 39。 \039。 coutrecvBufendl。 } while(1) { char sendBuf[1024]。 cout请输入你要发送的消息,输入 E退出 endl。 cinsendBuf。 string str=sendBuf。 if((E)) { err = send(sockClient,sendBuf,strlen(sendBuf)+1,0)。 //发送数据 if (err == SOCKET_ERROR) { coutsend() fail:WSAGetLastError()endl。 return 0。 } } 12 / 39 else { break。 } char recvBuf[1024]。 iLen = recv(sockClient,recvBuf,1024,0)。 //接收数据 if (iLen == 0) return 0。 else if (iLen == SOCKET_ERROR) { coutrecv() fail:WSAGetLastError()endl。 return 0。 } else { recvBuf[iLen] = 39。 \039。 coutrecvBufendl。 } } closesocket(sockClient)。 //关闭套接字 WSACleanup()。 } 基于 UDP 的应用程序设计 : 服务端设计: CUDPSocket 类,该类主要负责启动套接字,并接受客户发来的数据和发送数据 下面给出该类的源代码 //// : 头文件 pragma once // CUDPSocket 命令目标 pragma once include // CUDPSocket 命令目标 13 / 39 class CUDPSocket : public CAsyncSocket { public: CUDPSocket()。 virtual ~CUDPSocket()。 public: virtual void OnSend(int nErrorCode)。 virtual void OnReceive(int nErrorCode)。 bool BindLocatePC(DWORD m_LocateIP,int m_LocatePort)。 bool SendToRemotePC(DWORD m_RemoteIP,int m_RemotePort,CString m_SendData)。 public: bool m_fConnected。 //标志变量 }。 // // : 实现文件 // include include include include // CUDPSocket CUDPSocket::CUDPSocket(void) { m_fConnected = false。 } CUDPSocket::~CUDPSocket(void) { } // CUDPSocket 成员 函数 //事件响应函数(当一个套接字已经准备好了,并可以利用 Send 函数去发送数据时,就可以调用该函数去处理相应的消息) void CUDPSocket::OnSend(int nErrorCode) { CCUDPServerDlg *pDl = CCUDPServerDlg::GetDialog()。 //pDlOnSend(nErrorCode)。 } 14 / 39 //事件响应函数(当一个套接字准备好了,并且可以利用 Receive 接收时,就可以去调用该函数去处理相应的消息) void CUDPSocket::OnReceive(int nErrorCode) { CCUDPServerDlg *pDl = CCUDPServerDlg::GetDialog()。 pDlOnReceive(nErrorCode)。 } //地址和端口绑定 bool CUDPSocket::BindLocatePC(DWORD m_LocateIP,int m_LocatePort) { in_addr tempAddr。 CString buf。 /* LPCTSTR 类型 L 表示 long指针 这是 为了兼容 Window。
阅读剩余 0%
本站所有文章资讯、展示的图片素材等内容均为注册用户上传(部分报媒/平媒内容转载自网络合作媒体),仅供学习参考。 用户通过本站上传、发布的任何内容的知识产权归属用户或原始著作权人所有。如有侵犯您的版权,请联系我们反馈本站将在三个工作日内改正。