TCPIP network communication program design

zhaozj2021-02-11  216

TCP / IP network communication program design

College of Mathematics and Computer Science, Hubei University (430062) Demodes

This article introduces the object-oriented design method for TCP / IP network applications, and gives it

Program instances developed in Visual C 4.2 MFC in the Windows 95 environment.

1 Sockets with Winsock 95

Winsock 95 is developed on the basis of UNIX Sockets and Windows Sockets

Come. Sockets is originally BSD 4.3BSD UNI for UNIX support Internet communication

The API in the X version, which uses the communication mechanism of the customer-server mode to make the network client and

Server party achieves links and data exchange between networks via Sockets; Win Dows SOC

The KETS description defines a network programming interface of Microsoft Windows, which is Window.

S TCP / IP provides a BSD-type socket, except for 4.3BSD UNIX Sockets.

Recliscount, also includes an expansion file, implementing Windows in a set of additional APIs (mini

Piece-driven programming style; Winsock 95 is in Microso FT Windows 95

The interface for web application design. Windows 95 TC in the Internet domain domain

The P / IP protocol defines the Winsock 95 network programming specification and dissolves in many new features. MFC

The corresponding CSOCK ET class is provided to enable network communication.

2 Sockets Programming Principles

Sockets simultaneously supports data streams SOCKETS and Datashers SOCKETS.

Below is a process block diagram of a communication connection using Socket. Fig. 1 is a facing

Timing diagram, Figure 2 is a timing chart without connection.

figure 1

figure 2

As can be seen from the figure, the relationship between the customer and the server is asymmetrical. For TCP C / S,

The server starts first, then starts the customer with the server to establish a connection at a certain moment. service

Both the server must start calling socket () to create a socket, and then serve

Call bind () to bundle the socket with a local network address, then call Liste

n () makes the socket in a passive ready-to-receive state, and specifies its request queue.

Degree, then the server can call accept () to receive the client connection. Customer opens the set

After the word, the connection can be established by calling the CONNE CT () and the server. After the connection is established,

Data can be sent and received between the client and the server. Finally, to be transmitted

End, the two parties call the CloseSocket () closing the socket. For UDP C / S, customers do not

Establish a connection with the server, but only send a server address to the server.

Datagram. Similarly, the server does not receive a connection from the client, just call the function RE

CVFROM, waiting for data from the client. The protocol address returned in accordance with RECVFROM and

Data report, the server can send a response to the customer.

3 Winsock 95 programming method

Using Visual C 4.2 with MFC to realize network programming in Windows 95, mainly

Use the CSocket class and its related member functions:

(1) BOOL CREATE (uint nsocketport = 0, int NSockettype = SOCK_

Stream, Long Leve NT = fd_read | fd_write | fd_ood | fd_accept | fd_con

NECT | fd_close |, lpctstr | lpszsocket address = null

This function is used to build a socket.

(2) BOOL BIND (Uint NSocketport, LPCTSTR LPSZSSOCKETDESS = N

ULL)

The function of this function is to connect the Socket port with the network address.

(3) BOOL Listen (int NConnectionbackLog = 5)

The function of this function is to wait for the Socket request.

(4) Virtual Bool Accept (CasyncSocket & rConnected Socket,

Socket, SockAddr * lpsock addr = null, int * lpsock addrlen = NULL)

The function of this function is to get the first connection request on the queue and create a one with SO.

Cket The same characteristics socket.

(5) Bool Connect (LPCTSTSTR LPSZHOSTADDRESS, UINT NHOSTPORT)

The function of this function is to make a request. Among them, lpszhostaddress and Nhostport

To accept the network address and socket port number of the request process.

(6) Virtual void close () The function of this function is to close the socket.

Use the above class and member functions, follow these steps to design appropriate communication

program:

Server: Construct → Creat → Bind → Listen → Accept → Send → CLO

SE;

CLIENT: Constuct → Creat → Connect → Receive → Close.

4 program example

We designed a dayt in the Windows 95 environment with Visual C 4.2.

The IME clion program, the list is as follows:

Head file head.h content:

#define IDM_STRAT 200

#define IDM_EDIT 200

Class MainWnd: Public CFrame WND

{public: mainwnd ();

AFX_MSG INT OnCreat (LPCReatestruct);

AFX_MSG Void OnStart (Void);

Declare_MESSAGE_MAP ();

PRIVATE: CSTATIC CSSTATIC;

Cedt lineedit;

CButten StartButton;

Class Pengapp: Public CWINAPP

{public: BOOL INITINSTANCE ();

Source program client.cpp list:

#include

#include

#include "head.h"

Const int nport = 13;

PENGAPP THEAPP;

Main wnd: main wnd ()

{if (! CREATE (NULL, "Communication Program", WS_OVERLAPPEDW

Indow, RectDefaul T)) AFXABORT ();

Int mainwnd: oncreate (lpcreatestruct)

{Rect Rect; SetRect (& RECT, 80, 50, 160, 70);

Create ("Host Name:", WS_CHILD | WS_VISible | SS_LEFT, RECT, THI

S);

SetRect (& RECT, 60, 80, 180, 100);

LineEdit.create (WS_CHILD | WS_VISIBLE | WS_DLGFRAME | ES_LEFT,

Rect, this, IDM_ED IT;

SetRect (& RECT, 100, 120, 140, 140);

StartButton, Create ("Start", WS_CHILD | VS_VISIBLE | BS_PUSHBU

Tton, RECT, THIS, I DM_START);

Return 0;}

Begin_Message_Map (Main Wnd, CFrameWnd)

ON_WM_CREATE ()

ON_BN_CLICKED (IDM_START, ONSTART)

END_MESSAGE_MAP ()

Bool Controlapp: InitInstance ()

{m_pmainwnd = new main wnd ();

m_pmainwnd → showwindow (m_ncmdshow);

M_PMainWnd → UpdateWindow ();

Return;}

Void main wnd: onstart (void)

{CSocket TimeClient;

IF (! AFXSocketinit ()) MessageBox ("WindowsSocket Initial

Failed! "," Receiv E ", MB_ICONSTOP);

IF (! timeclient.create ()) MessageBox ("ReceiveSocket Crea

TE failed "," Rece Ive ", MB_i (ON) STOP);

Else TimeClient.connect (straddr, nport);

TimeClient.ReceiveFrom (CSReceiveText, Cscounts, Lineedit.g

ETWINTEXT, NPORT)

MessageBox (TimeClient.csReceiveText);

TimeClient.close ();

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

New Post(0)