Windows Socket Chat Program Description Report

xiaoxiao2021-03-06  45

Source File:

Main program: Winsocket main.h http://blog.9cbs.net/huyoo/archive/2004/12/02/201380.aspx

Dialog Failure File and ID Definition: Winsocket RC http://blog.9cbs.net/huyoo/archive/2004/12/201379.aspx

Main program: winsocket main.cpp http://blog.9cbs.net/huyoo/archive/2004/12/02/201378.aspx

Socket chat program description report

Keywords: Windows Socket Programming, Chat Program, WIN32 API Program Summary: This program uses C language and Win32 API programming, using the Windows Socket Function Library to complete the basic functions of the chat program. You can mess with messages and have chat records. Directory 1, program run 2, chat program implementation principle brief 3, summary ---------------------------------- ---- 1 Program runs the assembly server side and the client to run two instances, one to do server side, one to do client. After starting, the initial interface is as follows 1:

Figure 1, the start interface then starts the server side, listening; start the client, connect the server. After both establish a connection, you can send a message to each other. The start operation is as follows:

Figure 2, starting C / S

Mutual messages, as shown in Figure 3, 4:

Figure 3, 4, mutual message

2 Chat Program Implementation Principle Since the program is the server side and the client is integrated, I added a BOOL Server variable to determine whether the server is the server, or the client. Click on the menu "File" -> "Start Server", then server = true, indicating that the program instance is the server side; in turn, Server = false, indicating that the program instance is the client. Program dialog In the WM_INITDIALOG message response using WSAStartup initialization socket

IF (WSASTARTUP (Winsock_verion, & ws)) {MessageBox (HWND, "Winsock Initialization Failure", Szdlgtitle, MB_OK | MB_ICONSTOP); WSACLEANUP (); Return False;} // Initialization

Release Socket in the WM_CLOSE message response:

if (connected_skt = INVALID_SOCKET!) {closesocket (connected_skt);} if (! skt = INVALID_SOCKET) {closesocket (skt);} if (! WSACleanup () = 0) {MessageBox (hwnd, "can not be released Winsocket", szDlgTitle, MB_OK);

2.1 Server-side listening, call the CreateServer (HWND) function after listening to the monitor. Because Socket has been initialized, you create a socket: SKT = Socket (AF_INET, SOCK_STREAM, IPPROTO_TCP); create success, then bind the created socket: bind (SKT, (SockAddr *) & addr, sizeof (addr); After the binding is successful, listen to this socket: listen (SKT, MAX_CONNECTED_NUM); after the listening is successful, start the connection event of the listener client: if (Wsaasyncselect (SKT, HWND, Socketmsg, FD_ACCEPT) == Socket_ERROR) {MessageBox (hwnd, "Wsaasyncselect () failed", szdlgtitle, mb_ok); Return False;}

When there is a client connection, the WSAAsyncselect (SKT, HWND, Socketmsg, fd_accept) function intercepts this event, the function sends a message to the program Socketmsg (this is a custom message), then the program handles this news: Has a customer The end connection accepts the connection and creates a new Socket: Connected_skt for communication with the client. It originally created Socket - SKT continues to listen to whether there is any client connection event.

if ((connected_skt = accept (skt, (struct sockaddr *) & clientaddr, & Len)) == INVALID_SOCKET) {MessageBox (hwnd, "accept client Socket connection failed", szDlgTitle, MB_OK); return FALSE;} SetDlgItemText (hwnd, IDC_REVTXT, "Acceptable Client Connection"); / / Connect, then listen to the client's fd_read and close WSaasyncselect (Connected_SKT, HWND, Socketmsg, fd_read | fd_close);

After accepting the connection, you can mess up with the client. 2.2 Client Connection After pressing the Connection Server, create the client using the CreateClient (HWND) function and connect to the server. First create a socket, then connect to the server, then filter the Socket to filter selection, shut down, and connect events: WSaasyncSelect (SKT, HWND, Socketmsg, fd_read | fd_close | fd_connect). Then you can mess with the server. 3 Summary Through the writing of this chat program, basically understand the principle of Socket's CS architecture, the communication process is also clear, and the stability of the TCP / IP connection is to learn WinSock programming. The future work is to increase multithreading processing, add multiple users to realize real multiplayers. It can also increase the functionality that file mutual transmission, voice and video chat. Another problem is to optimize the code, improve the performance efficiency, use more fault tolerance.

Author: huzi Web CreateTime: 2005-01-02T13: 18: 00 Pages: 5 Words: 344 Characters: 1966 CharacterswithSpaces: 2306

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

New Post(0)