我的问题是我无法通过套接字(windows xp 和 windows7)连接两个组件,尽管用套接字创建的服务器正在监听并且我可以远程登录它。然后它接收信息并执行应该做的事情,但是如果我运行相应的套接字客户端,我会收到错误 10061。此外,我在防火墙后面 - 这两个组件在我的 LAN 中运行,Windows 防火墙已关闭,
comp1 [客户端]: 192.168.1.2 端口 12345
comp2 [服务器]: 192.168.1.5 端口 12345
路由器:192.168.1.1
也许端口转发会有帮助?但对我来说最重要的是回答为什么在 telnet 工作正常时套接字会失败。
客户:
int main(){
// Initialize Winsock.
WSADATA wsaData;
int iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if (iResult != NO_ERROR)
printf("Client: Error at WSAStartup().\n");
else
printf("Client: WSAStartup() is OK.\n");
// Create a socket.
SOCKET m_socket;
m_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (m_socket == INVALID_SOCKET){
printf("Client: socket() - Error at socket(): %ld\n", WSAGetLastError());
WSACleanup();
return 7;
}else
printf("Client: socket() is OK.\n");
// Connect to a server.
sockaddr_in clientService;
clientService.sin_family = AF_INET;
//clientService.sin_addr.s_addr = inet_addr("77.64.240.156");
clientService.sin_addr.s_addr = inet_addr("192.168.1.5");
//clientService.sin_addr.s_addr = inet_addr("87.207.222.5");
clientService.sin_port = htons(12345);
if (connect(m_socket, (SOCKADDR*)&clientService, sizeof(clientService)) == SOCKET_ERROR){
printf("Client: connect() - Failed to connect.\n");
wprintf(L"connect function failed with error: %ld\n", WSAGetLastError());
iResult = closesocket(m_socket);
if (iResult == SOCKET_ERROR)
wprintf(L"closesocket function failed with error: %ld\n", WSAGetLastError());
WSACleanup();
return 6;
}
// Send and receive data
int bytesSent;
int bytesRecv = SOCKET_ERROR;
// Be careful with the array bound, provide some checking mechanism
char sendbuf[200] = "Client: Sending some test string to server...";
char recvbuf[200] = "";
bytesSent = send(m_socket, sendbuf, strlen(sendbuf), 0);
printf("Client: send() - Bytes Sent: %ld\n", bytesSent);
while(bytesRecv == SOCKET_ERROR){
bytesRecv = recv(m_socket, recvbuf, 32, 0);
if (bytesRecv == 0 || bytesRecv == WSAECONNRESET){
printf("Client: Connection Closed.\n");
break;
}else
printf("Client: recv() is OK.\n");
if (bytesRecv < 0)
return 0;
else
printf("Client: Bytes received - %ld.\n", bytesRecv);
}
system("pause");
return 0;
}
服务器:
int main(){
WORD wVersionRequested;
WSADATA wsaData={0};
int wsaerr;
// Using MAKEWORD macro, Winsock version request 2.2
wVersionRequested = MAKEWORD(2, 2);
wsaerr = WSAStartup(wVersionRequested, &wsaData);
if (wsaerr != 0){
/* Tell the user that we could not find a usable WinSock DLL.*/
printf("Server: The Winsock dll not found!\n");
return 0;
}else{
printf("Server: The Winsock dll found!\n");
printf("Server: The status: %s.\n", wsaData.szSystemStatus);
}
/* Confirm that the WinSock DLL supports 2.2.*/
/* Note that if the DLL supports versions greater */
/* than 2.2 in addition to 2.2, it will still return */
/* 2.2 in wVersion since that is the version we */
/* requested. */
if (LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2 ){
/* Tell the user that we could not find a usable WinSock DLL.*/
printf("Server: The dll do not support the Winsock version %u.%u!\n", LOBYTE(wsaData.wVersion), HIBYTE(wsaData.wVersion));
WSACleanup();
return 0;
}else{
printf("Server: The dll supports the Winsock version %u.%u!\n", LOBYTE(wsaData.wVersion), HIBYTE(wsaData.wVersion));
printf("Server: The highest version this dll can support: %u.%u\n", LOBYTE(wsaData.wHighVersion), HIBYTE(wsaData.wHighVersion));
}
//////////Create a socket////////////////////////
//Create a SOCKET object called m_socket.
SOCKET m_socket;
// Call the socket function and return its value to the m_socket variable.
// For this application, use the Internet address family, streaming sockets, and the TCP/IP protocol.
// using AF_INET family, TCP socket type and protocol of the AF_INET - IPv4
m_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
// Check for errors to ensure that the socket is a valid socket.
if (m_socket == INVALID_SOCKET){
printf("Server: Error at socket(): %ld\n", WSAGetLastError());
WSACleanup();
//return 0;
}else{
printf("Server: socket() is OK!\n");
}
////////////////bind//////////////////////////////
// Create a sockaddr_in object and set its values.
sockaddr_in service;
// AF_INET is the Internet address family.
service.sin_family = AF_INET;
// "127.0.0.1" is the local IP address to which the socket will be bound.
service.sin_addr.s_addr = htons(INADDR_ANY);//inet_addr("127.0.0.1");//htons(INADDR_ANY); //inet_addr("192.168.1.2");
// 55555 is the port number to which the socket will be bound.
// using the htons for big-endian
service.sin_port = htons(12345);
// Call the bind function, passing the created socket and the sockaddr_in structure as parameters.
// Check for general errors.
if (bind(m_socket, (SOCKADDR*)&service, sizeof(service)) == SOCKET_ERROR){
printf("Server: bind() failed: %ld.\n", WSAGetLastError());
closesocket(m_socket);
//return 0;
}else{
printf("Server: bind() is OK!\n");
}
// Call the listen function, passing the created socket and the maximum number of allowed
// connections to accept as parameters. Check for general errors.
if (listen(m_socket, 1) == SOCKET_ERROR)
printf("Server: listen(): Error listening on socket %ld.\n", WSAGetLastError());
else{
printf("Server: listen() is OK, I'm waiting for connections...\n");
}
// Create a temporary SOCKET object called AcceptSocket for accepting connections.
SOCKET AcceptSocket;
// Create a continuous loop that checks for connections requests. If a connection
// request occurs, call the accept function to handle the request.
printf("Server: Waiting for a client to connect...\n");
printf("***Hint: Server is ready...run your client program...***\n");
// Do some verification...
while (1){
AcceptSocket = SOCKET_ERROR;
while (AcceptSocket == SOCKET_ERROR){
AcceptSocket = accept(m_socket, NULL, NULL);
}
// else, accept the connection... note: now it is wrong implementation !!!!!!!! !! !! (only 1 char)
// When the client connection has been accepted, transfer control from the
// temporary socket to the original socket and stop checking for new connections.
printf("Server: Client Connected! Mammamija. \n");
m_socket = AcceptSocket;
char recvBuf[200]="";
char * rc=recvBuf;
int bytesRecv=recv(m_socket,recvBuf,64,0);
if(bytesRecv==0 || bytesRecv==WSAECONNRESET){
cout<<"server: connection closed.\n";
}else{
cout<<"server: recv() is OK.\n";
if(bytesRecv<0){
return 0;
}else{
printf("server: bytes received: %ld.\n",recvBuf);
}
}
客户端的输出:
PS C:\Users\Piter\documents\vs2010\projects\client_socket\debug> ./client_socket.exe
Client: WSAStartup() is OK.
Client: socket() is OK.
Client: connect() - Failed to connect.
connect function failed with error: 10061
PS C:\Users\Piter\documents\vs2010\projects\client_socket\debug> ipconfig
我已经使用 netcat 和 powershell 创建了监听套接字(不确定以正确的方式):
PS C:\netcat> ./nc.exe -v -l -p 12345
listening on [any] 12345 ...
Warning: forward host lookup failed for cf16.chello.pl: h_errno 11001: HOST_NOT
connect to [192.168.1.2] from cf16.chello.pl [192.168.1.2] 4473: HOST_NOT_FOUND
第三行和第四行是在另一个 powershell 中创建客户端时发生的情况:
PS C:\netcat> ./nc.exe 192.168.1.2 12345
好的。 当强制 netstat 不解析但使用给定的 IP 时,现在它连接:
PS C:\netcat> ./nc.exe -n -v -l -p 12345
listening on [any] 12345 ...
connect to [192.168.1.2] from (UNKNOWN) [192.168.1.2] 4622
但我的 C++ 仍然返回错误 10061。在服务器运行时 powershell 中没有消息 - 似乎我的客户端根本没有连接 - 服务器什么也没说,只有客户端有错误 10061。 有任何想法吗? 请帮忙:D
最佳答案
你说的
comp1: 192.168.1.2 port 12345
comp1: 192.168.1.6 port 12345
不知道哪台电脑是server和client。
但是你的客户端代码如下。
clientService.sin_addr.s_addr = inet_addr("192.168.1.5");
您连接到错误的服务器。请检查您的代码。
关于c - 套接字连接失败,telnet OK,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9764723/
我正在使用Sequel构建一个愿望list系统。我有一个wishlists和itemstable和一个items_wishlists连接表(该名称是续集选择的名称)。items_wishlists表还有一个用于facebookid的额外列(因此我可以存储opengraph操作),这是一个NOTNULL列。我还有Wishlist和Item具有续集many_to_many关联的模型已建立。Wishlist类也有:selectmany_to_many关联的选项设置为select:[:items.*,:items_wishlists__facebook_action_id].有没有一种方法可以
我使用的是Firefox版本36.0.1和Selenium-Webdrivergem版本2.45.0。我能够创建Firefox实例,但无法使用脚本继续进行进一步的操作无法在60秒内获得稳定的Firefox连接(127.0.0.1:7055)错误。有人能帮帮我吗? 最佳答案 我遇到了同样的问题。降级到firefoxv33后一切正常。您可以找到旧版本here 关于ruby-无法在60秒内获得稳定的Firefox连接(127.0.0.1:7055),我们在StackOverflow上找到一个类
我已经构建了一些serverspec代码来在多个主机上运行一组测试。问题是当任何测试失败时,测试会在当前主机停止。即使测试失败,我也希望它继续在所有主机上运行。Rakefile:namespace:specdotask:all=>hosts.map{|h|'spec:'+h.split('.')[0]}hosts.eachdo|host|begindesc"Runserverspecto#{host}"RSpec::Core::RakeTask.new(host)do|t|ENV['TARGET_HOST']=hostt.pattern="spec/cfengine3/*_spec.r
网络编程套接字网络编程基础知识理解源`IP`地址和目的`IP`地址理解源MAC地址和目的MAC地址认识端口号理解端口号和进程ID理解源端口号和目的端口号认识`TCP`协议认识`UDP`协议网络字节序socket编程接口`sockaddr``UDP`网络程序服务器端代码逻辑:需要用到的接口服务器端代码`udp`客户端代码逻辑`udp`客户端代码`TCP`网络程序服务器代码逻辑多个版本服务器单进程版本多进程版本多线程版本线程池版本服务器端代码客户端代码逻辑客户端代码TCP协议通讯流程TCP协议的客户端/服务器程序流程三次握手(建立连接)数据传输四次挥手(断开连接)TCP和UDP对比网络编程基础知识
我正在尝试在Rails上安装ruby,到目前为止一切都已安装,但是当我尝试使用rakedb:create创建数据库时,我收到一个奇怪的错误:dyld:lazysymbolbindingfailed:Symbolnotfound:_mysql_get_client_infoReferencedfrom:/Library/Ruby/Gems/1.8/gems/mysql2-0.3.11/lib/mysql2/mysql2.bundleExpectedin:flatnamespacedyld:Symbolnotfound:_mysql_get_client_infoReferencedf
我需要一个非常简单的字符串验证器来显示第一个符号与所需格式不对应的位置。我想使用正则表达式,但在这种情况下,我必须找到与表达式相对应的字符串停止的位置,但我找不到可以做到这一点的方法。(这一定是一种相当简单的方法……也许没有?)例如,如果我有正则表达式:/^Q+E+R+$/带字符串:"QQQQEEE2ER"期望的结果应该是7 最佳答案 一个想法:你可以做的是标记你的模式并用可选的嵌套捕获组编写它:^(Q+(E+(R+($)?)?)?)?然后你只需要计算你获得的捕获组的数量就可以知道正则表达式引擎在模式中停止的位置,你可以确定匹配结束
我正在尝试在配备ARMv7处理器的SynologyDS215j上安装ruby2.2.4或2.3.0。我用了optware-ng安装gcc、make、openssl、openssl-dev和zlib。我根据README中的说明安装了rbenv(版本1.0.0-19-g29b4da7)和ruby-build插件。.这些是随optware-ng安装的软件包及其版本binutils-2.25.1-1gcc-5.3.0-6gconv-modules-2.21-3glibc-opt-2.21-4libc-dev-2.21-1libgmp-6.0.0a-1libmpc-1.0.2-1libm
s=Socket.new(Socket::AF_INET,Socket::SOCK_STREAM,0)s.connect(Socket.pack_sockaddr_in('port','hostname'))ssl=OpenSSL::SSL::SSLSocket.new(s,sslcert)ssl.connect从这里开始,如果ssl连接和底层套接字仍然是ESTABLISHED,或者它是否在默认值7200之后进入CLOSE_WAIT,我想检查一个线程几秒钟甚至更糟的是在实际上不需要.write()或.read()的情况下关闭。是用select()、IO.select()还是其他方法完成
require"socket"server="irc.rizon.net"port="6667"nick="RubyIRCBot"channel="#0x40"s=TCPSocket.open(server,port)s.print("USERTesting",0)s.print("NICK#{nick}",0)s.print("JOIN#{channel}",0)这个IRC机器人没有连接到IRC服务器,我做错了什么? 最佳答案 失败并显示此消息::irc.shakeababy.net461*USER:Notenoughparame
一段时间以来,我一直在使用open_uri下拉ftp路径作为数据源,但突然发现我几乎连续不断地收到“530抱歉,允许的最大客户端数(95)已经连接。”我不确定我的代码是否有问题,或者是否是其他人在访问服务器,不幸的是,我无法真正确定谁有问题。本质上,我正在读取FTPURI:defself.read_uri(uri)beginuri=open(uri).readuri=="Error"?nil:urirescueOpenURI::HTTPErrornilendend我猜我需要在这里添加一些额外的错误处理代码...我想确保我采取一切预防措施来关闭所有连接,这样我的连接就不是问题所在,但是我