- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我对 write(2) 函数有一个有趣的问题。 PrepareResponseForSetCoordinates 函数导致写入时出现错误的文件描述符错误。
这是错误行:perror("写入套接字时出错");总产量:写入套接字时出错:错误的文件描述符
我确信我已经建立了连接,因为 PrepareResponseForConnectionTest 非常有效。
你能知道错误的原因吗?
当我使用 gcc 作为编译器时,没有问题。在那之后,由于使用了多个新的 cpp 源,我使用 g++ 作为编译器,但出现了这个错误。
问候
下面是我的代码:
#define MAX_PMS_MESSAGE_LEN (4096)
unsigned char baCommBuffer[MAX_PMS_MESSAGE_LEN];
unsigned char PrepareResponseForSetCoordinates(void)
{
unsigned char baTempBuff[255]={0};
unsigned short bCnt=0,i=0,bCsum=0,bCnt2=0;
time_t lEpochTime;
time_t lSessionTime;
memset(baTempBuff,0,sizeof(baTempBuff));
memset(baCommBuffer,0,sizeof(baCommBuffer));
bzero(baCommBuffer,MAX_PMS_MESSAGE_LEN);
bzero(baTempBuff,sizeof(baTempBuff));
lEpochTime = time(NULL);
baCommBuffer[bCnt++] = START_CHAR;
baCommBuffer[bCnt++] = START_CHAR;
baCommBuffer[bCnt++] = PACKET_SEPERATOR;
sprintf((char*)baTempBuff,"%ld",(unsigned long)lEpochTime);
memcpy(baCommBuffer+bCnt,baTempBuff,10);
bzero(baTempBuff,sizeof(baTempBuff));
bCnt+=10;
baCommBuffer[bCnt++] = PACKET_SEPERATOR;
// lSessionTime = time(NULL);
if(SPMSMessage.lSessionID)
lSessionTime = SPMSMessage.lSessionID;
else
lSessionTime=lEpochTime;
sprintf((char*)baTempBuff,"%ld",(unsigned long)lSessionTime);
memcpy(baCommBuffer+bCnt,baTempBuff,10);
bzero(baTempBuff,sizeof(baTempBuff));
bCnt+=10;
baCommBuffer[bCnt++] = PACKET_SEPERATOR;
baCommBuffer[bCnt++] = PMC_ID;
baCommBuffer[bCnt++] = PACKET_SEPERATOR;
baCommBuffer[bCnt++] = PMS_ID;
baCommBuffer[bCnt++] = PACKET_SEPERATOR;
baCommBuffer[bCnt++] = 'R';
baCommBuffer[bCnt++] = 'E';
baCommBuffer[bCnt++] = 'P';
baCommBuffer[bCnt++] = PACKET_SEPERATOR;
baCommBuffer[bCnt++] = 'C';
baCommBuffer[bCnt++] = 'O';
baCommBuffer[bCnt++] = 'O';
baCommBuffer[bCnt++] = 'S';
baCommBuffer[bCnt++] = PACKET_SEPERATOR;
if(SPMSMessage.bParam== SET_COOR_CAM1_PARAM)
{
baCommBuffer[bCnt++] = '2';
baCommBuffer[bCnt++] = PACKET_SEPERATOR;
baCommBuffer[bCnt++] = 'O';
baCommBuffer[bCnt++] = 'N';
baCommBuffer[bCnt++] = 'E';
baCommBuffer[bCnt++] = PARAMETER_SEPERATOR;
baCommBuffer[bCnt++] = 'A';
baCommBuffer[bCnt++] = 'C';
baCommBuffer[bCnt++] = 'K';
}
else if(SPMSMessage.bParam== SET_COOR_CAM2_PARAM)
{
baCommBuffer[bCnt++] = '2';
baCommBuffer[bCnt++] = PACKET_SEPERATOR;
baCommBuffer[bCnt++] = 'T';
baCommBuffer[bCnt++] = 'W';
baCommBuffer[bCnt++] = 'O';
baCommBuffer[bCnt++] = PARAMETER_SEPERATOR;
baCommBuffer[bCnt++] = 'A';
baCommBuffer[bCnt++] = 'C';
baCommBuffer[bCnt++] = 'K';
}
else
{
baCommBuffer[bCnt++] = '1';
baCommBuffer[bCnt++] = PACKET_SEPERATOR;
baCommBuffer[bCnt++] = 'N';
baCommBuffer[bCnt++] = 'A';
baCommBuffer[bCnt++] = 'C';
baCommBuffer[bCnt++] = 'K';
}
baCommBuffer[bCnt++] = PACKET_SEPERATOR;
baCommBuffer[bCnt++] = '*';
memset(baTempBuff,0,sizeof(baTempBuff));
bCsum = CalculateCheckSum(baCommBuffer);
sprintf((char*)baTempBuff,"%.2X",bCsum);
memcpy(baCommBuffer+bCnt,baTempBuff,2);
bzero(baTempBuff,sizeof(baTempBuff));
bCnt+=2;
baCommBuffer[bCnt++] = PACKET_SEPERATOR;
baCommBuffer[bCnt++] = END_CHAR;
baCommBuffer[bCnt++] = END_CHAR;
#ifdef _DEBUG_DEEP_DETAILED
if(EDebugDeepDetail<GetDebugLevelOfPMC())
{
printf("WILL BE sent: %s uzunluk:%d\n", baCommBuffer,bCnt);
sprintf(caLogStr,"WILL BE sent: %s uzunluk:%d\n", baCommBuffer,bCnt);
PrintToLogFile(caLogStr);
memset(caLogStr,0,MAX_LOG_STRLEN);
}
#endif
if((ETcpConnectionState== ETcpStateConnected) || (ETcpConnectionState== ETcpStateConnectedAndWaitingToWrite) )
{
if (write(sockfd,baCommBuffer,bCnt) < 0)
{
#ifdef _DEBUG_DETAILED
if(EDebugDetail<GetDebugLevelOfPMC())
{
perror("ERROR writing to socket");
PrintToLogFile("ERROR writing to socket");
}
#endif
return 0;
}
#ifdef _DEBUG_DEEP_DETAILED
if(EDebugDeepDetail<GetDebugLevelOfPMC())
{
printf("sent: %s uzunluk:%d\n", baCommBuffer,bCnt);
sprintf(caLogStr,"sent: %s uzunluk:%d\n", baCommBuffer,bCnt);
PrintToLogFile(caLogStr);
memset(caLogStr,0,MAX_LOG_STRLEN);
}
#endif
}
else
{
#ifdef _DEBUG_DETAILED
if(EDebugDetail<GetDebugLevelOfPMC())
{
PrintToLogFile("Henüz Bağlantı Yok\n");
}
#endif
return 0;
}
return (bCnt);
下面你可以看到运行没有错误的代码:
unsigned char PrepareResponseForConnectionTest(void)
{
unsigned char baTempBuff[20]={0};
unsigned char bCnt=0,i=0,bCsum=0;
time_t lEpochTime;
time_t lSessionTime;
memset(baTempBuff,0,sizeof(baTempBuff));
memset(baCommBuffer,0,sizeof(baCommBuffer));
bzero(baCommBuffer,MAX_PMS_MESSAGE_LEN);
bzero(baTempBuff,sizeof(baTempBuff));
lEpochTime = time(NULL);
baCommBuffer[bCnt++] = START_CHAR;
baCommBuffer[bCnt++] = START_CHAR;
baCommBuffer[bCnt++] = PACKET_SEPERATOR;
sprintf((char*)baTempBuff,"%ld",(unsigned long)lEpochTime);
memcpy(baCommBuffer+bCnt,baTempBuff,10);
bzero(baTempBuff,sizeof(baTempBuff));
bCnt+=10;
baCommBuffer[bCnt++] = PACKET_SEPERATOR;
lSessionTime = SPMSMessage.lSessionID;
sprintf((char*)baTempBuff,"%ld",(unsigned long)lSessionTime);
memcpy(baCommBuffer+bCnt,baTempBuff,10);
bzero(baTempBuff,sizeof(baTempBuff));
bCnt+=10;
baCommBuffer[bCnt++] = PACKET_SEPERATOR;
baCommBuffer[bCnt++] = PMC_ID;
baCommBuffer[bCnt++] = PACKET_SEPERATOR;
baCommBuffer[bCnt++] = PMS_ID;
baCommBuffer[bCnt++] = PACKET_SEPERATOR;
baCommBuffer[bCnt++] = 'R';
baCommBuffer[bCnt++] = 'E';
baCommBuffer[bCnt++] = 'P';
baCommBuffer[bCnt++] = PACKET_SEPERATOR;
baCommBuffer[bCnt++] = 'C';
baCommBuffer[bCnt++] = 'O';
baCommBuffer[bCnt++] = 'N';
baCommBuffer[bCnt++] = 'T';
baCommBuffer[bCnt++] = PACKET_SEPERATOR;
baCommBuffer[bCnt++] = '1';
baCommBuffer[bCnt++] = PACKET_SEPERATOR;
baCommBuffer[bCnt++] = 'A';
baCommBuffer[bCnt++] = 'C';
baCommBuffer[bCnt++] = 'K';
baCommBuffer[bCnt++] = PACKET_SEPERATOR;
baCommBuffer[bCnt++] = '*';
memset(baTempBuff,0,sizeof(baTempBuff));
bCsum = CalculateCheckSum(baCommBuffer);
sprintf((char*)baTempBuff,"%.2X",bCsum);
memcpy(baCommBuffer+bCnt,baTempBuff,2);
bzero(baTempBuff,sizeof(baTempBuff));
bCnt+=2;
baCommBuffer[bCnt++] = PACKET_SEPERATOR;
baCommBuffer[bCnt++] = END_CHAR;
baCommBuffer[bCnt++] = END_CHAR;
if((ETcpConnectionState== ETcpStateConnected) || (ETcpConnectionState== ETcpStateConnectedAndWaitingToWrite) )
{
if (write(sockfd,baCommBuffer,bCnt) < 0)
{
perror("ERROR writing to socket\n");
PrintToLogFile("ERROR writing to socket\n");
}
#ifdef _DEBUG_DEEP_DETAILED
if(EDebugDeepDetail<GetDebugLevelOfPMC())
{
printf("sent: %s uzunluk:%d\n", baCommBuffer,bCnt);
sprintf(caLogStr,"sent: %s uzunluk:%d\n", baCommBuffer,bCnt);
PrintToLogFile(caLogStr);
memset(caLogStr,0,MAX_LOG_STRLEN);
}
#endif
return 0;
}
else
{
#ifdef _DEBUG_DETAILED
if(EDebugDetail<GetDebugLevelOfPMC())
{
PrintToLogFile("There is no connection yet\n");
}
#endif
//return 0;
}
//printf("\n\n");
return (bCnt);
}
这是我的 InitializeConnection 函数和 ConnectToServer 函数:
void InitializeTcpConnection(int argc, char *argv[])
{
int optval;
socklen_t optlen = sizeof(optval);
ETcpConnectionState = ETcpStateNotConnected;
if (argc < 3)
{
fprintf(stderr,"usage: %s hostname_or_ip port\n", argv[0]);
#ifdef _DEBUG_PROCESS
if(EDebugProcess<GetDebugLevelOfPMC())
{
sprintf(caLogStr,"usage: %s hostname_or_ip port\n", argv[0]);
PrintToLogFile(caLogStr);
memset(caLogStr,0,MAX_LOG_STRLEN);
}
#endif
exit(0);
}
portno = atoi(argv[2]);
/* int socket(domain,type,protocol)
* socket creates an endpoint for communication and returns a descriptor
* AF_INET: ARPA Internet protocols
* SOCK_STREAM: sequenced, two way connection based byte streams
*
* return: Socket returns a non-negative descriptor on success.
* On failure it returns -1 and sets errno to indicate the error
* */
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
#ifdef _DEBUG_PROCESS
if(EDebugProcess<GetDebugLevelOfPMC())
{
perror("ERROR creating socket");
PrintToLogFile("ERROR creating socket\n");
}
#endif
exit(1);
}
/**/
/* Set the option active */
optval = 1;
optlen = sizeof(optval);
if(setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, &optval, optlen) < 0)
{
#ifdef _DEBUG_PROCESS
if(EDebugProcess<GetDebugLevelOfPMC())
{
perror("setsockopt()");
PrintToLogFile("ERROR creating socket\n");
}
#endif
close(sockfd);
exit(EXIT_FAILURE);
}
#ifdef _DEBUG_PROCESS
if(EDebugProcess<GetDebugLevelOfPMC())
{
printf("SO_KEEPALIVE set on socket\n");
PrintToLogFile("SO_KEEPALIVE set on socket\n");
}
#endif
/* Check the status again */
if(getsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, &optval, &optlen) < 0) {
#ifdef _DEBUG_PROCESS
if(EDebugProcess<GetDebugLevelOfPMC())
{
perror("getsockopt()");
PrintToLogFile("getsockopt()");
}
#endif
close(sockfd);
exit(EXIT_FAILURE);
}
#ifdef _DEBUG_PROCESS
if(EDebugProcess<GetDebugLevelOfPMC())
{
printf("SO_KEEPALIVE is %s\n", (optval ? "ON" : "OFF"));
}
#endif
#ifdef _DEBUG_PROCESS
if(EDebugProcess<GetDebugLevelOfPMC())
{
PrintToLogFile("Setting socket for reusability\n");
}
#endif
if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(int)) == -1)
{
#ifdef _DEBUG_PROCESS
if(EDebugProcess<GetDebugLevelOfPMC())
{
perror("setsockopt");
PrintToLogFile("Setting socket option error\n");
}
#endif
exit(1);
}
/*********/
//get the address info by either host name or IP address
SetTcpServerIpAddress(argv[1]);
server = gethostbyname(argv[1]);
if (server == NULL)
{
#ifdef _DEBUG_PROCESS
if(EDebugProcess<GetDebugLevelOfPMC())
{
PrintToLogFile("ERROR, no such host\n");
}
#endif
exit(1);
}
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
bcopy((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr, server->h_length);
//serv_addr.sin_port: unsigned short
//htons converts the unsigned short hostshort from host byte order to network byte order
serv_addr.sin_port = htons(portno);
ETcpConnectionState = ETcpStateWaitingForConnection;
}
int ConnectToServer(void)
{
if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0)
{
#ifdef _DEBUG_DETAILED
if(EDebugDetail<GetDebugLevelOfPMC())
{
PrintToLogFile("ERROR connecting");
}
#endif
return 1; // connection error
//exit(1);
}
else
{
#ifdef _DEBUG_DETAILED
if(EDebugDetail<GetDebugLevelOfPMC())
{
PrintToLogFile("connection established\n");
}
#endif
#ifndef PMS_COMM_POLL_MODE
#ifdef _DEBUG_DETAILED
if(EDebugDetail<GetDebugLevelOfPMC())
{
PrintToLogFile("State is non poll mode\n");
}
#endif
ETcpConnectionState = ETcpStateConnected;
#endif
#ifdef PMS_COMM_POLL_MODE
#ifdef _DEBUG_DETAILED
if(EDebugDetail<GetDebugLevelOfPMC())
{
PrintToLogFile("State is poll mode\n");
}
#endif
ETcpConnectionState = ETcpStateConnectedAndWaitingToRead;
#endif
SendRequestToGetImageUploadInfo();
}
return 0;
}
最佳答案
一般来说,当遇到“Bad File Descriptor”时,意味着你传入API的socket文件描述符是无效的,这有多种可能的原因:
关于c - 错误的文件描述符与 Linux 套接字 write() 错误的文件描述符 C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11258781/
来自descriptor文档: A descriptor can be called directly by its method name. For example, d.__get__(obj).
概要 本人python理论知识远达不到传授级别,写文章主要目的是自我总结,并不能照顾所有人,请见谅,文章结尾贴有相关链接可以作为补充 全文分为三个部分装饰器理论知识、装饰器应用、装饰器延申
我正在查看 python 的描述 rune 档 here ,让我思考的陈述是: 对于物体,机械在 object.__getattribute__()转换 b.x进入 type(b).__dict__[
if((fd = creat(file_name,O_RDWR|S_IRWXU|S_IRWXG|S_IRWXO)) < 0){ perror("Create failed!");
长话短说Python 2.7.5,当使用描述符作为装饰器时,有没有办法传入参数(给 __init__ 方法)?或者如何使用带参数的方法装饰器 ( as here ) 访问类实例的属性? -- 我认为这
我试着用谷歌搜索一些关于它的东西。为什么非数据描述符适用于旧式类? 文档说他们不应该: “Note that descriptors are only invoked for new style ob
我升级到使用嵌入式 maven 3 的 netbeans 7。我有一个项目,其中包含许多模块和包含其他模块的模块。我的其他不依赖于内部项目的子模块可以在相同的配置下正常工作。在这种情况下,spring
我正在关注http://scikit-image.org/docs/0.11.x/auto_examples/plot_daisy.html ,但是不太清楚 desc[0],desc[1] 和 des
我有一个要求,其中有一个全局 FILE指针/描述符。其中一个函数将从该指针/描述符中读取。与FILE指针/描述符相关联的内部指针前进。此函数返回后,我想从同一个 FILE 指针/描述符中读取数据,并与
我正在编写一些描述符来封装数据验证,并想为它们编写测试。 我想知道是否应该通过在我的测试中创建描述符实例然后显式调用 __get__ 或 __set__ 方法来测试它们。 或者我应该在我的测试文件中创
我有这个 python 描述符: # Date Descriptor class DateAttribute(): def __init__(self, value=None):
分割: @font-face { font-family: 'RobotoLight'; src: url('../font/jura-demibold.eot'); src: url('../fon
我正在编写一个配置卷的存储自动化模块。我没有传递在存储 Controller 上实际创建卷所需的六个或更多参数,而是使用 __slots__ 创建了一个参数类,它被传递到 create 方法中,如下所
在我的应用程序中,我必须使用静态摄像头跟踪大学讲座中的讲师。目前我正在使用 Emgu CV 的默认 GPUHOGDescriptor,如果讲师的整个 body 都可见,它会很好用。在讲师站在 tabl
大家好,我正在使用 opencv3 和 contrib。问题是我想计算给定像素的筛选描述符(不使用检测到的关键点)。 我正在尝试用给定的像素构建一个关键点向量。但是,要创建关键点,除了像素位置外,我还
我正在使用 OpenCV 中的 HOGDescriptor 类进行对象检测。在我看来,该实现仅使用无符号渐变,因此无法区分亮->暗和暗->亮过渡,这是我真正需要的功能。有谁知道使用有符号梯度的 HOG
我目前正在使用 OpenCV 的 ORB 特征提取器,我确实注意到 ORB 描述符的存储方式很奇怪(至少对我来说是这样)(它基本上是一个 BRIEF-32,带有与我的问题无关的修改) .正如你们中的一
我想知道,在 MATLAB 中是否有针对“汽车”之类的对象而非人类的 HOG 描述符的任何实现? 但万一,只有人类,你能指导我找到那个代码,并给我一些提示,以改进代码以用于“汽车或摩托车等物体” 最佳
我正在尝试更好地理解描述符。 我不明白为什么在 foo 方法中描述符 __get__ 方法未被调用。 据我了解描述符 __get__ 当我通过点运算符访问对象属性或使用 __getattribute_
我想要一个类似于这个(无效)的结构: const uint8_t uArray[] = { uint8_t(sizeof(uArray)), 1, 2, 3 }; 并且 uArray[0] 应该是 4
我是一名优秀的程序员,十分优秀!