vc下c语言网络编程,用VC编写C/S消息传送程序
下載本文示例代碼
網(wǎng)絡(luò)編程已經(jīng)成為一種時髦,以TCP/IP協(xié)議的網(wǎng)絡(luò)更為流行.自己編一個服務(wù)器與客戶機互相傳送消息的程序,以便增加自己網(wǎng)絡(luò)編程的經(jīng)驗。下面我就介紹一下我編的程序。 首先介紹服務(wù)器程序: 1.創(chuàng)建一個名為"server"的項目,單文檔界面. 2.在serverview.h中加入代碼:
#include "winsock.h" 添加變量:
CSize sizeTotal;//控制滾動條intcount;//信息條數(shù)CString m_data[1000];//信息存放char Hostname[260];char Hostaddress[20];//主機IP地址SOCKET m_sock;HANDLE m_hListenThread;//線程BOOL m_bInitialized;//是否初始化WSADATAWSAData;BOOL flag;SOCKADDR_IN saClnt;int saClntLen;BOOL Isconnect;//是否連接 3.在serverview.cpp中重載CServerView()構(gòu)造器,創(chuàng)建并綁定嵌套字:
CServerView::CServerView(){ // TODO: add construction code here Isconnect=FALSE; flag=FALSE; sizeTotal.cy=350; sizeTotal.cx=300; m_hListenThread; count=5; int status; WSADATA wsaData; m_data[0]="initializing Windows Sockets DLL...."; if((status=WSAStartup(0x0101,&wsaData))==0) { m_data[0] ="Succeeded"; m_bInitialized=TRUE; } else { m_bInitialized=FALSE; } m_sock=socket(AF_INET,SOCK_DGRAM,0); m_data[1]="Creating socket...."; if(m_sock==INVALID_SOCKET) { m_data[1] ="Failed"; } m_data[1] ="Succeeded"; m_data[2]="Binding socket...."; sockaddr_in sa; sa.sin_family=AF_INET; sa.sin_addr.S_un.S_addr=htonl(INADDR_ANY); sa.sin_port=htons(5050); if(bind(m_sock,(PSOCKADDR)&sa,sizeof(sa))==SOCKET_ERROR) { m_data[2] ="Failed"; closesocket(m_sock); } m_data[2] ="Succeeded"; m_data[3]="Creating listener thread...."; unsigned long idThread; m_hListenThread=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)Listen,(void *)this,0,&idThread); if(m_hListenThread) { m_data[3] ="Succeeded"; m_data[4] ="Listening...."; } else m_data[4] ="Failed";} 4.在析構(gòu)函數(shù)中完成必需的清除操作:
CServerView::~CServerView(){ if(m_bInitialized) WSACleanup(); closesocket(m_sock); if(m_hListenThread) ::TerminateThread(m_hListenThread,0);} 5.定義接收和處理消息的線程:
long WINAPI Listen(CServerView *pView){ char msg[2000]=""; intnchar; SOCKADDR_IN saClnt; int saClntLen; while(1) { saClntLen=sizeof(saClnt); nchar=recvfrom(pView->m_sock,msg,1024,0,(PSOCKADDR)&saClnt,&saClntLen); if(nchar<0) { pView->m_data[pView->count ] ="Error in recvfrom\n"; pView->InvalidateRect(NULL); } else { switch(msg[0]) { case'A': wsprintf(msg,"A: Client from %s attached\n",inet_ntoa(saClnt.sin_addr)); pView->m_data[pView->count ]=msg; pView->flag=TRUE; pView->InvalidateRect(NULL); pView->Isconnect=TRUE; pView->saClnt=saClnt; pView->saClntLen=saClntLen; sendto(pView->m_sock,msg,1024,0,(PSOCKADDR)&saClnt,saClntLen); break; case 'D': wsprintf(msg,"D: Client form %s detached\n",inet_ntoa(saClnt.sin_addr)); pView->m_data[pView->count ]=msg; pView->flag=TRUE; pView->InvalidateRect(NULL); pView->Isconnect=FALSE; sendto(pView->m_sock,msg,1024,0,(PSOCKADDR)&saClnt,saClntLen); break; case 'R': saClntLen=sizeof(saClnt); pView->m_data[pView->count ]=msg; pView->flag=TRUE; pView->InvalidateRect(NULL); break; default: break; } } } return(0);} 6.在程序菜單項中添加"本機IP地址":
void CServerView::OnIp() { int WSAReturn; WSAReturn=WSAStartup( 0x0101, &WSAData ); if( WSAReturn == 0 ){ gethostname( Hostname, 260 ); struct hostent *pHostEnt; pHostEnt = gethostbyname( Hostname); if( pHostEnt != NULL ){ wsprintf( Hostaddress, "%d.%d.%d.%d", ( pHostEnt->h_addr_list[0][0] & 0x00ff ), ( pHostEnt->h_addr_list[0][1] & 0x00ff ), ( pHostEnt->h_addr_list[0][2] & 0x00ff ), ( pHostEnt->h_addr_list[0][3] & 0x00ff ) ); CString out; out.Format(Hostaddress); AfxMessageBox(out); } }} 7.在程序菜單中添加"發(fā)送消息":
void CServerView::OnSendmessage() { // TODO: Add your command handler code here char msg[2000]; Csend Sendmessage; if(Sendmessage.DoModal() ==IDOK&&!Sendmessage.m_Message.IsEmpty()) { wsprintf(msg,"R: " Sendmessage.m_Message); sendto(m_sock,msg,1024,0,(PSOCKADDR)&saClnt,saClntLen); m_data[count ]=Sendmessage.m_Message; flag=TRUE; InvalidateRect(NULL); }} 8.為發(fā)送消息項添加一個對話框的類,名為send,有一個文本框,用來發(fā)送消息.并為文本框添加CString m_Message 變量,并在ServerView.cpp中添加#include "send.h" 9.為發(fā)送消息項添加一個判斷函數(shù):
void CServerView::OnUpdateSendmessage(CCmdUI* pCmdUI) { // TODO: Add your command update UI handler code here pCmdUI->Enable(FALSE); if(Isconnect) pCmdUI->Enable(TRUE);} 10.再窗口顯示消息:
void CServerView::OnDraw(CDC* pDC){ if(flag) { sizeTotal.cy =20; for(int j=65;jTextOut(10,y,m_data[i]); y =20; } // TODO: add draw code for native data here} 11.在Project中點擊Settings中選擇Link項添加wsock32.lib. 最后編譯程序,就可以得到Server.exe程序. 共2頁。 1 2 :
網(wǎng)絡(luò)編程已經(jīng)成為一種時髦,以TCP/IP協(xié)議的網(wǎng)絡(luò)更為流行.自己編一個服務(wù)器與客戶機互相傳送消息的程序,以便增加自己網(wǎng)絡(luò)編程的經(jīng)驗。下面我就介紹一下我編的程序。 首先介紹服務(wù)器程序: 1.創(chuàng)建一個名為"server"的項目,單文檔界面. 2.在serverview.h中加入代碼:
#include "winsock.h" 添加變量:
CSize sizeTotal;//控制滾動條intcount;//信息條數(shù)CString m_data[1000];//信息存放char Hostname[260];char Hostaddress[20];//主機IP地址SOCKET m_sock;HANDLE m_hListenThread;//線程BOOL m_bInitialized;//是否初始化WSADATAWSAData;BOOL flag;SOCKADDR_IN saClnt;int saClntLen;BOOL Isconnect;//是否連接 3.在serverview.cpp中重載CServerView()構(gòu)造器,創(chuàng)建并綁定嵌套字:
CServerView::CServerView(){ // TODO: add construction code here Isconnect=FALSE; flag=FALSE; sizeTotal.cy=350; sizeTotal.cx=300; m_hListenThread; count=5; int status; WSADATA wsaData; m_data[0]="initializing Windows Sockets DLL...."; if((status=WSAStartup(0x0101,&wsaData))==0) { m_data[0] ="Succeeded"; m_bInitialized=TRUE; } else { m_bInitialized=FALSE; } m_sock=socket(AF_INET,SOCK_DGRAM,0); m_data[1]="Creating socket...."; if(m_sock==INVALID_SOCKET) { m_data[1] ="Failed"; } m_data[1] ="Succeeded"; m_data[2]="Binding socket...."; sockaddr_in sa; sa.sin_family=AF_INET; sa.sin_addr.S_un.S_addr=htonl(INADDR_ANY); sa.sin_port=htons(5050); if(bind(m_sock,(PSOCKADDR)&sa,sizeof(sa))==SOCKET_ERROR) { m_data[2] ="Failed"; closesocket(m_sock); } m_data[2] ="Succeeded"; m_data[3]="Creating listener thread...."; unsigned long idThread; m_hListenThread=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)Listen,(void *)this,0,&idThread); if(m_hListenThread) { m_data[3] ="Succeeded"; m_data[4] ="Listening...."; } else m_data[4] ="Failed";} 4.在析構(gòu)函數(shù)中完成必需的清除操作:
CServerView::~CServerView(){ if(m_bInitialized) WSACleanup(); closesocket(m_sock); if(m_hListenThread) ::TerminateThread(m_hListenThread,0);} 5.定義接收和處理消息的線程:
long WINAPI Listen(CServerView *pView){ char msg[2000]=""; intnchar; SOCKADDR_IN saClnt; int saClntLen; while(1) { saClntLen=sizeof(saClnt); nchar=recvfrom(pView->m_sock,msg,1024,0,(PSOCKADDR)&saClnt,&saClntLen); if(nchar<0) { pView->m_data[pView->count ] ="Error in recvfrom\n"; pView->InvalidateRect(NULL); } else { switch(msg[0]) { case'A': wsprintf(msg,"A: Client from %s attached\n",inet_ntoa(saClnt.sin_addr)); pView->m_data[pView->count ]=msg; pView->flag=TRUE; pView->InvalidateRect(NULL); pView->Isconnect=TRUE; pView->saClnt=saClnt; pView->saClntLen=saClntLen; sendto(pView->m_sock,msg,1024,0,(PSOCKADDR)&saClnt,saClntLen); break; case 'D': wsprintf(msg,"D: Client form %s detached\n",inet_ntoa(saClnt.sin_addr)); pView->m_data[pView->count ]=msg; pView->flag=TRUE; pView->InvalidateRect(NULL); pView->Isconnect=FALSE; sendto(pView->m_sock,msg,1024,0,(PSOCKADDR)&saClnt,saClntLen); break; case 'R': saClntLen=sizeof(saClnt); pView->m_data[pView->count ]=msg; pView->flag=TRUE; pView->InvalidateRect(NULL); break; default: break; } } } return(0);} 6.在程序菜單項中添加"本機IP地址":
void CServerView::OnIp() { int WSAReturn; WSAReturn=WSAStartup( 0x0101, &WSAData ); if( WSAReturn == 0 ){ gethostname( Hostname, 260 ); struct hostent *pHostEnt; pHostEnt = gethostbyname( Hostname); if( pHostEnt != NULL ){ wsprintf( Hostaddress, "%d.%d.%d.%d", ( pHostEnt->h_addr_list[0][0] & 0x00ff ), ( pHostEnt->h_addr_list[0][1] & 0x00ff ), ( pHostEnt->h_addr_list[0][2] & 0x00ff ), ( pHostEnt->h_addr_list[0][3] & 0x00ff ) ); CString out; out.Format(Hostaddress); AfxMessageBox(out); } }} 7.在程序菜單中添加"發(fā)送消息":
void CServerView::OnSendmessage() { // TODO: Add your command handler code here char msg[2000]; Csend Sendmessage; if(Sendmessage.DoModal() ==IDOK&&!Sendmessage.m_Message.IsEmpty()) { wsprintf(msg,"R: " Sendmessage.m_Message); sendto(m_sock,msg,1024,0,(PSOCKADDR)&saClnt,saClntLen); m_data[count ]=Sendmessage.m_Message; flag=TRUE; InvalidateRect(NULL); }} 8.為發(fā)送消息項添加一個對話框的類,名為send,有一個文本框,用來發(fā)送消息.并為文本框添加CString m_Message 變量,并在ServerView.cpp中添加#include "send.h" 9.為發(fā)送消息項添加一個判斷函數(shù):
void CServerView::OnUpdateSendmessage(CCmdUI* pCmdUI) { // TODO: Add your command update UI handler code here pCmdUI->Enable(FALSE); if(Isconnect) pCmdUI->Enable(TRUE);} 10.再窗口顯示消息:
void CServerView::OnDraw(CDC* pDC){ if(flag) { sizeTotal.cy =20; for(int j=65;jTextOut(10,y,m_data[i]); y =20; } // TODO: add draw code for native data here} 11.在Project中點擊Settings中選擇Link項添加wsock32.lib. 最后編譯程序,就可以得到Server.exe程序. 共2頁。 1 2 :
下載本文示例代碼
用VC編寫C/S消息傳送程序用VC編寫C/S消息傳送程序用VC編寫C/S消息傳送程序用VC編寫C/S消息傳送程序用VC編寫C/S消息傳送程序用VC編寫C/S消息傳送程序用VC編寫C/S消息傳送程序用VC編寫C/S消息傳送程序用VC編寫C/S消息傳送程序用VC編寫C/S消息傳送程序用VC編寫C/S消息傳送程序用VC編寫C/S消息傳送程序用VC編寫C/S消息傳送程序用VC編寫C/S消息傳送程序用VC編寫C/S消息傳送程序
閱讀(42) | 評論(0) | 轉(zhuǎn)發(fā)(0) |
與50位技術(shù)專家面對面20年技術(shù)見證,附贈技術(shù)全景圖總結(jié)
以上是生活随笔為你收集整理的vc下c语言网络编程,用VC编写C/S消息传送程序的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux下pwn从入门到放弃,pwn从
- 下一篇: c语言font6x8,爱字体下载安卓版-