TCP/IP network communication program design

Source: Internet
Author: User
TCP/IP network communication program design

Party depeng, School of mathematics and computer science, Hubei University (430062) This article introduces the Object-Oriented Design Method for TCP/IP network applications, the program instances developed in Windows 95 Using MFC in Visual C ++ 4.2 are provided. 1 sockets and Winsock 95 Winsock 95 are developed based on UNIX sockets and Windows Sockets. Sockets was originally an API in version 4.3bsd UNIX designed for UNIX to support Internet communication. It uses the client-server mode communication mechanism, enable the network client and server to communicate with each other through sockets. Win dows sockets defines a Microsoft Windows network programming interface, it provides a BSD socket for Windows TCP/IP. Besides being fully compatible with 4.3bsd UNIX sockets, it also includes an extension file, A set of additional APIs are used to implement the windows (event-driven) programming style, while Winsock 95 is an interface designed for network applications in microso Windows 95. The TCP/IP protocol of Windows 95 in the Internet control domain defines the network programming specification of WinSock 95 and introduces many new features. The corresponding csock et class is provided in MFC to implement network communication. 2 sockets programming principle sockets supports both data stream sockets and datagram sockets. The following is a process diagram of using socket for communication and connection. In this example, 1 is a connection-oriented sequence chart, and 2 is a connectionless sequence chart. Figure 1 Figure 2 shows that the relationship between the customer and the server is asymmetric. For TCP c/s, the server starts first, and then starts a connection between the client and the server at a certain time point. Both the server and the customer must call socket () to establish a Socket socket, and then the server calls BIND () to bundle the socket with a local network address, and then call listen () make the socket in a passive ready-to-receive State and specify the length of its request queue. Then the server can call accept () to receive client connections. After opening the socket, the customer can call Conne CT () to establish a connection with the server. After the connection is established, the customer and the server can send and receive data through the connection. Finally, when data transmission is complete, both parties call closesocket () to close the socket. For UDP c/s, the customer does not establish a connection with the server, but only sends a datagram containing the server address to the server. Similarly, the server does not receive a connection from the client, but calls the recvfrom function to wait for data from the client. Based on the Protocol address and datagram returned by recvfrom, the server can send a response to the customer. 3 Winsock 95 Programming Method Use Visual C ++ 4.2 to implement network programming with MFC in Windows 95. It mainly uses the csocket class and the following member functions: (1) bool create (uint nsocketport = 0, int nsockettype = sock_stream, Long Leve Nt = fd_read | fd_write | fd_ood | fd_accept | fd_connect | fd_close |, lpctstr | lpszsocket address = NULL this function is used to establish a socket. (2) bool BIND (uint nsocketport, lpctstr lpszsocketaddess = NULL) This function is used to connect the socket port with the network address. (3) bool listen (INT nconnectionbacklog = 5) This function is used to wait for socket requests. (4) virtual bool accept (casyncsocket & rconnected socket, socket, sockaddr * lpsock ADDR = NULL, int * lpsock addrlen = NULL) this function is used to obtain the first connection request in the queue and establish a socket with the same features as the socket. (5) bool connect (lpctstr lpszhostaddress, uint nhostport) This function is used to initiate a request. Here, lpszhostaddress and nhostport are the network address and socket Port Number of the Request receiving process. (6) virtual void close () The function is used to close the socket. By using the above classes and member functions, you can design a suitable communication program: SERVER: Construct → creat → bind → listen → accept → send → close; client: constuct → creat → connect → receive → close. 4 program example we use visual c ++ 4.2 MFC to design a daytime cliont program in Windows 95 environment, the list is as follows: header 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; cedit lineedit; cbutten startbutton;}; Class pengapp: Public cwinapp {public: bool initinstance ();} source program client. CP P list: # include <afxwin. h> # include <Winsock. h> # include "head. H "const int nport = 13; pengapp theapp; main WND: Main WND () {If (! Create (null, "communication program", ws_overlappedwindow, rectdefaul t) afxabort ();} int mainwnd: oncreate (lpcreatestruct) {rect; setrect (& rect, 80, 50, 160,70); Create ("Host Name:", ws_child | ws_visible | ss_left, rect, this); 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 | V S_visible | expect, rect, this, I dm_start); Return 0;} expect (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 create failed", "rece ive", mb_ I (on) Stop); else timeclient. connect (straddr, nport); timeclient. receivefrom (csreceivetext, cscounts, lineedit. getwintext, nport) MessageBox (timeclient. csreceivetext); timeclient. close ();}

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.