A tcp and UPD chat and file transfer program

Source: Internet
Author: User

A tcp and UPD chat and file transfer program

Author: Hebei ca Wang xuesong

Download source code

Abstract:

This article uses examples to show how to chat, send files to each other, and capture the screen of the recipient to save as a file in the LAN. The program also includes obtaining the IP address and computer name of the Local Machine and the other party. The first time I published an article on functions such as the rolling ListBox control, I just remembered the efficacy of throwing a brick and mortar. errors are inevitable. Please don't take it for me. Thank you.

The running interface is as follows:

1. Start and terminate the listening thread (only about TCP)

This program can be switched instantly, so it is necessary to end the thread that is still in the accept state.
// TCP listening thread

Uint _ listentcpthread (lpvoid lparam) {cmyqqdlg * pdlg = (cmyqqdlg *) lparam; csocket socksrvr; pdlg-> m_potr = port + pdlg-> m_server; // Save the current port, used to disable int createsucceed = socksrvr. create (pdlg-> m_potr); int listensucceed = socksrvr. listen (); // start listening to csocket recso; sockaddr_in client; int iaddrsize = sizeof (client); int acceptsucceed = socksrvr. accept (recso, (sockaddr *) & client, & iaddrsize); // accept the connection and obtain the peer ipsocksrvr. close (); char flag [F Lag] = {0}; // acceptance mark, used to identify received information, files, and other if (recso. Receive (flag, flag )! = 2) {return-1;} pdlg-> m_type = Flag [0]; If (pdlg-> m_type = ''d') return 0; // terminate this thread pthreadlisen =: afxbeginthread (_ listentcpthread, pdlg); pdlg-> receivefilemsg (recso, client); Return 0 ;}

Close

If (m_nsocktype = sock_tcp) {DWORD dwstatus; If (pthreadlisen! = NULL) {If (: getexitcodethread (pthreadlisen-> m_hthread, & dwstatus) = 0) {int errror = getlasterror (); return;} If (dwstatus = still_active) {csocket sockclient; sockclient. create (); cstring IP, strerror; IP = "127.0.0.1"; int conn = sockclient. connect (IP, m_potr); If (conn = 0) {afxmessagebox ("Close error! "+ Geterror (getlasterror (); sockclient. shutdown (2); sockclient. close (); return;} sockclient. send ("D", flag); // end }}

2. Send and accept files. Relatively simple. See the source program. Send

For (;) {num = myfile. read (m_buf, sizefile); If (num = 0) break; // After the file is read, end = sockclient. send (m_buf, num );}

Accept

For (;) {n = recso. receive (BUF, sizefile); // accept if (n = 0) break; // 0 indicates the end of F. write (BUF, n );}

3. For more information about chatting, see the source program.

Else if (m_type = 'm'') // information {char buff [100] = {0}; cstring MSG; int ret = 0; (;;) {ret = recso. receive (buff, 100); If (ret = 0) break; MSG + = Buff;} cstring Strout, strin; m_you_ip.getwindowtext (strin); getnamebyaddress (strin, Strout ); cstring youname; youname. format (inet_ntoa (client. sin_addr); cstring STR = youname + "<-" + Strout; addmsglist (STR, MSG );}

4. There are many examples in the VC knowledge base for screen capture. Tested on vc6 + Win2000.

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.