gpt4 book ai didi

c - Winsock - 为什么 ZNC(和 IRC 保镖)不接受我的 Winsock 连接?

转载 作者:行者123 更新时间:2023-11-30 15:57:40 26 4
gpt4 key购买 nike

使用典型的 irc 客户端,我可以输入:

/server localhost 6667 nick:pass

当我输入为 ZNC 配置的 nick:pass(IRC 保镖)时,我将被转发到在我的服务器/nick:pass 组合下连接到 znc 的服务器。

如何以编程方式同时打开包含所有这些参数的 Winsock 连接? /server localhost 6667 nick:pass

我尝试在连接后发送数据,但 znc 似乎忽略了请求。或者我根本就没有连接到它。此代码已连接到不需要 Ping 身份验证的 IRC 服务器,因此我知道它可以工作。

#define AF_INET                    2
#define SOCK_STREAM 1
#define SOL_SOCKET 0xffff
#define SO_SNDTIMEO 0x1005

string server_addr = "127.0.0.1";
int server_port = 6667;

void ircconnect(){
int struct_sockaddr[4];
int addr, port_low, port_high;
int opts[1];
int c;

if (irc_disabled == 1) return(0);

// fill the sockaddr struct
addr = inet_addr(server_addr);
port_low = server_port & 0x00ff;
port_high = (server_port & 0xff00) >> 8;
struct_sockaddr[0] = AF_INET | (port_high << 16) | (port_low << 24);
struct_sockaddr[1] = addr;
struct_sockaddr[2] = 0;
struct_sockaddr[3] = 0;

// connect
s = socket(AF_INET, SOCK_STREAM, 0);

opts[0] = 1000; // send timeout milliseconds
setsockopt(s, SOL_SOCKET, SO_SNDTIMEO, opts, 4);
c = connect(s, struct_sockaddr, 16);

Sleep(5000);

sendLine(nick + ":" + password);

最佳答案

参见下面的Python解释:

int struct_sockaddr[4];
int addr, port_low, port_high;
int opts[1];
int c;
string zncauth = nick + ":" + password;

if (irc_disabled == 1) return(0);

// fill the sockaddr struct
addr = inet_addr(server_addr);
port_low = server_port & 0x00ff;
port_high = (server_port & 0xff00) >> 8;
struct_sockaddr[0] = AF_INET | (port_high << 16) | (port_low << 24);
struct_sockaddr[1] = addr;
struct_sockaddr[2] = 0;
struct_sockaddr[3] = 0;

// connect
s = socket(AF_INET, SOCK_STREAM, 0);
opts[0] = 1000; // send timeout milliseconds
setsockopt(s, SOL_SOCKET, SO_SNDTIMEO, opts, 4);
c = connect(s, struct_sockaddr, 16);

// send
sendLine("NICK zncbotnick");
sendLine("USER znc bot znc :znc");
sendLine("PASS " + zncauth);

void sendLine(string text){
if (irc_disabled == 1) return(0);
text = text + "\r\n";
send(s, text, StringLen(text), 0);
}

上面的代码终于可以工作了,这对我来说在 python 中可以通过 ZNC 进行身份验证。基本上,在另一种语言中,我向 znc 发出命令,但它们从未到达 znc,因为代码缺少返回和换行符 '\r\n'。借助 python,我能够实时诊断问题。

#!/usr/bin/env python
# -*- coding: utf-8 -*-

//Test.py
import socket

zncauth = 'nick:password'
server_addr = '127.0.0.1'
server_port = 6667
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((server_addr, server_port))
s.send( 'NICK botnick\r\n')
s.send( 'USER znc bot znc :znc\r\n')
s.send( 'PASS ' + zncauth + ' \r\n')

关于c - Winsock - 为什么 ZNC(和 IRC 保镖)不接受我的 Winsock 连接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10343533/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com