Programs that use VC6.0 compilers to transfer messages with clients

zhaozj2021-02-11  206

Programs that use VC6.0 compilers to transfer messages with clients

---- Nanjing University of Science and Technology Zhu Xiaohua

---- Network programming has become a fashionable, more popular in TCP / IP protocols. Make a server with the client to transfer the message to each other in order to increase your own network programming. I will introduce me below. The program.

---- First introduce the server program:

---- 1. Create a project called "Server", single document interface.

---- 2. Add code in ServerView.h:

#include "winsock.h"

Add variables:

CSIZE SIZETAl; / / Control Scroll Bar

INTCOUNT; // information

CString M_Data [1000]; // Information storage

Char Hostname [260];

Char Hostaddress [20]; // host IP address

Socket m_sock;

Handle m_hlistenthread; // thread

BOOL M_BINITIALIZED; / / Whether is initialized

WSADATAWSADATA;

Bool flag;

SockAddr_in saclnt;

INT SACLNTLEN;

Bool isconnect; // is connected

---- 3. Overload the CSerView () constructor in ServerView.cpp, create and bind the nested word:

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_SockAddr) & 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. Complete the required clearing operation in the destructor:

CSERVERVIEW :: ~ CSERVERVIEW ()

{

IF (m_binitialize)

WSACLEANUP ();

CloseSocket (M_SOCK);

IF (m_hlistenthread)

:: TerminateThread (M_HlistentHread, 0);

}

---- 5. Define threads that receive and process 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-> InvalidateERECT (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-> InvalidateERECT (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-> InvalidateERECT (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 "Native IP Address" in the program menu item:

Void CSerView :: 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 Messages" in the Program menu:

Void CSerView :: 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. To send a message item, add a dialog box, named Send, have a text box, used to send a message. Add the cstring m_message variable for the text box, and add a #include in ServerView.cpp. " Send.h "

---- 9. Add a judgment function to the send message item:

Void CserveView :: onupdatesendMessage

(CCMDUI * PCMDUI)

{

// Todo: add your command update

Ui Handler Code Here

PCMDUI-> Enable (false);

IF (Isconnect)

PCMDUI-> Enable (TRUE);

}

---- 10. Re-window Display Message:

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 in Settings in Project to select the LINK item to add WSOCK32.LIB. Last compiler, you can get a server.exe program. --- Now introduce the client program:

---- 1. Create a project called "Client", single document interface.

---- 2. Add code in ClientView.h:

#include "winsock.h"

Add variables:

CString M_Data [1000];

Handle M_Hlistenthread;

Socket m_sock;

SockAddr_in m_sasrvr;

Boolisconnect;

INT country;

CSIZE SIZETAl;

Boolflag;

---- 3. Initialization variables in the constructor:

CClientView :: CClientView ()

{

// Todo: Add Construction Code Here

ISCONNECT = FALSE;

Sizetotal.cy = 350;

Sizetotal.cx = 300;

Flag = false;

}

---- 4. Complete the clear operation in the destructor:

CClientView :: ~ cclientview ()

{IF (m_binitialized)

WSACLEANUP ();

CloseSocket (M_SOCK);

IF (m_hlistenthread)

:: TerminateThread (M_HlistentHread, 0);

}

---- 5. Add a "dial" item in the menu:

Void cclientview :: overdial ()

{

// Todo: add your command handler code here

Count = 5;

IF (m_binitialize)

{

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_And);

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_SockAddr) & 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 ....";

}

Else

m_data [4] = "failed";

INVALIDATERECT (NULL);

}

}

---- 6. Add a dial-up dialog, named Dial, have a text box to write the IP address. Add code to the clientView.cpp:

#include dial.h

---- 7. Add a judgment function in the dial item:

Void CClientView :: onupdatedial

(CCMDUI * PCMDUI)

{

// Todo: add your command update

Ui Handler Code Here

PCMDUI-> Enable (TRUE);

IF (Isconnect)

PCMDUI-> Enable (false);

}

---- 8. Add a thread that receives and send messages:

Long WinAPI Listen (CClientView * PView)

{

Char msg [2000];

PView-> m_data [5] = "Sending attach command";

PView-> InvalidateERECT (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-> InvalidateERECT (NULL);

}

Else

{

PView-> m_data [pView-> count ] = msg;

PVIEW-> isconnect = TRUE;

PView-> flag = true;

PView-> InvalidateERECT (NULL);

}

}

Return (0);

}

---- 9. Do a send message item as the main program, the code is as above.

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

---- 12. The compiler can get the client.exe program.

---- Server.exe and Client.exe After completing, you can do it in the network under TCP / IP protocol. By the above example, you can quickly understand the advantages of VC network programming, you can also add other Function item, I don't have more narratives here. I hope that my program can play the purpose of throwing bricks, let us have a good network program.

转载请注明原文地址:https://www.9cbs.com/read-4031.html

New Post(0)