- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我试图修复遗留代码中的一些错误。这是代码:
#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include <sys/msg.h>
#include "libSvr.h"
#define CONEC 3
#define CONFIG 4
typedef struct {
long type;
int cmd;
int id;
char data[7000];
} msg;
typedef struct {
int id[100];
unsigned long ip[100];
int type[100];
char name[100][20];
int num;
} conec;
typedef struct {
char ip;
char svr;
} param_cfg;
typedef struct {
unsigned long my_ip;
unsigned long svr_ip;
} tcp_cfg;
msg client, server;
conec conec_tab;
param_cfg param;
tcp_cfg setup;
void *Receive_MSG(void *arg) {
int i;
while (1) {
if ( msgrcv(msgkey,&client,7008,2,0) != -1 ) {
switch (client.cmd) {
case CONEC:
memmove((char*)&conec_tab,client.data,sizeof(conec_tab));
printf("\n%d\n",conec_tab.num);
break;
case CONFIG:
switch (conec_tab.type[client.id]) {
case 0:
memmove((char*)¶m,&client.data[sizeof(param_cfg)],sizeof(param_cfg));
printf("------------PARAM Config------------\n");
printf("%c\n%c\n\n",param.ip,param.svr);
break;
case 1:
memmove((char*)&setup,client.data,sizeof(tcp_cfg));
printf("\n------------Setup------------\n");
printf("%s\n",Inet_ntoa(setup.my_ip));
break;
}
}
}
}
}
void send_msg() {
msgsnd(msgkey,&(server.type),7008,0);
}
void Process(char *command) {
server.type = 1;
if ( !strncmp(command,"text",8) ) {
server.cmd = 1;
send_text();
send_msg();
}
else if ( !strncmp(command,"image",9) ) {
server.cmd = 2;
send_image();
send_msg();
}
else
printf("\nCommand not found.\n");
}
int main()
{
int i;
char cmd[100];
msgkey=msgget(100,0);
if ( msgkey == -1) {
printf("ERROR msgkey!!");
exit(1);
}
pthread_t receive;
pthread_create(&receive,NULL,Receive_MSG,NULL);
do {
printf("Enter a command -> ");
if (fgets(cmd,100,stdin) == NULL) {
printf("error");
exit(1);
}
cmd[strlen(cmd)-1]='\0';
if ( (strcmp(cmd,"exit")) && (strcmp(cmd,"")) )
Process(cmd);
} while (strcmp(cmd,"exit"));
return 0;
}
libSvr.h
代码:
#ifndef LIBSVR_H
#define LIBSVR_H
#pragma pack(push,1)
#pragma pack(1)
extern "C" void send_text();
extern "C" void send_image();
#pragma pack(pop)
#endif
线程 receive
刚从队列 ID msgkey
中的另一个应用程序接收到一些消息并打印一些文本。
问题是当我进入 do while 循环时,我需要获得一些用户输入,fgets
失败...当我打印错误(使用 errno
)时,我看到这是一个错误的文件描述符错误( EBADF
)。
Process
function get 命令调用适当的函数,基本上将通过套接字发送一些数据。这些函数由 Process
调用来自用 C++ 编写的库 ( libSvr
),具有 C 外部函数。
我正在使用带有 gcc 4.7.2 的 ubuntu 12.10 机器。
我试图查看预处理器(使用 gcc -E)和 stdin
之后的代码仍显示为 stdin
.我试图打印 fileno(stdin)
的输出它是 0(我在某处看到它是正确的值)然后我尝试调用 fgets
这样:fgets(cmd,100,0)
.但没有任何效果。
有人知道发生了什么事吗?
谢谢!
编辑:
包含所有代码。
我试过调用 gets()
, scanf()
和 read()
并评论线程创建(pthread_create
)但没有任何改变。
我运行了命令 strace -s2000 -etrace=open,close <MYPROGRAM>
这是输出:
open("/mnt/hgfs/projects/tls/i686/sse2/cmov/libSvr.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
open("/mnt/hgfs/projects/tls/i686/sse2/libSvr.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
open("/mnt/hgfs/projects/tls/i686/cmov/libSvr.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
open("/mnt/hgfs/projects/tls/i686/libSvr.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
open("/mnt/hgfs/projects/tls/sse2/cmov/libSvr.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
open("/mnt/hgfs/projects/tls/sse2/libSvr.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
open("/mnt/hgfs/projects/tls/cmov/libSvr.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
open("/mnt/hgfs/projects/tls/libSvr.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
open("/mnt/hgfs/projects/i686/sse2/cmov/libSvr.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
open("/mnt/hgfs/projects/i686/sse2/libSvr.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
open("/mnt/hgfs/projects/i686/cmov/libSvr.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
open("/mnt/hgfs/projects/i686/libSvr.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
open("/mnt/hgfs/projects/sse2/cmov/libSvr.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
open("/mnt/hgfs/projects/sse2/libSvr.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
open("/mnt/hgfs/projects/cmov/libSvr.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
open("/mnt/hgfs/projects/libSvr.so.1", O_RDONLY|O_CLOEXEC) = 3
close(3) = 0
open("/mnt/hgfs/projects/libpthread.so.0", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
open("/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
close(3) = 0
open("/lib/i386-linux-gnu/libpthread.so.0", O_RDONLY|O_CLOEXEC) = 3
close(3) = 0
open("/mnt/hgfs/projects/libc.so.6", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
open("/lib/i386-linux-gnu/libc.so.6", O_RDONLY|O_CLOEXEC) = 3
close(3) = 0
open("/mnt/hgfs/projects/libstdc++.so.6", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
open("/usr/lib/i386-linux-gnu/libstdc++.so.6", O_RDONLY|O_CLOEXEC) = 3
close(3) = 0
open("/mnt/hgfs/projects/libgcc_s.so.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
open("/lib/i386-linux-gnu/libgcc_s.so.1", O_RDONLY|O_CLOEXEC) = 3
close(3) = 0
open("/mnt/hgfs/projects/libm.so.6", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
open("/lib/i386-linux-gnu/libm.so.6", O_RDONLY|O_CLOEXEC) = 3
close(3) = 0
close(0) = 0
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
close(0) = -1 EBADF (Bad file descriptor)
Enter a command ->
errorEnter a command ->
errorEnter a command ->
errorEnter a command ->
因此,stdin
文件描述符正在关闭,但现在我需要知道它在哪里关闭。
我现在正在研究一些 howto 和 strace 的手册页,因为我以前从未使用过它。
我们欢迎任何帮助和提示。
再次感谢!
最佳答案
我让你的代码运行了一些变化。我没有设法让 System V 的东西工作,但我让它读取命令而不会崩溃。
我认为您程序中 System V-stiff 的初始化是错误的,并且会产生未定义的行为。
所有 close(0)
-s 可能来自某处的服务器线程,它不会因错误而从 while 循环中中断。
System V 的初始化(我不知道如何使用它,真的,只是阅读手册页),我是这样做的:
// msg_key is declared above the server-code (used by both) like `key_t msg_key`
msg_key = ftok ("/home/atle/prog/test.c", 'A');
if (msg_key == -1) {
printf ("ftok error: %s\n", strerror(errno));
exit(1);
}
msgkey = msgget(msg_key, IPC_CREAT);
if ( msgkey == -1) {
printf ("msgget error: %s\n", strerror(errno));
exit(1);
}
在服务器中,我是这样的:
int msgkey_server = msgget(msg_key, 0);
if ( msgkey_server == -1) {
printf ("Server msgget error: %s\n", strerror(errno));
exit(1);
}
while (1) {
printf ("Server loop\n");
if ( msgrcv(msgkey_server,&client,sizeof(client),0,0) == -1 ) {
printf ("Server read error: %s\n", strerror(errno));
exit(1);
}
else {
// MSG READ OK
}
顺便说一句:
当你将一个结构传递给一个附加了它的大小的命令时,你应该像我在这里所做的那样始终使用 sizeof()
,而不是手动写入大小(导致问题)。
额外顺便说一句:
您需要错误 header :
#include <errno.h>
关于C 标准输入不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19437455/
今天有小伙伴给我留言问到,try{...}catch(){...}是什么意思?它用来干什么? 简单的说 他们是用来捕获异常的 下面我们通过一个例子来详细讲解下
我正在努力提高网站的可访问性,但我不知道如何在页脚中标记社交媒体链接列表。这些链接指向我在 facecook、twitter 等上的帐户。我不想用 role="navigation" 标记这些链接,因
说现在是 6 点,我有一个 Timer 并在 10 点安排了一个 TimerTask。之后,System DateTime 被其他服务(例如 ntp)调整为 9 点钟。我仍然希望我的 TimerTas
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我就废话不多说了,大家还是直接看代码吧~ ? 1
Maven系列1 1.什么是Maven? Maven是一个项目管理工具,它包含了一个对象模型。一组标准集合,一个依赖管理系统。和用来运行定义在生命周期阶段中插件目标和逻辑。 核心功能 Mav
我是一名优秀的程序员,十分优秀!