|
неименованные каналы.
#38599928
Ссылка:
Ссылка на сообщение:
Ссылка с названием темы:
Ссылка на профиль пользователя:
|
|
|
|
Здравствуйте!
Написал программку с неименованными каналами. Вроде все работает, но хотел бы понять правильное ли я все делаю.
Если несложно, прокомментируйте, пожалуйста.
1. Есть серверный процесс, который запускает двух клиентов и создает два канала. (Клиенты могут обмениваться строкой в обоих направлениях)
Вот код клиента:
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. 51. 52. 53. 54. 55. 56. 57. 58. 59. 60. 61. 62. 63.
#include <QCoreApplication>
#include <windows.h>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
SECURITY_ATTRIBUTES PipeSA = {sizeof (SECURITY_ATTRIBUTES), NULL, TRUE};
PROCESS_INFORMATION ProcInfo1, ProcInfo2;
STARTUPINFO StartInfoCh1, StartInfoCh2;
wchar_t procName1[] = L"..\\UntitledPipiWriter\\debug\\UntitledPipiWriter.exe";
wchar_t procName2[] = L"..\\UntitledPipeReader\\debug\\UntitledPipeReader.exe";
HANDLE hReadPipe, hWritePipe, hReadPipe1, hWritePipe1;
ZeroMemory(&StartInfoCh1, sizeof(StartInfoCh1));
ZeroMemory(&StartInfoCh2, sizeof(StartInfoCh2));
StartInfoCh1.cb = sizeof(StartInfoCh1);
StartInfoCh2.cb = sizeof(StartInfoCh2);
CreatePipe(&hReadPipe, &hWritePipe, &PipeSA, 0);
StartInfoCh1.hStdOutput = hWritePipe;
StartInfoCh2.hStdInput = hReadPipe;
CreatePipe(&hReadPipe1, &hWritePipe1, &PipeSA, 0);
StartInfoCh1.hStdInput = hReadPipe1;
StartInfoCh2.hStdOutput = hWritePipe1;
StartInfoCh2.dwFlags = STARTF_USESTDHANDLES;
StartInfoCh1.dwFlags = STARTF_USESTDHANDLES;
CreateProcess(0,
procName1,
0,
0,
TRUE,
0,
0,
0,
&StartInfoCh1,
&ProcInfo1);
CloseHandle(ProcInfo1.hThread);
CloseHandle(hWritePipe);
CreateProcess(0,
procName2,
0,
0,
TRUE,
0,
0,
0,
&StartInfoCh2,
&ProcInfo2);
CloseHandle(ProcInfo2.hThread);
CloseHandle(hReadPipe);
WaitForSingleObject(ProcInfo1.hProcess, INFINITE);
CloseHandle(ProcInfo1.hProcess);
WaitForSingleObject(ProcInfo2.hProcess, INFINITE);
CloseHandle(ProcInfo2.hProcess);
return a.exec();
}
2. Первый клиент(пишет / читает):
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44.
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "windows.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_Write_clicked()
{
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
DWORD dword;
bool bSuccess;
char outBuf[100];
QString qstr = ui->lineEdit->text();
strcpy(outBuf, qstr.toLocal8Bit().data());
bSuccess = WriteFile(hOut, outBuf, sizeof(outBuf), &dword, NULL);
}
void MainWindow::on_Read_clicked()
{
bool bSuccess;
char inBuf[100];
HANDLE hRead = GetStdHandle(STD_INPUT_HANDLE);
DWORD dword;
bSuccess = ReadFile(hRead, inBuf, sizeof(inBuf), &dword, NULL);
if (bSuccess){
inBuf[dword] = '\0';
QString qstr;
qstr = QString::fromUtf8(inBuf);
ui->label->setText(qstr);
}
}
Второй клиент (читает / пишет)
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44.
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <windows.h>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_Read_clicked()
{
bool bSuccess;
char inBuf[100];
HANDLE hRead = GetStdHandle(STD_INPUT_HANDLE);
DWORD dword;
bSuccess = ReadFile(hRead, inBuf, sizeof(inBuf), &dword, NULL);
if (bSuccess){
inBuf[dword] = '\0';
QString qstr;
qstr = QString::fromUtf8(inBuf);
ui->label->setText(qstr);
}
}
void MainWindow::on_Write_clicked()
{
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
DWORD dword;
bool bSuccess;
char outBuf[100];
QString qstr = ui->lineEdit->text();
strcpy(outBuf, qstr.toLocal8Bit().data());
bSuccess = WriteFile(hOut, outBuf, sizeof(outBuf), &dword, NULL);
}
Спасибо.
|
|
|