- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在每个新的客户端连接上 fork 服务器进程
不同的进程(服务器的其他子进程,即 exec)无法识别在 fork 子进程中使用相同 fd 的客户端。
如何在其他进程上区分客户端?
如果文件描述符为新 sockfd,则接受调用将返回相同的值
/* server process */
#include
#include
#include
#include
#include
#define SIZE sizeof(struct sockaddr_in)
void catcher(int sig);
int newsockfd;
int main(void)
{
int sockfd;
char c;
struct sockaddr_in server = {AF_INET, 7000, INADDR_ANY};
static struct sigaction act;
act.sa_handler = catcher;
sigfillset(&(act.sa_mask));
sigaction(SIGPIPE, &act, NULL);
/* set up the transport end point */
if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
perror("socket call failed");
exit(1);
}
/* bind an address to the end point */
if ( bind(sockfd, (struct sockaddr *)&server, SIZE) == -1)
{
perror("bind call failed");
exit(1);
}
/* start listening for incoming connections */
if ( listen(sockfd, 5) == -1 )
{
perror("listen call failed");
exit(1) ;
}
for (;;)
{
/* accept a connection */
if ( (newsockfd = accept(sockfd, NULL, NULL)) == -1)
{
perror("accept call failed");
continue;
}
printf("New connection. File descriptor fd is %d\n",newsockfd);
/* spawn a child to deal with the connection */
if ( fork() == 0)
{
while (recv(newsockfd, &c, 1, 0) > 0)
{
c = toupper(c);
send(newsockfd, &c,1, 0);
}
/* when client is no longer sending information the socket can be closed and the child process terminated */
close(newsockfd);
exit (0);
}
/* parent doesn't need the newsockfd */
close(newsockfd);
}
}
void catcher(int sig)
{
close(newsockfd);
exit (0);
}
/* client process */
#include
#include
#include
#include
#include
#include
#define SIZE sizeof(struct sockaddr_in)
int main(void)
{
int sockfd;
char c, rc;
struct sockaddr_in server = {AF_INET, 7000};
/* convert and store the server's IP address */
server.sin_addr.s_addr = inet_addr("127.0.0.1");
/* set up the transport end point */
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
perror("socket call failed");
exit(1);
}
/* connect the socket to the server's address */
if ( connect (sockfd, (struct sockaddr *)&server, SIZE) == -1)
{
perror("connect call failed");
exit(1);
}
/* send and receive information with the server */
for(rc = '\n';;)
{
if (rc == '\n')
printf("Input a lower case character\n");
c = getchar();
send(sockfd, &c, 1, 0);
if (recv(sockfd, &rc, 1, 0) >0)
printf("%c", rc);
khác
{
printf("server has died\n");
close(sockfd);
exit(1);
}
}
}
1 Câu trả lời
文件描述符编号仅在它所在的进程中是唯一的,并且一旦关闭,就可以重用(例如,下次您调用 accept
时)。这不是“连接标识符”的好选择。
在您为每个连接都有一个新进程的情况下,连接的最自然标识符将是进程 ID - 但是您通过不保存 cái nĩa
的返回值而将其丢弃了。 .在父进程中,cái nĩa
返回它创建的子进程的 pid。您想保存它并在以后使用它。特别是您可以使用它来终止子进程或识别子进程何时退出( Chờ đợi
-family 函数会告诉您退出的是哪个子进程)。
当然,如果您的整个模型要为每个连接使用单独的进程,我不确定您为什么需要在父进程中识别连接。如果每个连接都不是一个完全独立的进程,那么使用线程而不是进程可能会做得更好。
关于C : "same file descriptors of all client connections" (client server programming),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5915144/
我有一个用于查找存储设备序列号的内核驱动程序,但该驱动程序存在问题。Descriptor->SerialNumberOffset 为 103但是 (LPCSTR)(UINT_PTR)Descripto
在我的程序中,每当我用导致无法检测到 ORB 功能的东西覆盖相机时,程序就会崩溃并出现错误: OpenCV Error: Assertion failed (type == src2.type() &
定义 通常,一个 descriptor 是具有“绑定行为”的对象属性。所绑定行为可通过 descriptor 协议被自定义的 __get__() , __set__() 和 __delete__(
如 normaluser : $ ulimit -n 4096 -bash: ulimit: open files: cannot modify limit: Operation not permit
我正在尝试在elasticsearch中安装ik分析,ik源来自以下位置: GitHub 我的步骤来自自述文件和来自互联网的一些资料 cd elasticsearch-analysis-ik mvn
我有以下代码: int fds[2]; if (pipe(fds) < 0) { fprintf(stderr, "ERROR, unable to open pipe: %s\n", str
我在 C 中有一个简单的生产者消费者程序,尝试用 fork 解决它当生产者试图在管道上写入时,我得到了错误:我用相同的逻辑编写了另一个程序,但这个程序没有给我任何线索,让我知道为什么? 生产者无法在管
我很难理解 FREAK 描述符中的参数 orientationNormalized 和 scaleNormalized。知道它们的意思或作用吗? OpenCV FREAK 文档:http://docs
我在做的事情是否符合通用设计模式?如果有,名字是什么? 我有一个复杂对象,它具有“简单”字段,例如字符串和字符串列表,以及其他复杂对象。我想将此对象的实例添加到 JMS 消息队列中,这意味着它们需要是
在例子中: event.events = EPOLLIN; event.data.fd = fd; int ret = epoll_ctl(epoll_fd, EPOLL_CTL_ADD, event
最近,我的 Crashlytics 和 Apple 崩溃日志收到了许多崩溃信息 -[CTTelephonyNetworkInfo updateRat:descriptor:]在没有太多其他信息的情况下
是否有可能将N个文件描述符作为一个文件描述符显示给程序,以便在N个文件描述符(即从N个套接字)中接收的数据将被转发回单个文件描述符上的调用API,从而隐藏它实际上可能来自不同的文件描述符的事实吗?是否
网络编程菜鸟在这里, 我对accept和connect套接字函数的行为感到困惑。在大多数编程语言中,这些函数的包装返回不同类型的值:accept返回可用于发送/接收数据的新描述符,但是connect不
我正在尝试启动resque-web,但是会发生此错误: [Sun Mar 06 05:27:48 +0000 2011]启动“resque-web” ... [Sun Mar 06 05:27:48
我有一个项目,其中有几个为程序集插件编写的自定义描述符。有没有办法一次只运行其中一个描述符而不是整个描述符?我尝试使用文档中的描述符开关 here ,传递到我想要运行的一个描述符的完整路径,但它正在运
我正在尝试学习 POSIX 中的基本 IO 函数,我编写了以下代码,但它不起作用,并且在我尝试执行代码时返回“Bad file descriptor”错误: #include #include #
编辑:最小、完整和可验证的示例位于下面的注释中,代码实际上有效,问题出在不同的区域。抱歉,发帖错误,我现在无法删除它。 我知道,已经有一些关于此的页面,但我确实尝试了所有方法,但没有任何效果。我一直遇
#define MAX 2 int main(){ int mutex = semget(ftok("/usr",'P'),1,IPC_CREAT|0666); int wrt = s
我正在尝试实现一个 simpel c shell,它将通过管道传输任意数量的命令。这是相关的 for 循环: int status; int i,j,inputFile,outputFile,pid;
简介 我典型的swig接口(interface)文件类似如下: %{ //COPY THIS BLOCK AS IS #include static CppClass* get_Cp
Tôi là một lập trình viên xuất sắc, rất giỏi!