Code/Resource
Windows Develop
Linux-Unix program
Internet-Socket-Network
Web Server
Browser Client
Ftp Server
Ftp Client
Browser Plugins
Proxy Server
Email Server
Email Client
WEB Mail
Firewall-Security
Telnet Server
Telnet Client
ICQ-IM-Chat
Search Engine
Sniffer Package capture
Remote Control
xml-soap-webservice
P2P
WEB(ASP,PHP,...)
TCP/IP Stack
SNMP
Grid Computing
SilverLight
DNS
Cluster Service
Network Security
Communication-Mobile
Game Program
Editor
Multimedia program
Graph program
Compiler program
Compress-Decompress algrithms
Crypt_Decrypt algrithms
Mathimatics-Numerical algorithms
MultiLanguage
Disk/Storage
Java Develop
assembly language
Applications
Other systems
Database system
Embeded-SCM Develop
FlashMX/Flex
source in ebook
Delphi VCL
OS Develop
MiddleWare
MPI
MacOS develop
LabView
ELanguage
Software/Tools
E-Books
Artical/Document
Client.c
Upload User: dq031136
Upload Date: 2022-08-08
Package Size: 802k
Code Size: 11k
Category:
Visual C++ Books
Development Platform:
C++ Builder
- #define STRICT
- #include <windows.h>
- #include "client.h"
- #include <string.h>
- #include <stdio.h>
- #include <stdlib.h>
- HANDLE hInst;
- HWND hWndClient;
- CHAR ShrName[LINE_LEN]; // Global: net share name.
- CHAR ClntName[NAME_SIZE]; // Global: user or pipe client name.
- CHAR lpBuffer[255]; // Global: buffer for string resources
- int APIENTRY WinMain (HINSTANCE hInstance,
- HINSTANCE hPrevInstance,
- LPSTR lpCmdLine,
- int nCmdShow)
- {
- DWORD retCode;
- UNREFERENCED_PARAMETER( nCmdShow );
- UNREFERENCED_PARAMETER( lpCmdLine );
- UNREFERENCED_PARAMETER( hPrevInstance );
- hInst = hInstance;
- retCode = DialogBox ((HANDLE)hInst, (LPCTSTR)"ClientDialog",
- NULL, (DLGPROC)ClientDlgProc);
- return (retCode);
- }
- LONG CALLBACK InitDlgProc (HWND hDlg, UINT wMsg, WPARAM wParam, LPARAM lParam)
- {
- UNREFERENCED_PARAMETER(lParam);
- switch (wMsg)
- {
- case WM_INITDIALOG:
- PostMessage (GetDlgItem (hDlg, IDD_SVREDIT),
- EM_LIMITTEXT, LINE_LEN, 0);
- PostMessage (GetDlgItem (hDlg, IDD_CLNTEDIT),
- EM_LIMITTEXT, NAME_SIZE, 0);
- case WM_COMMAND:
- switch (LOWORD(wParam))
- { // When the user clicks okay, get the
- case IDB_INITOK: // share name and user name from the
- // edit fields.
- GetWindowText (GetDlgItem (hDlg, IDD_SVREDIT), ShrName, LINE_LEN);
- GetWindowText (GetDlgItem (hDlg, IDD_CLNTEDIT), ClntName, NAME_SIZE);
- EndDialog(hDlg, 0);
- return (0);
- default:
- return (0);
- }
- default:
- return (0);
- }
- return (0);
- }
- LONG CALLBACK ClientDlgProc (HWND hDlg, UINT wMsg, WPARAM wParam, LPARAM lParam)
- {
- DWORD retCode; // Return code.
- CHAR errorBuf[LINE_LEN] = ""; // Error message buffer.
- CHAR outBuf[OUT_BUF_SIZE] = ""; // Buffer trapping message to send.
- CHAR sendBuf[OUT_BUF_SIZE] = ""; // Buffer used to modify message.
- DWORD bytesWritten; // Used for WriteFile().
- DWORD threadID; // Used for CreateThread().
- CHAR fileName[LINE_LEN+NAME_SIZE+2]; // Used to modify pipe/file name.
- DWORD lastError; // Used to get returns from GetLastError.
- static HANDLE hPipe; // File or Pipe handle.
- static OVERLAPPED OverLapWrt; // Overlapped structure
- static HANDLE hEventWrt; // Event handle for overlapped writes.
- UNREFERENCED_PARAMETER( lParam );
- hWndClient = hDlg;
- switch (wMsg)
- {
- case WM_COMMAND:
- switch (LOWORD(wParam))
- {
- case IDB_SEND: // Get the text from the edit field.
- GetWindowText (GetDlgItem(hDlg,IDD_EDITWRITE),
- outBuf, PLEASE_WRITE);
- // Prepend it with the user name, and
- // terminate it with a new line
- // character.
- wsprintf (sendBuf, "%s%s %sn", ClntName, ":", outBuf);
- // Do the overlapped write.
- retCode = WriteFile (hPipe, sendBuf, PLEASE_WRITE,
- &bytesWritten, &OverLapWrt);
- if (!retCode)
- {
- lastError = GetLastError();
- // If Error = IO_PENDING, wait til
- // the event signals success.
- if (lastError == ERROR_IO_PENDING)
- WaitForSingleObject (hEventWrt, (DWORD)-1);
- }
- return (0);
- default:
- return (0);
- }
- case WM_INITCLIENT:
- // Launch Init dialog box to capture
- // share name and user name.
- DialogBox ((HANDLE)GetModuleHandle(NULL),
- (LPCTSTR)"InitDialog",
- (HWND)hDlg,
- (DLGPROC)InitDlgProc);
- // Put captured user name in window
- // caption.
- SetWindowText (hDlg, ClntName);
- // Construct file/pipe name.
- wsprintf (fileName, "%s%s%s", "\\", ShrName, "\PIPE\test");
- // Do CreateFile() to connect to the
- // named pipe.
- hPipe = CreateFile (fileName, // Pipe name.
- GENERIC_WRITE // Generic access, read/write.
- | GENERIC_READ,
- FILE_SHARE_READ // Share both read and write.
- | FILE_SHARE_WRITE ,
- NULL, // No security.
- OPEN_EXISTING, // Fail if not existing.
- FILE_FLAG_OVERLAPPED, // Use overlap.
- NULL); // No template.
- // Do some error checking.
- if ((DWORD)hPipe == 0xFFFFFFFF)
- {
- retCode = GetLastError();
- // This error means pipe wasn't found.
- if ((retCode == ERROR_SEEK_ON_DEVICE) ||
- (retCode == ERROR_FILE_NOT_FOUND)) {
- LoadString(hInst, IDS_CANTFINDPIPE, lpBuffer, sizeof(lpBuffer));
- MessageBox (hDlg, lpBuffer, "", MB_OK);
- }
- else
- { // Flagging unknown errors.
- LoadString(hInst, IDS_GENERALERROR, lpBuffer, sizeof(lpBuffer));
- wsprintf (errorBuf, lpBuffer, retCode);
- LoadString(hInst, IDS_DEBUGTITLE, lpBuffer, sizeof(lpBuffer));
- MessageBox (hDlg, errorBuf, lpBuffer,
- MB_ICONINFORMATION | MB_OK | MB_APPLMODAL);
- }
- EndDialog (hDlg, 0); // Kill app if pipe didn't connect.
- };
- // Create and init overlapped structure
- // for writes.
- hEventWrt = CreateEvent (NULL, TRUE, FALSE, NULL);
- OverLapWrt.hEvent = hEventWrt;
- // Write the client name to server.
- retCode = WriteFile (hPipe, ClntName, PLEASE_WRITE,
- &bytesWritten, &OverLapWrt);
- if (!retCode) // Wait on overlapped if need be.
- {
- lastError = GetLastError();
- if (lastError == ERROR_IO_PENDING)
- WaitForSingleObject (hEventWrt, (DWORD)-1);
- }
- // Create a thread to read the pipe.
- CreateThread (NULL,
- 0,
- (LPTHREAD_START_ROUTINE)ReadPipe,
- (LPVOID)&hPipe,
- 0,
- &threadID);
- return (0);
- case WM_INITDIALOG:
- // PostMessage() give time for the
- // dialog box to be created.
- PostMessage (hDlg, WM_INITCLIENT, 0, 0);
- return (0);
- case WM_GO_AWAY:
- CloseHandle (hPipe);
- CloseHandle (hEventWrt);
- EndDialog (hDlg, TRUE);
- return TRUE;
- case WM_SYSCOMMAND:
- if (wParam == SC_CLOSE)
- {
- CloseHandle (hPipe);
- CloseHandle (hEventWrt);
- EndDialog(hDlg, TRUE);
- return TRUE;
- }
- break;
- }
- return (FALSE);
- }
- VOID ReadPipe (HANDLE *hPipe)
- {
- CHAR inBuf[IN_BUF_SIZE] = "";// Input buffer.
- DWORD bytesRead; // Used for ReadFile()
- DWORD retCode; // Used to trap return codes.
- CHAR Buf[80]; // Message box buffer.
- DWORD lastError; // Used to trap returns from GetLastError.
- HANDLE hEventRd; // Event handle for overlapped reads.
- OVERLAPPED OverLapRd; // Overlapped structure.
- DWORD bytesTrans; // Bytes transferred in read.
- // Create and init overlap structure.
- hEventRd = CreateEvent (NULL, TRUE, FALSE, NULL);
- memset (&OverLapRd, 0, sizeof(OVERLAPPED));
- OverLapRd.hEvent = hEventRd;
- do{
- // Read the pipe handle.
- retCode = ReadFile (*hPipe, inBuf, IN_BUF_SIZE, &bytesRead, &OverLapRd);
- if (!retCode) { // Do some error checking.
- lastError = GetLastError();
- // Check for 3 kinds of errors:
- // IO_PENDING, BROKEN_PIPE, or
- // other.
- // If Error = IO_PENDING, wait for
- // event handle to signal success.
- if (lastError == ERROR_IO_PENDING)
- {
- WaitForSingleObject (hEventRd, (DWORD)-1);
- }
- else { // If pipe is broken, tell user and break.
- if (lastError == (DWORD)ERROR_BROKEN_PIPE) {
- LoadString(hInst, IDS_CONNECTBROKEN, lpBuffer, sizeof(lpBuffer));
- MessageBox (hWndClient, lpBuffer, "", MB_OK);
- }
- else { // Or flag unknown errors, and break.
- LoadString(hInst, IDS_READFAILED, lpBuffer, sizeof(lpBuffer));
- wsprintf (Buf, lpBuffer, GetLastError());
- LoadString(hInst, IDS_CLIENTDBG, lpBuffer, sizeof(lpBuffer));
- MessageBox (hWndClient, Buf, lpBuffer, MB_OK);
- }
- break;
- }
- }
- // NULL terminate string.
- GetOverlappedResult (*hPipe, &OverLapRd, &bytesTrans, FALSE);
- inBuf[bytesTrans] = '';
- // Write message to larger edit field.
- SendMessage (GetDlgItem (hWndClient, IDD_EDITREAD),
- EM_REPLACESEL,
- 0, (LONG)inBuf);
- // Add a new line.
- SendMessage (GetDlgItem (hWndClient, IDD_EDITREAD),
- EM_REPLACESEL,
- 0, (LONG)"rn");
- }while(1);
- // When pipe is broken, send quit
- // messages to Client dialog box.
- PostMessage (hWndClient, WM_GO_AWAY, 0,0);
- ExitThread(0);
- }