gpt4 book ai didi

c - C 中的静态变量

转载 作者:行者123 更新时间:2023-11-30 18:24:57 25 4
gpt4 key购买 nike

我有这个程序,它是具有 tcp 协议(protocol)的服务器的一部分,它从客户端获取一个数字并将其用于另一个函数。另外,我有一个 static int 类型变量,我想在服务器每次从客户端收到消息时对其进行计数,但每次它都不会保留其值;变量是counter你们能告诉我为什么会发生这种情况吗?

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <signal.h>
#include <errno.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

// used port
#define PORT 1114

// error code
extern int errno;

static int counter;

int main ()
{
struct sockaddr_in server; // structure used by server
struct sockaddr_in from;
char message[100]; //message received from client
int socketDescriptor; //socket descriptor


//creating socket
if ((socketDescriptor = socket (AF_INET, SOCK_STREAM, 0)) == -1)
{
perror ("[server]Error at socket\n");
return errno;
}

//preparing data structures
bzero (&server, sizeof (server));
bzero (&from, sizeof (from));

//filling structures
server.sin_family = AF_INET;

server.sin_addr.s_addr = htonl (INADDR_ANY);

server.sin_port = htons (PORT);

//attach socket to descriptor
if (bind (socketDescriptor, (struct sockaddr *) &server, sizeof (struct sockaddr)) == -1)
{
perror ("[server]Error at bind\n");
return errno;
}

//server is listening
if (listen (socketDescriptor, 2) == -1)
{
perror ("[server]Error at listen\n");
return errno;
}

/serving concurrent the clients

while (1)
{
int client;
int length = sizeof (from);

printf ("[server]Waiting at port %d...\n",PORT);
fflush (stdout);

//accepting client

client = accept (socketDescriptor, (struct sockaddr *) &from, &length);
counter ++;
switch(fork())
{
case -1:
perror("fork err\n");
exit(2);
case 0:
//error if failed connection
if (client < 0)
{
perror ("[server]Error at accept\n");
continue;
}
//conenction established
bzero (message, 100);
printf ("[server]Waiting for message...\n");
fflush (stdout);

//reading message
if (read (client, message, 100) <= 0)
{
perror ("[server]Error at read\n");
close (client); //closing connection
continue; //keep listening
}

printf ("[server]Message was received%s\n", message);
//this is where I want to increment counter, when I want to verify message

int number;
number = atoi(message);//convert char to int

printf("The number is: %d\n", number);//print number

printf("The counter is : %d\n", counter);

fflush(stdout);

exit(2);
}
close (client);
} /* while */
} /* main */

最佳答案

只需将 counter++ 移动到父进程即可。当子进程启动时,它会获取 counter 的副本,并且您修改的副本不会影响其在父进程中的副本(实际上是原始副本)。如果您在父进程中更新它,您将实现您想要的。

int main(int argc, char ** argv)
{
int number;
int listenfd, connfd, n;
pid_t childpid;
socklen_t clilen;
char message[MAXLINE], answer[MAXLINE];
struct sockaddr_in clientaddr, serveraddr;
int counter;

counter = 0;
// create socket
if ((listenfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
perror("ERROR at creating socket\n");
exit(2);
}

// preparation of socket address
serveraddr.sin_family = AF_INET;
serveraddr.sin_addr.s_addr = htonl(INADDR_ANY);
serveraddr.sin_port = htons(PORT);

// bind the socket
bind(listenfd, (struct sockaddr *) &serveraddr, sizeof(serveraddr));

// listen to the socket
listen(listenfd, LIMIT);

printf("Server running, waiting for connections at port : %d\n", PORT);

while (1)
{
clilen = sizeof(clientaddr);
// accept a connection
connfd = accept(listenfd, (struct sockaddr *) &clientaddr, &clilen);
printf("Recieved guest\n");
switch (fork())
{
case -1:
perror("fork err\n");
exit(2);
case 0:
/* eroare la acceptarea conexiunii de la un client */
if (connfd < 0)
{
perror("[server]Eroare la accept().\n");
continue;
}

/* s-a realizat conexiunea, se astepta mesajul */
bzero(message, 100);
printf("[server]Asteptam mesajul...\n");
fflush(stdout);

/* citirea mesajului */
if (read(connfd, message, 100) <= 0)
{
perror("[server]Eroare la read() de la client.\n");
close(connfd); /* inchidem conexiunea cu clientul */

continue; /* continuam sa ascultam */
}
printf ("[server]Message was received...%s\n", message);
fflush(stdout);

number = atoi(message);

printf("The number is: %d\n", number);
printf ("%d\n", counter + 1);
_exit(0); // The child should not create further grand children
default:
counter++;
break;
}//switch
}//for
close(connfd);
}//main

关于c - C 中的静态变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34443976/

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