void OnServerOpen() //开启服务器功能 { WSADATA wsaData; int iErrorCode; char chInfo[64]; if (WSAStartup(WINSOCK_VERSION, &wsaData)) //调用Windows Sockets DLL { MessageBeep(MB_ICONSTOP); MessageBox("Winsock无法初始化!", AfxGetAppName(), MB_OK|MB_ICONSTOP); WSACleanup(); return; } else WSACleanup(); if (gethostname(chInfo, sizeof(chInfo))) { ReportWinsockErr("/n无法获取主机!/n "); return; } CString csWinsockID = "/n==>>服务器功能开启在端口:No. "; csWinsockID += itoa(m_pDoc->m_nServerPort, chInfo, 10); csWinsockID += "/n"; PrintString(csWinsockID); //在程序视图显示提示信息的函数,读者可自行创建 m_pDoc->m_hServerSocket=socket(PF_INET, SOCK_STREAM, DEFAULT_PROTOCOL); //创建服务器端Socket,类型为SOCK_STREAM,面向连接的通信 if (m_pDoc->m_hServerSocket == INVALID_SOCKET) { ReportWinsockErr("无法创建服务器socket!"); return;} m_pDoc->m_sockServerAddr.sin_family = AF_INET; m_pDoc->m_sockServerAddr.sin_addr.s_addr = INADDR_ANY; m_pDoc->m_sockServerAddr.sin_port = htons(m_pDoc->m_nServerPort); if (bind(m_pDoc->m_hServerSocket, (LPSOCKADDR)&m_pDoc->m_sockServerAddr, sizeof(m_pDoc->m_sockServerAddr)) == SOCKET_ERROR) //与选定的端口绑定 {ReportWinsockErr("无法绑定服务器socket!"); return;} iErrorCode=WSAAsyncSelect(m_pDoc->m_hServerSocket,m_hWnd, WM_SERVER_ACCEPT, FD_ACCEPT); //设定服务器相应的网络事件为FD_ACCEPT,即连接请求, // 产生相应传递给窗口的消息为WM_SERVER_ACCEPT if (iErrorCode == SOCKET_ERROR) { ReportWinsockErr("WSAAsyncSelect设定失败!"); return;} if (listen(m_pDoc->m_hServerSocket, QUEUE_SIZE) == SOCKET_ERROR) //开始监听客户连接请求 {ReportWinsockErr("服务器socket监听失败!"); m_pParentMenu->EnableMenuItem(ID_SERVER_OPEN, MF_ENABLED); return;} m_bServerIsOpen = TRUE; //监视服务器是否打开的变量 return; } |
LRESULT OnClientRead(WPARAM wParam, LPARAM lParam) { int iRead; int iBufferLength; int iEnd; int iRemainSpace; char chInBuffer[1024]; int i; for(i=0;(i //MAXClient是服务器可响应连接的最大数目 {} if(i==MAXClient) return 0L; iBufferLength = iRemainSpace = sizeof(chInBuffer); iEnd = 0; iRemainSpace -= iEnd; iBytesRead = recv(m_aClientSocket[i], (LPSTR)(chInBuffer+iEnd), iSpaceRemaining, NO_FLAGS); //用可控缓冲接收函数recv()来接收字符 iEnd+=iRead; if (iBytesRead == SOCKET_ERROR) ReportWinsockErr("recv出错!"); chInBuffer[iEnd] = '/0'; if (lstrlen(chInBuffer) != 0) {PrintString(chInBuffer); //服务器端文字显示 OnServerBroadcast(chInBuffer); //自己编写的函数,向所有连接的客户广播这个客户的聊天文字 } return(0L); } |
void OnSocketConnect() { WSADATA wsaData; DWORD dwIPAddr; SOCKADDR_IN sockAddr; if(WSAStartup(WINSOCK_VERSION,&wsaData)) //调用Windows Sockets DLL {MessageBox("Winsock无法初始化!",NULL,MB_OK); return; } m_hSocket=socket(PF_INET,SOCK_STREAM,0); //创建面向连接的socket sockAddr.sin_family=AF_INET; //使用TCP/IP协议 sockAddr.sin_port=m_iPort; //客户端指定的IP地址 sockAddr.sin_addr.S_un.S_addr=dwIPAddr; int nConnect=connect(m_hSocket,(LPSOCKADDR)&sockAddr,sizeof(sockAddr)); //请求连接 if(nConnect) ReportWinsockErr("连接失败!"); else MessageBox("连接成功!",NULL,MB_OK); int iErrorCode=WSAAsyncSelect(m_hSocket,m_hWnd,WM_SOCKET_READ,FD_READ); //指定响应的事件,为服务器发送来字符 if(iErrorCode==SOCKET_ERROR) MessageBox("WSAAsyncSelect设定失败!"); } |
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; } |
SOCKET socket(int family,int type,int protocol) |
SOCKRET s; s=socket(AF_INET,SOCK_DGRAM,0); 或s=socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP) |
int bind(SOCKET s,struct sockaddr_in*name,int namelen) |
//设置有关的全局变量 SOCKET sr,ss; HPSTR sockBufferS,sockBufferR; HANDLE hSendData,hReceiveData; DWROD dwDataSize=1024*4; struct sockaddr_in therel.there2; #DEFINE LOCAL_HOST_ADDR 200.200.200.201 #DEFINE REMOTE_HOST-ADDR 200.200.200.202 #DEFINE LOCAL_HOST_PORT 2000 #DEFINE LOCAL_HOST_PORT 2001 //套接字建立函数 BOOL make_skt(HWND hwnd) { struct sockaddr_in here,here1; ss=socket(AF_INET,SOCK_DGRAM,0); sr=socket(AF_INET,SOCK_DGRAM,0); if((ss==INVALID_SOCKET)||(sr==INVALID_SOCKET)) { MessageBox(hwnd,“套接字建立失败!”,“”,MB_OK); return(FALSE); } here.sin_family=AF_INET; here.sin_addr.s_addr=inet_addr(LOCAL_HOST_ADDR); here.sin_port=htons(LICAL_HOST_PORT); //another socket herel.sin_family=AF_INET; herel.sin_addr.s_addr(LOCAL_HOST_ADDR); herel.sin_port=htons(LOCAL_HOST_PORT1); SocketBuffer();//套接字缓冲区的锁定设置 setsockopt(ss,SOL_SOCKET,SO_SNDBUF,(char FAR*)sockBufferS,dwDataSize); if(bind(ss,(LPSOCKADDR)&here,sizeof(here))) { MessageBox(hwnd,“发送套接字绑定失败!”,“”,MB_OK); return(FALSE); } setsockopt(sr SQL_SOCKET,SO_RCVBUF|SO_REUSEADDR,(char FAR*) sockBufferR,dwDataSize); if(bind(sr,(LPSOCKADDR)&here1,sizeof(here1))) { MessageBox(hwnd,“接收套接字绑定失败!”,“”,MB_OK); return(FALSE); } return(TRUE); } //套接字缓冲区设置 void sockBuffer(void) { hSendData=GlobalAlloc(GMEM_MOVEABLE|GMEM_SHARE,dwDataSize); if(!hSendData) { MessageBox(hwnd,“发送套接字缓冲区定位失败!”,NULL, MB_OK|MB_ICONEXCLAMATION); return; } if((sockBufferS=GlobalLock(hSendData)==NULL) { MessageBox(hwnd,“发送套接字缓冲区锁定失败!”,NULL, MB_OK|MB_ICONEXCLAMATION); GlobalFree(hRecordData[0]; return; } hReceiveData=globalAlloc(GMEM_MOVEABLE|GMEM_SHARE,dwDataSize); if(!hReceiveData) { MessageBox(hwnd,"“接收套接字缓冲区定位败!”,NULL MB_OK|MB_ICONEXCLAMATION); return; } if((sockBufferT=Globallock(hReceiveData))=NULL) MessageBox(hwnd,"发送套接字缓冲区锁定失败!”,NULL, MB_OK|MB_ICONEXCLAMATION); GlobalFree(hRecordData[0]); return; } { |
int sendto(SOCKET s.char*buf,int len,int flags,struct sockaddr_in to,int tolen); int recvfrom(SOCKET s.char*buf,int len,int flags,struct sockaddr_in fron,int*fromlen) |
void CloseSocket(void) { GlobalUnlock(hSendData); GlobalFree(hSenddata); GlobalUnlock(hReceiveData); GlobalFree(hReceiveDava); if(WSAAysncSelect(ss,hwnd,0,0)=SOCKET_ERROR) { MessageBos(hwnd,“发送套接字关闭失败!”,“”,MB_OK); return; } if(WSAAysncSelect(sr,hwnd,0,0)==SOCKET_ERROR) { MessageBox(hwnd,“接收套接字关闭失败!”,“”,MB_OK); return; } WSACleanup(); closesockent(ss); closesockent(sr); return; } |