我正在尝试将 ipv4 应用程序移植到 ipv6,但无法将套接字绑定到 ipv6 地址。
问题就在这里:
err=bind(listening, (sockaddr*)&hint, sizeof(hint));
Run Code Online (Sandbox Code Playgroud)
应该err是 0,但在此代码中它返回 -1。出了什么问题?
SOCKET listening = socket(AF_INET6, SOCK_STREAM, 0);
if (listening == INVALID_SOCKET)
{
cerr << "Can't create a socket! Quitting" << endl;
return;
}
int err;
// Bind the ip address and port to a socket
sockaddr_in6 hint;
hint.sin6_family = AF_INET6;
hint.sin6_flowinfo = 0;
hint.sin6_port = htons(54000);
hint.sin6_addr = in6addr_any;
err=bind(listening, (sockaddr*)&hint, sizeof(hint)); //<======= here
Run Code Online (Sandbox Code Playgroud) 昨天我重新安装了 Ubuntu(出于不相关的原因),从那时起 ssh 的行为就非常奇怪。
连接需要很长时间(几分钟)。使用 -vvv 我发现它首先尝试使用 ipv6 连接(超时),然后尝试通过 ipv4 连接,这几乎立即生效。
我以前从来没有遇到过 ssh 问题。
跑步
ssh login@address.net -vvv
Run Code Online (Sandbox Code Playgroud)
(带有我想要连接的地址)返回
OpenSSH_8.2p1 Ubuntu-4, OpenSSL 1.1.1f 31 Mar 2020
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: include /etc/ssh/ssh_config.d/*.conf matched no files
debug1: /etc/ssh/ssh_config line 21: Applying options for *
debug2: resolving "address.net" port 22
debug2: ssh_connect_direct
debug1: Connecting to address.net [*:*:*:*::*] port 22.
debug1: connect to address *:*:*:*::* port 22: Connection timed out
debug1: Connecting to address.net [*.*.*.*] port 22.
debug1: …Run Code Online (Sandbox Code Playgroud) 我希望使用套接字创建一个绑定到 IPv4 和 IPv6 地址的 TCP 服务器。
有没有办法在不创建 2 个套接字的情况下做到这一点?
尝试通过 Ipv6 地址在之间创建 tcp 套接字连接,我得到 Network is unreachable (os error 101) As a Binding local address is used fe80::850***。可能是因为 fe80*** 是操作系统生成的本地 ipv6 地址。有没有办法为系统提供正确的配置以通过 IPv6 进行呼叫?
我正在尝试用C#创建一个小型HTTP服务器,但我遇到了一些IPv6客户端问题.我的机器上有IPv6支持,但是当我尝试创建一个侦听套接字时,它就失败了.
Log("Creating server socket on port {0}", LogType.Info, _port);
_serversocket = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
_serversocket.Bind(new IPEndPoint(IPAddress.Any, _port));
_serversocket.Listen(10);
Run Code Online (Sandbox Code Playgroud)
我在这做错了什么?
代码抛出此异常:系统在尝试使用调用中的指针参数时检测到无效指针地址
编辑:
堆栈跟踪:
位于C:\ Users\Chris\Documents中TroutServer.Trout.Start(Int32端口)的System.Net.Sockets.Socket.Bind(EndPoint localEP)上的System.Net.Sockets.Socket.DoBind(EndPoint endPointSnapshot,SocketAddress socketAddress)\Visual Studio 2008\Projects\TroutServer\trout\trout.cs:第62行
Type是SocketException
我有一个ipv6地址,格式如下
uint32_t adress6 [4];
所以上面的数组存储了4个uint32_t类型的数据,总共等于16个字节,因此是一个ipv6类型的地址.
如何将以上格式存储的地址转换为网络字节顺序?
这是将IPv4客户端连接到IPv6服务器的延续:连接被拒绝.我正在尝试使用双堆栈套接字并尝试了解哪些setsockopt与IPV6_V6ONLY有用.在链接的问题上,我被告知"如果您还将服务器绑定到IPv6映射的IPv4地址,则将IPV6_V6ONLY设置为0会很有用".我在下面做了这个,并期望我的服务器能够接受来自IPv6和IPv4客户端的连接.但令人震惊的是,当我使用V4和V6插槽运行我的客户端时,两者都无法连接!
有人可以告诉我我做错了什么,还是我误解了IPv6双栈功能?
服务器:
void ConvertToV4MappedAddressIfNeeded(PSOCKADDR pAddr)
{
// if v4 address, convert to v4 mapped v6 address
if (AF_INET == pAddr->sa_family)
{
IN_ADDR In4addr;
SCOPE_ID scope = INETADDR_SCOPE_ID(pAddr);
USHORT port = INETADDR_PORT(pAddr);
In4addr = *(IN_ADDR*)INETADDR_ADDRESS(pAddr);
ZeroMemory(pAddr, sizeof(SOCKADDR_STORAGE));
IN6ADDR_SETV4MAPPED(
(PSOCKADDR_IN6)pAddr,
&In4addr,
scope,
port
);
}
}
addrinfo* result, hints;
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
int nRet = getaddrinfo("powerhouse", "82", &hints, &result);
SOCKET sock = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
int no …Run Code Online (Sandbox Code Playgroud) 如何将IPv6地址转换为二进制字符串?
例:
IPv6: 2001:0db8:85a3:0000:0000:8a2e:0370:7334
Binary: 0010000000000001 0000110110111000 1000010110100011 0000000000000000 0000000000000000 1000101000101110 0000001101110000 0111001100110100
Run Code Online (Sandbox Code Playgroud)
我想用Java做到这一点.这是我失败的尝试(我没有要求与此尝试相关的解决方案):
用C++实现,我更熟悉:
#include <iostream>
#include <string>
#define _BSD_SOURCE
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
std::string toBinary(unsigned long int decimalIpV6)
{
std::string r;
while(decimalIpV6!=0) {r=(decimalIpV6%2==0 ?"0":"1")+r; decimalIpV6/=2;}
return r;
}
unsigned long int ipV6toDecimal(std::string binaryString) {
struct sockaddr_in antelope;
inet_aton(binaryString.c_str(), &antelope.sin_addr); // store IP in antelope
// and this call is the same as the inet_aton() call, above:
antelope.sin_addr.s_addr = inet_addr(binaryString.c_str());
return antelope.sin_addr.s_addr;
}
int main() {
std::string …Run Code Online (Sandbox Code Playgroud) 我有一个NodeJS express正在运行的服务,Centos并监听GET请求,我需要标识用户的IP。
目前,我正在使用此脚本
ip = req.headers['x-forwarded-for'] ||
req.connection.remoteAddress ||
req.socket.remoteAddress ||
req.connection.socket.remoteAddress
Run Code Online (Sandbox Code Playgroud)
问题在于,有时返回的IP是IPv4,有时是IPv6。有没有办法只获取IPv4 IP?