- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我试图用 C 语言制作一个 IRC 机器人。当机器人试图连接到一个 IRC 服务器时,它进入了一个无限循环,在那里它什么也没收到。我不确定这是因为我加入 IRC 服务器的过程格式不正确,还是我丢失了一些应该发送/接收的数据。
#include<stdio.h>
#include<sys/socket.h>
#include<netdb.h>
#include<string.h>
#include<stdlib.h>
#include<time.h>
#define MAXSIZE 4096
void delay(int milliseconds)
{
long pause;
clock_t now;
pause = milliseconds*(CLOCKS_PER_SEC/1000);//set delay using
now = clock();
while( now < pause )
now = clock();
}
int send_data(int sockfd, char message[])
{
send(sockfd, message, strlen(message), 0);
printf("OUT: %s\n", message);
return 1;
}
int recv_data(int sockfd, char *message)
{
int n;
n = recv(sockfd, message, MAXSIZE, 0);
printf("IN: %s\n", message);
return n;
}
int tcp_connect(int *sockfd, char server[], char port[])
{
//declare variables
struct addrinfo hints, *res;
//zero out structures
memset(&hints,0,sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
//query DNS server for IP address and port
getaddrinfo(server,port,&hints,&res);
//create socket for data transmission
*sockfd = socket(res->ai_family,res->ai_socktype,0);
if (*sockfd < 0)
{
printf("failure to create socket\n");
return 0;
}
//connect to server side port using created socket
if (connect(*sockfd, res->ai_addr, res->ai_addrlen)!= 0)
{
printf("failure to connect to port\n");
return 0;
}
freeaddrinfo(res);
return 1;
}
int irc_auth(int sockfd)
{
//create and start clock
clock_t start_t;
start_t = clock();
//seed RNG with clock output
srand(start_t);
//generate necessary variables
char name[15] = "bot";
char user[35] = "USER ";
char nick[20] = "NICK ";
char join[20] = "JOIN #randChat\r\n";
int i,id;
//generate random character for ID tag A-Z
for(i=0; i<5; i++)
{
id = rand() % 91;
if(id < 65)
{
while(id < 65)
{
id = rand() % 91;
}
}
name[strlen(name)] = id;
}
//append return and null to string
strcat(nick,name);
strcat(nick,"\r\n");
//append to finish creating USER IRC command
strcat(user,name);
strcat(user," 8 * :");
strcat(user,name);
strcat(user,"\r\n");
//send data to server
send_data(sockfd,user);
delay(1000);
send_data(sockfd,nick);
delay(1000);
send_data(sockfd,join);
return 1;
}
int main (int argc, char *argv)
{
//variables
int sockfd, n, flag;
char *mesg_in = malloc(sizeof(char) * MAXSIZE);
char *pos;
char nick[30];
char *mesg_out = malloc(sizeof(char) * MAXSIZE);
//connect to port 6667 of irc.freenode.org using tcp
while(flag<1)
{
if(tcp_connect(&sockfd,"irc.freenode.org","6667") == 1)
{
flag = 1;
}
}
//IRC channel authentication
irc_auth(sockfd);
//command loop
while(1)
{
mesg_in[0] = 0;// zero out message
//memset(mesg_in,0,strlen(mesg_in));
n = recv_data(sockfd,mesg_in);// pull message from channel
if (n > 0)// check to see if it recieved a command
{
mesg_in[n] = 0;// set null at the end of recieved data
//respond to ping commands from server
if(strstr(mesg_in,"PING") != NULL)
{
mesg_out[0] = 0;// zero out message
pos = strstr(mesg_in," ")+1;// point to data needed
//append to out bound message
sprintf(mesg_out,"PONG %s\r\n",pos);
//send outbound message
send_data(sockfd,mesg_out);
}
}
}
}
我们将不胜感激任何帮助
最佳答案
无论可能存在什么其他问题,delay()
都是其中之一。你在这个测试程序中的函数等待两秒钟,然后同时打印 1 2 3
,因为它只考虑从程序开始耗时,而不是从当前时刻开始的时间。
#include <stdio.h>
#include <time.h>
void delay(int milliseconds)
{
long pause;
clock_t now;
pause = milliseconds*(CLOCKS_PER_SEC/1000);//set delay using
now = clock();
while( now < pause )
now = clock();
}
int main (void)
{
delay(2000);
printf("1\n");
delay(2000);
printf("2\n");
delay(2000);
printf("3\n");
return 0;
}
此版本以两秒为间隔打印 1 2 3
#include <stdio.h>
#include <time.h>
void delay(clock_t milliseconds)
{
clock_t elapsed, pause, stamp;
stamp = clock();
pause = milliseconds * CLOCKS_PER_SEC / 1000;
while ((elapsed = clock() - stamp) < pause);
}
int main (void)
{
delay(2000);
printf("1\n");
delay(2000);
printf("2\n");
delay(2000);
printf("3\n");
return 0;
}
另请注意,在整数运算中,我先乘法再除法。
关于c - IRC 机器人故障,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41368466/
我的对话 fragment 有问题。我想使用 android:onClick 属性,因为在我看来代码更清晰。 在我的布局中,我有以下声明: 现在我的 DialogFragment import an
我正在制作一个使用谷歌地图的应用程序,我的主要 xml 代码 fragment 如下: 我的java代码是: import android.os.Bundle; import com.googl
是否可以在一个应用程序中有两个单独的首选项?我有一个在运行时连接到外部可执行文件(引擎)的应用程序。应用程序有自己的偏好,引擎也有。这两者不能混用,因为下次用户可能会决定加载具有不同偏好的完全不同的引
这是“Google Catalogs”应用程序的快照。我只是想知道我们如何进行这种图像重叠。![enter image description here][1] 最佳答案 您可以使用 FrameLay
我制作了一个 GridView ,其中添加了图像。但我的问题是它显示的 gridview 尺寸非常小,其中只有 3 张图像,就像图片中那样,我想在其中显示 9 张图像。 小号 但我希望 gridvie
我目前正在学习如何在 Android 上创建和使用服务。我查看了 android SDK 以获得进一步的帮助,我发现了 android:enable=[true |假]。 在SDK中是这样说的: Th
在完成我的 Android 游戏时,我希望用户将他/她的分数与高分进行比较。为此,我将当前的最高分存储在 SQLite 数据库中。但我认为我的方法(似乎可行)笨拙且丑陋: //in the final
出于某种原因,谷歌没有为模拟器提供任何通知声音,我找不到任何关于如何加载通知声音的引用。 我遇到过这个线程:How to play ringtone/alarm sound in Android但是除
我的应用有以下样式: true #ffffff true 它在我使用的大多数手机上运行良好。 (LG G5、Nexus 5、Moto G)但是当我在 LeEco Pro
我想确保我的网站阻止 Selenium 和 QTP 等自动化工具。有没有办法做到这一点 ?网站上的哪些设置会导致 Selenium 失败? 最佳答案 适当考虑对原始问题“你到底为什么要这样做?”的评论
我正在处理我的联系表,我希望它尽可能地防止垃圾邮件。我正在使用一些方法作为反击: 使用 JavaScript 正则表达式验证电子邮件的合法性 验证所有字段是否具有足够的值 制作一个不可见的字段来吸引机
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, over
我正在制作一个 android 应用程序,我需要在我的应用程序中显示远程图像我使用以下代码。但图像未显示: for(int i=0;i
我有几个与 Android 操作系统中的 AIML 集成相关的问题。我对 Java 了解不多,但我对 AIML 了解很多。我制作了 AIML 文件,现在我想做的是制作一个可以加载文件并在 GUI 中运
我有一个具有“登录”和“注销”操作的应用程序。用户登录后,在他们注销时堆栈中可能还剩下任意数量的 Activity 。 当用户注销时,我希望重置所有应用程序状态。我清除了我的磁盘缓存和首选项,但在运行
我是 Android 应用程序开发的新手,我正在尝试开发一个可用的应用程序。但是我创建的这个页面自创建以来就出现了问题,我真的希望有人能帮助我解决这个问题。每次我运行这个程序时,应用程序都会关闭。 这
我尝试将 android:imeOptions 设置为 actionSend、actionSearch。但是键盘上没有“发送”或“搜索”按钮,只有普通的“Enter”键。我还尝试设置不同的输入类型。
我想创建一种方法来查看相机坐标的中心是否在某个区域的边界内,如果是,则执行一些操作。 当我尝试这样做时,出现错误:Operator !不能应用于 LatLngBounds。 我也试过 if(temp
我正在尝试将视频录制时长限制为 5 秒。我正在使用默认相机和此代码: intent.putExtra(MediaStore.EXTRA_DURATION_LIMIT, 5); 现在它在三星 S6 上工
我正在开发一款安卓游戏 https://code.google.com/p/something-soft/我的日志猫说它正试图激发游戏的 Intent ,但随后主线程似乎死了(出现 ActivityN
我是一名优秀的程序员,十分优秀!