_NETWORKT COMMUNCIATIONS USING NETBEUI PROTOCOL_ by Marshall Brain Listing One //******************************************************************* // sms_recv.cpp -- this creates a mailslot server and reads from it. // The mailslot receiver uses polling. By Marshall Brain. //******************************************************************* #include #include int main() { char toDisptxt[80]; HANDLE hSMS_Slot; DWORD nextSize,Msgs,NumBytesRead; BOOL Status; /* Create a mailslot for receiving messages */ hSMS_Slot=CreateMailslot("\\\\.\\mailslot\\sms", 0, 0, (LPSECURITY_ATTRIBUTES) NULL); /* Check and see if the mailslot was created */ if (hSMS_Slot == INVALID_HANDLE_VALUE) { cerr << "ERROR: Unable to create mailslot " << GetLastError() << endl; return (1); } /* Repeatedly check for messages until the program is terminated */ while(1) { Status=GetMailslotInfo(hSMS_Slot, (LPDWORD) NULL, &nextSize, &Msgs,(LPDWORD) NULL); if (!Status) { cerr << "ERROR: Unable to get status. " << GetLastError() << endl; CloseHandle(hSMS_Slot); return (1); } if (Msgs) /* If messages are available, then get them */ { /* Read the message and check if read was successful */ if (!ReadFile(hSMS_Slot, toDisptxt, nextSize, &NumBytesRead, (LPOVERLAPPED) NULL)) { cerr << "ERROR: Unable to read from mailslot " << GetLastError() << endl; CloseHandle(hSMS_Slot); return (1); } cout << toDisptxt << endl;/* Display the Message */ } else Sleep(500); /* Check for new messages twice a second */ } /* while */ } Listing Two //*************************************************************** // sms_send.c -- a simple mailslot sender that writes to // a mailslot every five seconds. By Marshall Brain. //*************************************************************** #include #include #include int main() { char toSendTxt[100], buffer[100]; DWORD bufferLen=100, NumBytesWritten; HANDLE hSMS_Slot; BOOL Status; /* Create the mailslot file handle for sending messages */ hSMS_Slot=CreateFile("\\\\*\\mailslot\\sms", GENERIC_WRITE, FILE_SHARE_READ, (LPSECURITY_ATTRIBUTES) NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,(HANDLE)NULL); /* if the mailslot file was not opened, terminate program */ if (hSMS_Slot == INVALID_HANDLE_VALUE) { cerr << "ERROR: Unable to create mailslot " << GetLastError() << endl; return (1); } GetComputerName(buffer, &bufferLen); /* form string to send */ strcpy(toSendTxt, "Test string from "); strcat(toSendTxt, buffer); /* Repeatedly send message until program is terminated */ while(1) { cout << "Sending..." << endl; /* Write message to mailslot */ Status=WriteFile(hSMS_Slot, toSendTxt, (DWORD) strlen(toSendTxt)+1, &NumBytesWritten, (LPOVERLAPPED) NULL); /* If error occurs when writing to mailslot,terminate program */ if (!Status) { cerr << "ERROR: Unable to write to mailslot " << GetLastError() << endl; CloseHandle(hSMS_Slot); return (1); } Sleep(4800); /* Wait sending the message again */ } /* while*/ } Listing Three //**************************************************************************** // ssnprecv.cpp --- a simple named pipe server (receiver). The server will wait // and accept one connection, then receive messages from it. By Marshall Brain. //**************************************************************************** #include #include int main() { char toDisptxt[80]; HANDLE ssnpPipe; DWORD NumBytesRead; /* Create a named pipe for receiving messages */ ssnpPipe=CreateNamedPipe("\\\\.\\pipe\\ssnp", PIPE_ACCESS_INBOUND, PIPE_TYPE_MESSAGE | PIPE_WAIT, 1, 0, 0, 150, (LPSECURITY_ATTRIBUTES) NULL); /* if named pipe was not created, terminate */ if (ssnpPipe == INVALID_HANDLE_VALUE) { cerr << "ERROR: Unable to create a named pipe. " << endl; return (1); } cout << "Waiting for connection... " << endl; /* Allow a client to connect to the pipe, terminate if unsuccessful */ if(!ConnectNamedPipe(ssnpPipe, (LPOVERLAPPED) NULL)) { cerr << "ERROR: Unable to connect a named pipe " << GetLastError() << endl; CloseHandle(ssnpPipe); return (1); } /*Repeatedly check for messages until the program is terminated. */ while(1) { /* Read the message and check to see if read was successful */ if (!ReadFile(ssnpPipe, toDisptxt, sizeof(toDisptxt), &NumBytesRead, (LPOVERLAPPED) NULL)) { cerr << "ERROR: Unable to read from named pipe " << GetLastError() << endl; CloseHandle(ssnpPipe); return (1); } cout << toDisptxt << endl;/* Display the Message */ } /* while */ } Listing Four //*************************************************************** // ssnpsend.cpp -- a simple named pipe sender. // This connects to receiver (ssnprecv) and sends it messages. //*************************************************************** #include #include int main() { char *toSendtxt="Test String"; HANDLE ssnpPipe; DWORD NumBytesWritten; char machineName[80],pipeName[80]; cout << "Enter name of server machine: "; cin >> machineName; wsprintf(pipeName, "\\\\%s\\pipe\\ssnp", machineName); /* Create the named pipe file handle for sending messages */ ssnpPipe=CreateFile(pipeName, GENERIC_WRITE, FILE_SHARE_READ, (LPSECURITY_ATTRIBUTES) NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, (HANDLE) NULL); /* If the named pipe file was not opened, terminate program */ if (ssnpPipe == INVALID_HANDLE_VALUE) { cerr << "ERROR: Unable to create a named pipe " << endl; cerr << GetLastError() << endl; return (1); } /* Repeatedly send message until program is terminated */ while(1) { cout << "Sending..." << endl; /* Write message to the pipe */ if (!WriteFile(ssnpPipe, toSendtxt, (DWORD) strlen(toSendtxt)+1, &NumBytesWritten, (LPOVERLAPPED) NULL)) { /* If an error occurs when writing to pipe, terminate program */ cerr << "ERROR: Unable to write to named pipe " << GetLastError() << endl; CloseHandle(ssnpPipe); return (1); } Sleep(4800);/* Wait before sending the message again */ } /* while*/ }