- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在用c语言构建一个多客户端UDP服务器,但是当我尝试从系统连接到我的服务器时,我收到此错误gethostbyaddr上的错误:成功请找到下面的服务器代码。我已经尝试了类似问题的解决方案( gethostbyaddr() returns NULL but errno result in SUCCESS ),但我无法让它工作。任何帮助将不胜感激
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define BUFSIZE 1024
/*
* error - wrapper for perror
*/
void error(char *msg) {
perror(msg);
exit(1);
}
int main(int argc, char **argv) {
int sockfd; /* socket */
int portno; /* port to listen on */
int clientlen; /* byte size of client's address */
struct sockaddr_in serveraddr; /* server's addr */
struct sockaddr_in clientaddr; /* client addr */
struct hostent *hostp; /* client host info */
char buf[BUFSIZE]; /* message buf */
char *hostaddrp; /* dotted decimal host addr string */
int optval; /* flag value for setsockopt */
int n; /* message byte size */
FILE *fp; /* file variable */
char str[10];
int i = 0;
char userlist[10];
int array_size;
char line[256];
int cred,flag;
/*
* check command line arguments
*/
if (argc != 2) {
fprintf(stderr, "usage: %s <port>\n", argv[0]);
exit(1);
}
portno = atoi(argv[1]);
/*
* socket: create the parent socket
*/
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (sockfd < 0)
error("ERROR opening socket");
/* setsockopt: Handy debugging trick that lets
* us rerun the server immediately after we kill it;
* otherwise we have to wait about 20 secs.
* Eliminates "ERROR on binding: Address already in use" error.
*/
optval = 1;
setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR,
(const void *)&optval , sizeof(int));
/*
* build the server's Internet address
*/
bzero((char *) &serveraddr, sizeof(serveraddr));
memset(&serveraddr,0,sizeof(serveraddr));
serveraddr.sin_family = AF_INET;
serveraddr.sin_addr.s_addr = htonl(INADDR_ANY);
serveraddr.sin_port = htons((unsigned short)portno);
/*
* bind: associate the parent socket with a port
*/
if (bind(sockfd, (struct sockaddr *) &serveraddr,
sizeof(serveraddr)) < 0)
error("ERROR on binding");
/*
* main loop: wait for a datagram, then echo it
*/
clientlen = sizeof(clientaddr);
while (1) {
/*
* recvfrom: receive a UDP datagram from a client
*/
bzero(buf, BUFSIZE);
n = recvfrom(sockfd, buf, BUFSIZE, 0,
(struct sockaddr *) &clientaddr, &clientlen);
if (n < 0)
error("ERROR in recvfrom");
/*
* gethostbyaddr: determine who sent the datagram
*/
hostp = gethostbyaddr((const char *)&clientaddr.sin_addr.s_addr,
sizeof(clientaddr.sin_addr.s_addr), AF_INET);
if (hostp == NULL)
error("ERROR on gethostbyaddr");
hostaddrp = inet_ntoa(clientaddr.sin_addr);
if (hostaddrp == NULL)
error("ERROR on inet_ntoa\n");
printf("server received datagram from %s (%s)\n",
hostp->h_name, hostaddrp);
printf("server received %d/%d bytes: %s\n", strlen(buf), n, buf);
fp = fopen("users.txt", "r");
while (fgets(line, sizeof(line), fp)) {
//printf("%s\n",line);
cred = strncmp(buf,line,strlen(line)-1);
//printf("%d",strlen(line)-1);
if(cred == 0){
printf("Authenticated....");
flag = 1;
break;
}
else{
printf("Invalid username/password");
}
}
fclose(fp);
最佳答案
gethostbyaddr()
需要一个指向 struct in_addr
的指针作为第一个参数,对于您显示的代码,该参数将为 &clientaddr.sin_addr
。
形成相关(Linux)文档(man gethostbyaddr
):
<小时/>[...] The host address argument is a pointer to a struct of a type depending on the address type, for example a struct in_addr * (probably obtained via a call to inet_addr(3)) for address type AF_INET.
gethostbyaddr()
在 h_errno
中设置错误代码,而不是在 errno
中。
形成相关(Linux)文档(man gethostbyaddr
):
RETURN VALUE
[...[ The gethostbyname() and gethostbyaddr() functions return the hostent structure or a null pointer if an error occurs. On error, the h_errno variable holds an error number.
手册页中也给出了可能的错误代码:
ERRORS
The variable h_errno can have the following values:
HOST_NOT_FOUND
The specified host is unknown.
NO_ADDRESS or NO_DATA
The requested name is valid but does not have an IP address.
NO_RECOVERY
A nonrecoverable name server error occurred.
TRY_AGAIN
A temporary error occurred on an authoritative name server. Try again later.
关于c - gethostbyaddr : Success 上出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39695078/
问题:假设我们有一个名为success 的C++ 字符串。 &success 和 success& 有什么区别? 这题是我期中复习的,我理解&success是获取对象的地址。但我不确定 success
我正在阅读 gearman 代码的手册页( http://manpages.ubuntu.com/manpages/precise/man3/gearman_success.3.html )。他们有两
在 Angular 中,[class]="'success'" 和 class="success" 有区别吗? 例如: ... 或 ... 如果同时使用 IIUC 后者会导致替换前者,所以我想更好
当我查看日志文件时 D:\SAS\XXX\Lev1\SASMain\BatchServer\Logs 我看到了这两行 NOTE: Libref TESTLIB successfully assigne
我正在努力完成这项工作: $http.post('/route/path', {'username': $scope.threadedUsers[currentIndex].name}). s
我正在尝试设置协议(protocol)来处理我的 javascript 函数中的成功和错误,但是当函数成功时它似乎并不合作。我收到以下错误: Failed with: TypeError: Objec
在 typescript 中,我有一个 DataAccess 类,以便所有 Ajax 调用都通过单个对象进行路由,以节省应用程序中许多地方的代码重复。 在使用这种方法时,我需要使用回调将响应返回到调用
我已经用 Spring Security 3.0.2 实现了一个登录-注销系统,一切都很好,但是对于这一点:在我添加了一个带有 invalid-session-url 属性的 session 管理标签
运行命令pip install rpy2会导致以下输出和错误消息:。我按照另一篇Stackoverflow帖子上的说明卸载并重新安装了R和相关目录,但得到了相同的错误。我还尝试安装以前版本的rpy2,
我最近在项目的测试目标中添加了一个新的单元测试,当我点击产品 > 测试时,Xcode 说“测试成功”。我相当确定测试应该失败,因为被测试的方法尚未实现。我在测试中添加了一个断点,但从未到达过;测试仍然
我正在编写一个程序来创建一个AD帐户并启用Exchange邮箱,并且从中得到一些奇怪的行为。 首先,尽管它成功创建了AD用户,但由于“找不到MyPath/先生示例”,因此无法启用邮箱。我认为这是由于A
在cd_deployer_conf文件中我们设置了Cleanup="false,这样传输包在发布后不会被删除,但是我们可以看到单个发布操作有3个不同的包喜欢 tcm_0-264891-66560.CO
我正在使用 $().each() 循环访问一些项目。我想确保这段脚本之后的操作仅在 each() 完成时执行。 示例: $('something').each(function() { // do
我正在尝试创建一个 ping 洪水程序,它将目标 IP 地址和广播 IP 地址作为参数。该程序将向广播地址发送 icmp echo 数据包,并将受害者的 IP 地址作为源。网络上所有收到数据包的主机都
我正在实现一个虚拟 LTE EPC 设置,其模块 (HSS) 之一需要 mysql 数据库。创建后,我必须运行该模块,但收到屏幕截图 1 中所示的错误。附:我对这个东西很陌生 最佳答案 您必须修改您的
#include #include #include #include #include #include #include #include #include void error(cha
我的基本代码如下, fd = open("test.file", O_RDONLY); if (read(fd, &tempch, 1) < 1) { perror("F
更新 :这似乎是在 Pipeline: Declarative 中引入的错误插件版本 1.3.5 - 降级到 1.3.4.1解决了这个问题。票证创建于: https://issues.jenkins-
我最近在 jQuery 网站上看到了弃用通知。 Deprecation Notice: The jqXHR.success(), jqXHR.error(), and jqXHR.complete()
我假设调用 status.success() 会终止后台作业。但是,当我运行它时,我仍然看到第二条日志消息: Parse.Cloud.job("Tester", function (request,
我是一名优秀的程序员,十分优秀!