- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试创建一个支持多个客户端的基本服务器。我希望主服务器连接到两个客户端,然后通过调用 execl 将客户端的文件描述符 fork 并传递给另一个程序。两个客户端都连接到服务器并能够与其通信(通过向客户端询问用户名或句柄进行测试),但是当我尝试将客户端传递给第二个程序时(这意味着两者之间的个人服务器)客户端举办棋盘游戏比赛)当第二个程序试图联系他们时,客户端没有显示输出,我肯定这是我试图传递连接信息的方式。对正确传递连接的任何帮助表示赞赏。
这是主服务器接受两个连接后的代码:
主服务器:注意。根据建议添加了额外的错误检查。
if(fork() == 0){
close(listener);
int nBytes;
char* playerOne[20];
char* playerTwo[20];
//Creates strings to hold file descriptor information for execl
char connAscii[sizeof(int)];
char connAscii2[sizeof(int)];
snprintf(connAscii,sizeof(conn), "%d", conn);
snprintf(connAscii2,sizeof(conn2), "%d", conn2);
fprintf(stderr, "Int conn: %d, asciiConn: %s, backToInt: %d", conn, connAscii, atoi(connAscii));
char *argf[2];
argf[0] = connAscii;
argf[1] = connAscii2;
//Send Handle Request to Connection 1
nBytes = send(conn, handleRequest, sizeof(handleRequest),0);
if(nBytes == -1){
perror("send");
exit(1);
}
//Receive Handle from Connection 1
nBytes = recv(conn, playerOne, 20, 0);
if(nBytes == -1){
perror("recv");
exit(1);
}
//Send Handle Request to Connection 2
nBytes = send(conn2, handleRequest, sizeof(handleRequest),0);
if(nBytes == -1){
perror("send");
exit(1);
}
//Receive Handle from Connection 2
nBytes = recv(conn2, playerTwo, 20, 0);
if(nBytes == -1){
perror("recv");
exit(1);
}
//Send Handle for Connection 2 to Connection 1
nBytes = send(conn, playerTwo, sizeof(playerTwo),0);
if(nBytes == -1){
perror("send");
exit(1);
}
//Send Handle for Connection 1 to Connection 2
nBytes = send(conn2, playerOne, sizeof(playerOne),0);
if(nBytes == -1){
perror("send");
exit(1);
}
//Passes file descriptors to nimMatch
execl("nimMatch", "nimMatch", argf, (char *)0);
}
个人服务器(与主服务器不同的 .c 文件):注意。添加调试语句
char greet[] = {"Hello players, please wait for match setup."};
int main(int argc, char *argv[]) {
int conn1 = atoi(argv[1]);
int conn2 = atoi(argv[2]);
int sent;
fprintf(stderr, "Attempting connection\n");
sent = send(conn1, greet,sizeof(greet),0);
if(sent == -1){
perror("send");
exit(1);
}
return 0;
}
以及与对手连接匹配后的客户端代码:
printf("Your opponent is: %s\n\n Connecting to match server...", otherHandle);
memset(&buf[0],0,sizeof(buf));
if ((numbytes = recv(sockfd, buf, 99, 0)) == -1) { //should come from personal server
perror("recv");
exit(1);
}
printf("From match server: %s\n", buf);
如果需要更多代码,请告诉我,我知道客户端已成功连接到服务器,并且在编译或运行时我没有遇到任何错误。
来自 EJP 的建议答案确保私有(private)服务器接收到正确的输入,但客户端似乎没有存储来自私有(private)服务器的字符串。下面调整后的客户端代码声明客户端正在接收 64 字节但 buf 为空。
printf("You're opponent is: %s\n\n Connecting to match server...", otherHandle);
memset(&buf[0],0,sizeof(buf));
numbytes = recv(sockfd, buf, 99, 0);
if (numbytes == -1) {
perror("recv");
exit(1);
}
if(strlen(buf) < 1)
fprintf(stderr, "No buffer input, found %d bytes\n", numbytes);
printf("From match server: %s\n", buf);
最佳答案
您将参数错误地传递给 execl()
。它不采用参数数组,它采用可变参数列表。应该是
execl(path, connAscii, connAscii2, (char*)0);
关于C: 通过 execl 传递套接字连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29292358/
假设我 putenv 一个环境变量 ABC,然后执行 execl,或者我执行 execle 并将 ABC 添加到 envp 数组中我传递给 execle 的指针。 如果有的话,有区别吗? 最佳答案 p
我试图在 C++ 中运行 execl(),但它失败了,我不确定原因。 string fileName = "test.txt"; string computerName = "jssoile@
POSIX 系统公开了一系列 exec 函数,这些函数允许将可能不同的东西加载到当前进程中,保持打开的文件描述符、进程标识符等。 这可以出于多种原因完成,在我的例子中,这是自举——我想更改我自己进程的
我正在使用 c 中的 find 命令进行编程,但当我想对文件使用 exec 时,我被阻止了。 如果它是一个目录,那么很简单,我只使用 chdir(path) 但在文件上我有错误不是目录,所以我不能在上
if (fork_return==0) { printf("FROM THE CHILD PROCESS \n"); //print process id printf("The chil
我真的很喜欢这方面的一些调试帮助。我从早上到凌晨 4 点就一直在做这件事。 (我预计会在 7 小时内交付此内容 [上午 11 点]) main.c 中的所有内容都有效,但是当我创建一些子进程来使用 e
我试图理解这个系统调用(execle()),但我不知道它是如何工作的。我不知道如何使用 char* envp[],我们必须将其作为参数传递。我已经尝试过,但它不起作用: #include #incl
我有一个程序可以读取文件,对其进行处理并将结果放入输出文件中。当我有一个参数(输入文件)时,我创建输出文件并写入内容。 我创建了一个 fork() 以便将 stdout 重定向为 write() 内容
#include #include #include #define MAXLINE 512 main(int argc,char* argv[]){ int k; for (k
程序从配置文件中读取一些值,有些已定义,有些未定义,有些值为 0,有些处于事件状态。 我有以下代码: char *arg1="", *arg1_value="", *arg2="", *arg
你能帮我解决这个问题吗?execl 调用的程序“exam”和“students”执行了多少次?我认为正确的答案是程序“考试”有 8 个运行时间,“学生”有 0 个运行时间,因为在前两个 fork 中将
我正在制作一个 C 程序,该程序使用 execl 来执行 ls 命令来列出文件。如果在没有任何命令行参数的情况下调用程序,则会列出当前目录,如果用户将文件目录指定为命令行参数,则会列出该目录。 exe
我使用 TCC 编译并运行了以下程序在 Windows 7 下出现应用程序崩溃: #include int main(void) { if (execlp("c:\\windows\\sys
我正在使用 cywin 在 Windows 上编译 crashme 源代码,我遇到了一些编译错误。 错误: crashme.c: In function 'vfork_main': crashme.c
在UNIX环境下的系统编程上下文中,在使用C++编程语言时,在我的理解中,execl()会传入它要运行的程序的路径,以及一个vector。当传入该 vector 时,我将其理解为传入入口点,通常是 m
我有以下两个简单的程序: bye.cc #include int main() { std::cout #include #include #include using namespace
这个 execl 语句可能有什么问题?当我尝试运行它时,接收可执行文件提示 argc 小于 3。当我打印 argv 内容时,我得到以下信息: argv[1] = -1076146944 argv[2]
我的目标是学习如何执行 execle() 函数。我在 Head First C 书中找到了这段代码。 这是主进程代码(diner_info.c): #include #include int ma
在一个项目上工作,当我调用 execl() 时它不工作。它在 fork 之后调用,应该重新执行当前文件。(参数在文件的前面声明): argument = argv[0]; int err =execl
我正在尝试用 C 语言重现此命令: ls | wc > output.txt 因此,为此,我编写了以下程序: #include #include #include #include #incl
我是一名优秀的程序员,十分优秀!