gpt4 book ai didi

c - 如果不操纵它如何改变值(value)?

转载 作者:行者123 更新时间:2023-11-30 20:59:05 25 4
gpt4 key购买 nike

服务器:

void send_list(sqlite3 *db, int sock) {

int Fwrite,Fread;
int id_order;
int bet2=0;
Booking info;
char query2[200];


Fread = FullRead(sock,&bet2,sizeof(bet2));

if(Fread <0)
{
perror("Error to Recive \n");
exit(1);
}



Fwrite = FullWrite(sockREP,&bet2,sizeof(bet2));

if(Fwrite < 0)
{
perror("Error to Send \n");
exit(1);
}

printf("bet2: %d\n",bet2 );

Fread = FullRead(sock,&info.NAME,sizeof(info));

if(Fread <0)
{
perror("Error to Recive \n");
exit(1);
}

Fread = FullRead(sock,&info.SURNAME,sizeof(info));

if(Fread <0)
{
perror("Error to Recive \n");
exit(1);
}

Fread = FullRead(sock,&info.COD_RIC,sizeof(info));

if(Fread <0)
{
perror("Error to Recive \n");
exit(1);
}



struct timeval time;
gettimeofday(&time, NULL);
srand((time.tv_sec*1000)+(time.tv_sec/1000));

id_order = rand() %100 + 1;


Fwrite = FullWrite(sockREP,&info.NAME,sizeof(info));

if(Fwrite < 0)
{
perror("Error to Send \n");
exit(1);
}

Fwrite = FullWrite(sockREP,&info.SURNAME,sizeof(info));

if(Fwrite < 0)
{
perror("Error to Send \n");
exit(1);
}

Fwrite = FullWrite(sockREP,&info.COD_RIC,sizeof(info));

if(Fwrite < 0)
{
perror("Error to Send \n");
exit(1);
}

Fwrite = FullWrite(sockREP,&id_order,sizeof(id_order));

if(Fwrite < 0)
{
perror("Error to Send \n");
exit(1);
}

printf("this bet2: %d\n",bet2 );


Fwrite = FullWrite(sock,&id_order,sizeof(id_order));

if(Fwrite < 0)
{
perror("Error to Send \n");
exit(1);
}
}

服务器2(SockREP):

void recv_list(sqlite3 *db, int sock) {

int Fwrite,Fread;
int id_order;
int bet2=0;
Booking info;


Fread = FullRead(sock,&bet2,sizeof(bet2));

if(Fread < 0)
{
perror("Error to Recive \n");
exit(1);
}

Fread = FullRead(sock,&info.NAME,sizeof(Booking));

if(Fread < 0)
{
perror("Error to Receive \n");
exit(1);
}

Fread = FullRead(sock,&info.SURNAME,sizeof(Booking));

if(Fread < 0)
{
perror("Error to Receive \n");
exit(1);
}

Fread = FullRead(sock,&info.COD_RIC,sizeof(Booking));

if(Fread < 0)
{
perror("Error to Receive \n");
exit(1);
}

Fread = FullRead(sock,&id_order,sizeof(id_order));

if(Fread < 0)
{
perror("Error to Send \n");
exit(1);
}
}

客户端( socks ):

void getMenu(int sock)
{
int Fwrite,Fread;
int id_order;
int bet2=0;
Booking info;


printf("Select Data: ");
scanf("%d",&bet2);

Fwrite = FullWrite (sock,&bet2,sizeof(bet2));

if(Fwrite < 0)
{
perror("Error to Send \n");
exit(1);
}


printf("Name: ");
scanf("%s",&info.NAME);

printf("Surname: ");
scanf("%s",&info.SURNAME);

printf("Medical Code: ");
scanf("%s",&info.COD_RIC);


Fwrite = FullWrite(sock,&info.NAME,sizeof(Booking));

if(Fwrite <0)
{
perror("Error to Recive \n");
exit(1);
}

Fwrite = FullWrite(sock,&info.SURNAME,sizeof(Booking));

if(Fwrite <0)
{
perror("Error to Recive \n");
exit(1);
}

Fwrite = FullWrite(sock,&info.COD_RIC,sizeof(Booking));

if(Fwrite <0)
{
perror("Error to Recive \n");
exit(1);
}



Fread = FullRead(sock,&id_order,sizeof(id_order));

if(Fread < 0)
{
perror("Error to Send \n");
exit(1);
}

}

我的问题是:

为什么在服务器中变量bet2在3次完整读取后改变值:

1) Fread = FullRead(sock,&info.NAME,sizeof(info));

2) Fread = FullRead(sock,&info.SURNAME,sizeof(info));

3) Fread = FullRead(sock,&info.COD_RIC,sizeof(info));

我尝试使用read(),但结果是相等的

FullRead函数是这样的:

ssize_t FullRead(int fd, void *buf, size_t count)
{
size_t nleft = count;
ssize_t nread;
while (nleft > 0)
{
if ( (nread = read(fd, buf, nleft)) < 0)
{
if (errno == EINTR)
continue;
else
exit(nread);
}
else if (nread == 0)
break;
nleft -= nread;
buf += nread;
}
buf = 0;
return (nleft);
}

FullWrite 函数是这样的:

ssize_t FullWrite(int fd, const void *buf, size_t count)
{
size_t nleft;
ssize_t nwritten;
nleft = count;
while (nleft > 0) { /* repeat until no left */
if ( (nwritten = write(fd, buf, nleft)) < 0) {
if (errno == EINTR) { /* if interrupted by system call */
continue; /* repeat the loop */
} else {
return(nwritten); /* otherwise exit with error */
}
}
nleft -= nwritten; /* set left to write */
buf +=nwritten; /* set pointer */
}
return (nleft);
}

最佳答案

如果我正确理解你的问题,你是在问各种变量(info.NAME、info.SURNAME、bet2等)的值如何改变,因为你的代码中没有任何内容实际上使用 = 运算符设置这些值.

答案是指针别名。例如,info.SURNAME 位于计算机内存中的特定位置。可以通过 & 运算符创建指向该位置的指针,例如void * ptr = &info.SURNAME

因此,当您像这样调用 FullRead 时:

Fread = FullRead(sock,&info.SURNAME,sizeof(Booking));

...您向 FullRead 函数提供有关您的 info.SURNAME 字符数组在内存中的位置的信息,以及该字符数组的大小(以字节为单位)。

有了这些信息,FullRead() 现在可以将新字节写入 char 数组。例如它可能会做这样的事情:

int FullRead(int sock, char * buf, int numBytesInBuf)
{
// the real code would read bytes from the network but
// to keep this example simple I'll just declare some dummy data
char dummyData[] = "blah blah blah";

int numBytesToCopy = std::min(numBytesInBuf, sizeof(dummyData));
for (int i=0; i<numBytesToCopy; i++) buf[i] = dummyData[i];
return numBytesToCopy;
}

...这会将数据写入 buf 指向的内存,也就是说,写入 info.SURNAME

鉴于 bet2 不是 char 数组而是 int,那么这将如何与 bet2 一起使用?

它的工作方式完全相同 - bet2 可能是一个 int,但它仍然在内存中具有一个位置,并且大小以字节为单位(通常为 4 或 8 字节,具体取决于您使用的计算机类型),所以同样的技巧也适用...FullRead()可以写入bet2所在的内存区域,从而通过更新其字节来改变bet2的值。

 Fread = FullRead(sock,&bet2,sizeof(bet2));  // writes new value into bet2!

关于c - 如果不操纵它如何改变值(value)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48675603/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com