标准线程活动检查(C语言) [英] Standart thread activity check (C language)
问题描述
我正在研究C中的Window Sockets并编写一个多路复用的I / O客户端 - 服务器应用程序,我正在尝试处理标准线程。我想在
stdin,stdout和stderr
中检查活动但看起来我无法理解它是如何工作的。输入线程,例如,当我使用printf函数或打开一些文件(fopen,fopen_s),输出 - 从serverby客户端读取消息,从file和errorthread读取字符串 - 正在检查错误(使用fprintf(errno,param2) ,param3)。
我尝试了什么:
我想要的检查这个Threadsand的活动我正在编写一个程序,只是为了看看它是如何工作的但我遇到了一些问题。我不明白如何检查这个活动(是的,也许它听起来很愚蠢,但我真的坚持了)。 />
我想使用select()函数然后使用FD_ZERO和FD_SET。我应该如何获得哪个套接字处于活动状态而哪个套接字处于活动状态?如何使其成为主导?
这是我的scelet代码:
< pre> #pragma comment(lib,ws2_32.lib )
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#include< winsock2.h>
#include< stdio.h>
#include< stdlib.h>
#include <犯错no.h>
#include< sys / types.h>
#define BUFFERSIZE 64
#define PORT 3765
#define SERVERADDR argv [1]
int main(int argc,char * argv [] )
{
struct sockaddr_in serverAddr = {0};
fd_set readSD;
fd_set writeSD;
fd_set errorSD;
timeval tv;
int retVal;
char msg ='1';
FD_ZERO(& readSD);
FD_ZERO(& writeSD);
FD_ZERO(& errorSD);
FD_SET(msg,& readSD);
FD_SET(msg,& writeSD);
FD_SET(msg,& errorSD);
tv.tv_sec = 5;
retVal = select(msg + 1,& readSD,& writeSD,& errorSD,& tv);
if(retVal< 0)
{
printf(Error\);
返回-1;
}
else if(retVal == 0)
{
printf(没有人,抱歉!);
}
else
{
int fd_isset_r = FD_ISSET(msg,& readSD);
if((fd_isset_r)!= 0)
{
printf(fd_isset_r error\\\
);
}
其他
{
printf(readSD是%s \ n,FD_ISSET(msg,& readSD)?ready:not ready);
}
int fd_isset_w = FD_ISSET(msg,& writeSD);
if((fd_isset_w)!= 0)
{
printf(fd_isset_w error \ n);
}
其他
{
printf(writeSD是%s \ n,FD_ISSET(msg,& writeSD)?ready:not ready);
}
int fd_isset_er = FD_ISSET(msg,& errorSD);
if((fd_isset_er)!= 0)
{
printf(fd_isset_er error\);
}
其他
{
printf(errorSD是%s \ n,FD_ISSET(msg,& errorSD)?ready:not ready);
}
}
system(暂停);
返回0;
}
我很满意任何建议和帮助。谢谢
为了学习客户端/服务器架构,最好先阅读文章或观看视频。这使您可以更好地了解现实生活中的客户端/服务器编程。
标准I / O是系统常量,它们与PC硬件配合使用并处理FIFO原理
I'm studying Window Sockets in C and writing a multiplexing i/o client-server application I'm tryting to deal with Standard Threads. I want to checkactivity in
stdin, stdout and stderr
but It seems I can't get how it works. Input thread it's, for example, when I use printf function or open some file (fopen,fopen_s), output - reading a message from serverby client, reading strings from fileand errorthread -just checking of errors (using fprintf(errno, "param2", param3).
What I have tried:
I want to check activity of this Threadsand I'm writing a programm just to see how it works but I met some issus.I don't understand how to check this activity (yes, maybe it soundsstupid but I really stucked with that).
I wanna do that using select() function and then FD_ZERO and FD_SET. How should I get which socket is active and which is not? How to make themactive?
Here is my "scelet" code:
<pre> #pragma comment(lib, "ws2_32.lib") #define _WINSOCK_DEPRECATED_NO_WARNINGS #include <winsock2.h> #include <stdio.h> #include <stdlib.h> #include <errno.h> #include <sys/types.h> #define BUFFERSIZE 64 #define PORT 3765 #define SERVERADDR argv[1] int main(int argc, char *argv[]) { struct sockaddr_in serverAddr = { 0 }; fd_set readSD; fd_set writeSD; fd_set errorSD; timeval tv; int retVal; char msg = '1'; FD_ZERO(&readSD); FD_ZERO(&writeSD); FD_ZERO(&errorSD); FD_SET(msg, &readSD); FD_SET(msg, &writeSD); FD_SET(msg, &errorSD); tv.tv_sec = 5; retVal = select(msg + 1, &readSD, &writeSD, &errorSD, &tv); if (retVal < 0) { printf("Error\n"); return -1; } else if (retVal == 0) { printf("There is nobody, sorry\n"); } else { int fd_isset_r = FD_ISSET(msg, &readSD); if ((fd_isset_r)!=0) { printf("fd_isset_r error\n"); } else { printf("readSD is %s \n", FD_ISSET(msg, &readSD) ? "ready" : "not ready"); } int fd_isset_w = FD_ISSET(msg, &writeSD); if ((fd_isset_w) != 0) { printf("fd_isset_w error\n"); } else { printf("writeSD is %s\n", FD_ISSET(msg, &writeSD) ? "ready" : "not ready"); } int fd_isset_er = FD_ISSET(msg, &errorSD); if ((fd_isset_er) != 0) { printf("fd_isset_er error\n"); } else { printf("errorSD is %s\n", FD_ISSET(msg, &errorSD) ? "ready" : "not ready"); } } system("pause"); return 0; }
I aprreciate any advices and help. Thanks
For learning client/server architecture you better read this article or watch that video. This gives you better insight of real life client/server programming.
The standard I/O are system constants which are working with the PC hardware and working on the FIFO principle.
这篇关于标准线程活动检查(C语言)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!