我想用 ci 中的 ipc 制作一个 x&0 游戏,声明 3 个字符数组,读取要选择的女巫线以及放置 x 或 0 的位置。当我尝试从客户端通过 fifo 发送行号时服务器收到一个不同的数字。例如,我发送 1 并且服务器得到 3144200 ....这是代码,它不仅仅是一次读写。这是服务器:
#include <windows.h>
#include <stdio.h>
#include <string.h>
#include <iostream>
int main() {
HANDLE f1, f2;
DWORD x;
char l1[3]="\0";
char l2[3]="\0";
char l3[3]="\0";
char X='x';
char* a="";
char* lineChar="";
int n=1,lineInt=0,coor=0;
printf("I am the server \n");
printf("You will play with x\n");
// creating pipes
f1=CreateNamedPipe(TEXT("\\\\.\\PIPE\\fifo1"), PIPE_ACCESS_INBOUND,PIPE_TYPE_BYTE|PIPE_WAIT, 3, 0, 0, 0, NULL);
f2=CreateNamedPipe(TEXT("\\\\.\\PIPE\\fifo2"), PIPE_ACCESS_OUTBOUND,PIPE_TYPE_BYTE|PIPE_WAIT, 3, 0, 0, 0, NULL);
ConnectNamedPipe(f1, NULL);
ConnectNamedPipe(f2, NULL);
while(n<=9){
for(int i=0;i<=2;i++){
printf("[%s]",&l1[i]);
}
printf("\n");
for(int i=0;i<=2;i++){
printf("[%s]",&l2[i]);
}
printf("\n");
for(int i=0;i<=2;i++){
printf("[%s]",&l3[i]);
}
printf("\n");
printf("You will begin,select the line from 1 to 3 : \n");
scanf_s("%d",&lineInt);
x=0;
lineChar=reinterpret_cast<char*>(lineInt);
printf("%s",lineChar);
//strcpy_s(a,sizeof(lineChar),lineChar);
if (WriteFile(f2,lineChar,sizeof(lineChar)+1, &x, NULL)==0) {
printf("writing error..%d\n", x);
}
n++;
}
DisconnectNamedPipe(f1);
DisconnectNamedPipe(f2);
CloseHandle(f1);
CloseHandle(f2);
}
这是客户:
#include <windows.h>
#include <stdio.h>
#include <string.h>
#include<stdlib.h>
int main(){
HANDLE f1, f2;
DWORD x;
char l1[3]="\0";
char l2[3]="\0";
char l3[3]="\0";
char o='0';
char* lineChar="";
int n=1,lineInt=0,coor=0;
printf("I am the client \n");
printf("You will play with 0\n");
// connect to pipes created by server
f1=CreateFile(TEXT("\\\\.\\PIPE\\fifo1"), GENERIC_WRITE, FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
f2=CreateFile(TEXT("\\\\.\\PIPE\\fifo2"), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
while(n<=9){
for(int i=0;i<=2;i++){
printf("[%s]",&l1[i]);
}
printf("\n");
for(int i=0;i<=2;i++){
printf("[%s]",&l2[i]);
}
printf("\n");
for(int i=0;i<=2;i++){
printf("[%s]",&l3[i]);
}
printf("\n");
x=0;
Sleep(3000);
if (ReadFile(f2, lineChar, sizeof(lineChar), &x, NULL)==0) {
printf("reading error..%d\n", x);
}
printf("%s",lineChar);
//lineInt=atoi(lineChar);
//printf("\n%d\n",lineInt);
n++;
}
CloseHandle(f1);
CloseHandle(f2);
}