Program for transmitting messages with vc6.0 encoding server and client

Source: Internet
Author: User
Tags htons
Program for transmitting messages with vc6.0 encoding server and client

----

Zhu Xiaohua, Nanjing University of Technology

---- Network Programming has become a fashion, and networks with TCP/IP protocols are more popular. compile a program for sending messages between the server and the client to increase your network programming experience. next I will introduce my program.

---- First introduce the server program:

---- 1. Create a project named "server", single document interface.

---- 2. Add the code to serverview. h:

# Include "Winsock. H "add variable: csize sizetotal; // control the scroll bar intcount; // number of information entries cstring m_data [1000]; // The information is stored in char hostname [260]; char hostaddress [20]; // host IP address socket m_sock; handle m_hlistenthread; // thread bool m_binitialized; // whether to initialize wsadatawsadata; bool flag; sockaddr_in saclnt; int saclntlen; bool isconnect; // whether to connect

----

3. Reload the cserverview () constructor in serverview. cpp to create and bind nested words:

CServerView::CServerView(){// TODO: add construction code hereIsconnect=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....";}elsem_data[4]+="Failed";} 

----

4. Complete the necessary clearing operations in the Destructor:

CServerView::~CServerView(){if(m_bInitialized)WSACleanup();closesocket(m_sock);if(m_hListenThread)::TerminateThread(m_hListenThread,0);}

----

5. Define the thread for receiving and processing messages:

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. Add "local IP Address" in the program menu ":

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. Add "Send message" in the program menu ":

void CServerView::OnSendmessage() {// TODO: Add your command handler code herechar 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. add a dialog box class for sending message items, named send, with a text box used to send messages. add the cstring m_message variable to the text box and go to serverview. add # include "send. h"

---- 9. Add a judgment function for the message sending item:

void CServerView::OnUpdateSendmessage(CCmdUI* pCmdUI) {// TODO: Add your command update UI handler code herepCmdUI->Enable(FALSE);if(Isconnect)pCmdUI->Enable(TRUE);}

----

10. The message is displayed in the window:

void CServerView::OnDraw(CDC* pDC){if(flag){sizeTotal.cy+=20;for(int j=65;j
 
  TextOut(10,y,m_data[i]);y+=20;}// TODO: add draw code for native data here}
 

----

11. Click "Settings" in the project and select "Link" to add wsock32.lib. the final compilation program will be available to the server.exe program.

---- Introduce the client program now:

---- 1. Create a project named "client", single document interface.

---- 2. Add the code in clientview. h:

# Include "Winsock. H" to add variables: cstring m_data [1000]; handle m_hlistenthread; socket m_sock; sockaddr_in m_sasrvr; boolisconnect; int count; csize sizetotal; boolflag;

----

3. initialize the variable in the constructor:

CClientView::CClientView(){// TODO: add construction code hereIsconnect=FALSE;sizeTotal.cy=350;sizeTotal.cx=300;flag=FALSE;}

----

4. Complete the clearing operation in the Destructor:

CClientView::~CClientView(){if(m_bInitialized)WSACleanup();closesocket(m_sock);if(m_hListenThread)::TerminateThread(m_hListenThread,0);}

----

5. Add the "dial" item to the menu:

void CClientView::OnDial() {// TODO: Add your command handler code herecount=5;if(m_bInitialized){AfxMessageBox("Already dialing");return;}Cdial dial;if(dial.DoModal()==IDOK&&!dial.m_HostAddress.IsEmpty()){m_saSrvr.sin_family=AF_INET;m_saSrvr.sin_addr.S_un.S_addr=htonl(INADDR_ANY);m_saSrvr.sin_addr.S_un.S_addr=inet_addr(dial.m_HostAddress);m_saSrvr.sin_port=htons(5050);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(0);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]+="Waiting....";}elsem_data[4]+="Failed";InvalidateRect(NULL);}}

----

6. Add a dialing dialog box named dial. There is a text box used to write IP addresses and add code in clientview. cpp:

#include dial.h

----

7. Add a judgment function to the dial-up item:

void CClientView::OnUpdateDial(CCmdUI* pCmdUI) {// TODO: Add your command update UI handler code herepCmdUI->Enable(TRUE);if(Isconnect)pCmdUI->Enable(FALSE);}

----

8. Add the thread for receiving and sending messages:

long WINAPI Listen(CClientView *pView){char msg[2000];pView- >m_data[5]="Sending ATTACH command";pView- >InvalidateRect(NULL);wsprintf(msg,"A: ");sendto(pView- >m_sock,msg,1024,0,(PSOCKADDR)&pView- >m_saSrvr,sizeof(pView- >m_saSrvr));int saSrvrLen ,nchar;while(1){saSrvrLen=sizeof(pView->m_saSrvr);nchar=recvfrom(pView- >m_sock,msg,1024,0,(PSOCKADDR)&pView- >m_saSrvr,&saSrvrLen);if(nchar<0) { pView->m_data[pView- >count++]="Error in recvform";pView- >InvalidateRect(NULL);}else{pView->m_data[pView- >count++]=msg;pView->Isconnect=TRUE;pView->flag=TRUE;pView->InvalidateRect(NULL);}}return(0);}

----

9. Make a message sending item like the main program. The Code is as above.

---- 10. The display program is the same as the main program, and the code is as above.

---- 11. Click settings in the project and select link to add wsock32.lib.

---- 12.compile the program to access the client.exe program.

---- After server.exe and client.exe are completed, they can be executed in a network with TCP/IP protocol. through the above example, you can quickly understand the advantages of VC ++ network programming. You can also add other functions, which I will not discuss here. I hope that my program can serve as an example, so that we can compile a good network program.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.