- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我的问题是我无法通过套接字(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/
我正在维护一些 Java 代码,我目前正在将它们转换为 C#。 Java 代码是这样做的: sendString(somedata + '\000'); 在 C# 中,我正在尝试做同样的事情: sen
如何确定函数中传递的参数是字符串还是字符(不确定如何正确调用它)文字? 我的函数(不正确): void check(const char* str) { // some code here }
我真的不知道如何准确地提出这个问题,但我希望标题已经说明了这一点。 我正在寻找一种方法(一个框架/库),它提供了执行 String.contains() 函数的能力,该函数告诉我给定的字符串是否与搜索
我正在尝试编写一些读取 Lambda 表达式并输出 beta 缩减版本的东西。 Lambda 的类型如下:\variable -> expression,应用程序的形式为 (表达式) (表达式)。因此
StackOverflow 上的第 1 篇文章,如果我没能把它做好,我深表歉意。我陷入了一个愚蠢的练习,我需要制作一个“刽子手游戏”,我尝试从“.txt”文件中读取单词,然后我得到了我的加密函数,它将
我想在 Groovy 中测试我的 Java 自定义注释,但由于字符问题而未能成功。 Groovyc: Expected 'a' to be an inline constant of type cha
当我尝试在单击按钮期间运行 javascript location.href 时,出现以下错误“字 rune 字中的字符过多”。 最佳答案 这应该使用 OnClientClick相反? 您可能还想停
我想要类似的东西: let a = ["v".utf8[0], 1, 2] 我想到的最接近的是: let a = [0x76, 1, 2] 和 "v".data(using: String.Encod
有没有办法在 MySQL 中指定 Unicode 字 rune 字? 我想用 Ascii 字符替换 Unicode 字符,如下所示: Update MyTbl Set MyFld = Replace(
阅读 PNG 规范后,我有点惊讶。我读过字 rune 字应该用像 0x41 这样的二进制值进行硬编码,而不是在(程序员友好的)'A' 中。问题似乎是在具有不同底层字符集的不同系统上编译期间字 rune
考虑一个具有 UTF-8 执行字符集的 C++11 编译器(并且符合要求 char 类型为有符号 8 位字节的 x86-64 ABI) . 字母 Ä(元音变音)具有 0xC4 的 unicode 代码
为什么即使有 UTF-8 字符串文字,C11 或 C++11 中也没有 UTF-8 字 rune 字?我知道,一般来说,字 rune 字表示单个 ASCII 字符,它与单字节 UTF-8 代码点相同,
我怎样才能用 Jade 做到这一点? how would I do this 我几乎可以做任何事情,除了引入一个 span 中间句子。 最佳答案 h3.blur. how would I do t
这似乎是一个非常简单的问题,但我只是想澄清我的疑问。我正在查看其他开发人员编写的代码。有一些涉及 float 的计算。 示例:Float fNotAvlbl = new Float(-99); 他为什
我想知道第 3 行“if dec:”中的“dec”是什么意思 1 def dec2bin(dec): 2 result='' 3 if dec:
我试图在字符串中查找不包含任何“a”字符的单词。我写了下面的代码,但它不起作用。我怎么能对正则表达式说“不包括”?我不能用“^”符号表示“不是”吗? import re string2 = "asfd
这个问题在这里已经有了答案: Is floating point math broken? (31 个答案) Is floating point arbitrary precision availa
我正在创建一个时尚的文本应用程序,但在某些地方出现错误(“字 rune 字中的字符太多”)。我只写了一个字母,但是当我粘贴它时,它会转换成许多这样的字母:“\uD83C\uDD89”,原始字母是“🆉
我正在尝试检查用户是否在文本框中输入了一个数字值,是否接受了小数位。非常感谢任何帮助。 Private Sub textbox1_AfterUpdate() If IsNumeric(textbox1
我知道一个 Byte 是 8 位,但其他的代表什么?我正在参加一个使用摩托罗拉 68k 架构的汇编类(class),我对目前的词汇感到困惑。 最佳答案 如 operator's manual for
我是一名优秀的程序员,十分优秀!