- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
感谢这里的帮助,我得到了一个基于 poll() 的大部分可用的 socks4 代理。我正在使用这个程序来学习 C 和套接字编程。该程序仍然缺少对部分写入的 send() 检查,但我相当确定这不是当前问题的原因。它在某些代理 TCP 连接上工作正常,例如 netcat 甚至 chrome(尽管会抛出过多的 recv() 错误)。它不适用于 firefox 或 ssh。我不确定有什么区别,但我想我一定是在不正确地处理套接字。该错误当前在不工作的客户端中出现:
我仔细检查了 tcp 段有效负载,它与来自代理和直接来自服务器的连接的 tcp 段完全相同。例如,ssh 客户端应该在收到 ssh 服务器 hello 后向服务器发送 ssh 客户端 hello。
如果我通过 netcat(作为客户端)连接,我可以验证我是否正确获得了横幅:
root@ubuntu# nc -X 4 -x 127.0.0.1:8000 127.0.0.1 22
SSH-2.0-OpenSSH_5.3p1 Debian-3ubuntu7
我在 wireshark 中看到 ssh 客户端也得到了这个横幅,但他什么也没做。同样的行为发生在 SSL 连接和 firefox 中的正常连接(firefox 告诉我是否要保存“二进制数据”)。我的代码中可能有问题:
#include <stdio.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <poll.h>
#include <signal.h>
#include <errno.h>
#define RCVBUFSIZE 1000000
void ProxyData(int rcvSocket)
{
char rcvBuffer[RCVBUFSIZE];
char sndBuffer[RCVBUFSIZE];
int recvMsgSize;
int sndMsgSize;
char Socks4Response[] = "\x00\x5a\x00\x00\x00\x00\x00\x00";
int errno;
int dstSocket;
struct sockaddr_in dstAddr;
struct pollfd fds[2];
int timeout_msecs = 67000; /* dont use this yet */
/* Receive message from client */
if ((recvMsgSize = recv(rcvSocket, rcvBuffer, RCVBUFSIZE, 0)) < 0)
{
perror("recv() failed");
close(rcvSocket);
exit;
}
/* Send Sock 4 Response... this is not robust ofc... */
if ((rcvBuffer[0] == 0x04))
if (send(rcvSocket, Socks4Response, 9, 0) < 0)
{
perror("send() failed");
close (rcvSocket);
exit;
}
/* todo implement socks error responsees*/
/* setting up the destination socket for the socks request */
if((dstSocket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
{
perror("socket() failed");
close(rcvSocket);
exit;
}
memset(&dstAddr, 0, sizeof(dstAddr));
dstAddr.sin_family = AF_INET;
memcpy(&dstAddr.sin_addr.s_addr, &rcvBuffer[4], 4);
memcpy(&dstAddr.sin_port, &rcvBuffer[2], 2);
if (connect(dstSocket, (struct sockaddr *) &dstAddr, sizeof(dstAddr)) < 0 )
{
perror("connect() failed");
close(rcvSocket);
close(dstSocket);
exit;
}
fds[0].fd = rcvSocket;
fds[1].fd = dstSocket;
fds[0].events = POLLIN;
fds[1].events = POLLIN;
signal(SIGPIPE, SIG_IGN);
fcntl(rcvSocket, F_SETFL, O_NONBLOCK);
fcntl(dstSocket, F_SETFL, O_NONBLOCK);
recvMsgSize = 1; /* set this so condition doesnt fail on first while loop */
sndMsgSize = 1; /* see above */
while (1)
{
poll(fds, 2, -1); /* polling indefinately */
/* client to server block */
if ((fds[0].revents & POLLIN) && (recvMsgSize > 0)) /* if data is available and the socket wasnt closed before we read on it */
{
recvMsgSize = recv(rcvSocket, rcvBuffer, RCVBUFSIZE, 0);
if (recvMsgSize > 0)
{
sndcheck = send(dstSocket, rcvBuffer, recvMsgSize, 0);
if (sndcheck < 0)
{
perror("send() dstSocket failed");
if ((errno != EINTR) && (errno != EWOULDBLOCK)) break; /* connection failure -> going to close() outside loop*/
}
}
if (recvMsgSize == 0) shutdown(dstSocket, SHUT_WR);
if (recvMsgSize < 0)
{
perror("recv() rcvSocket failed");
if ((errno != EINTR) && (errno != EWOULDBLOCK)) break; /*connection failure -> going to close() outside loop*/
}
}
/* server to client block */
if ((fds[1].revents & POLLIN) && (sndMsgSize > 0)) /* if data is available and the socket wasnt closed before we read on it */
{
sndMsgSize = recv(dstSocket, sndBuffer, RCVBUFSIZE, 0);
if (sndMsgSize > 0)
{
if (send(rcvSocket, sndBuffer, sndMsgSize, 0) < 0)
{
perror("send() rcvSocket failed");
if ((errno != EINTR) && (errno != EWOULDBLOCK)) break; /* connection failure -> going to close() outside loop*/
}
}
if (sndMsgSize == 0) shutdown(rcvSocket, SHUT_WR);
if (sndMsgSize < 0)
{
perror("recv() dstSocket failed");
if ((errno != EINTR) && (errno != EWOULDBLOCK)) break; /* connection failure -> going to close() outside loop*/
}
}
if ((sndMsgSize == 0) && (recvMsgSize == 0)) break; /* both sockets shutdowned() cleanly, close() outside loop*/
}
close(rcvSocket);
close(dstSocket);
}
添加信息:例如,这是来自代理与 ssh 服务器的 TCP 段 - 第一个被客户端忽略并且连接超时,第二个得到 ssh 客户端响应。我完全困惑,因为它是完全相同的有效载荷:
0000 00 50 56 c0 00 08 00 0c 29 38 32 d4 08 00 45 00 .PV.....)82...E.
0010 00 4f 4a 21 40 00 40 06 be b4 c0 a8 58 81 c0 a8 .OJ!@.@.....X...
0020 58 01 1f 40 c1 08 a1 29 ff 93 72 1b 9c a6 50 18 X..@...)..r...P.
0030 00 b7 2f 63 00 00 53 53 48 2d 32 2e 30 2d 4f 70 ../c..SSH-2.0-Op
0040 65 6e 53 53 48 5f 35 2e 33 70 31 20 44 65 62 69 enSSH_5.3p1 Debi
0050 61 6e 2d 33 75 62 75 6e 74 75 37 0d 0a an-3ubuntu7..
0000 00 50 56 c0 00 08 00 0c 29 38 32 d4 08 00 45 00 .PV.....)82...E.
0010 00 4f 2d c9 40 00 40 06 db 0c c0 a8 58 81 c0 a8 .O-.@.@.....X...
0020 58 01 00 16 c1 0a 74 9e bf a4 9b 43 5e c4 50 18 X.....t....C^.P.
0030 00 b7 cf bf 00 00 53 53 48 2d 32 2e 30 2d 4f 70 ......SSH-2.0-Op
0040 65 6e 53 53 48 5f 35 2e 33 70 31 20 44 65 62 69 enSSH_5.3p1 Debi
0050 61 6e 2d 33 75 62 75 6e 74 75 37 0d 0a an-3ubuntu7..
最佳答案
如您所说,您没有处理部分写入。鉴于缓冲区的极端大小,写入过早返回并非不可能。仅用于测试:尝试将缓冲区大小降低到合理的数量(几百字节)。
另外,为了让这个东西更健壮,你真的需要添加一些(循环)缓冲代码。
关于c - 代理中的非阻塞套接字和 poll() 怪癖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7287721/
对于一个简单的聊天程序,我使用了一个通过 boost::python 包装的 c 库。 使用 PyQT 编写了一个简单的 GUI。接收消息是通过阻塞调用完成的lib说。对于独立刷新的 GUI,通信部分
当我创建以下内容时,我试图创建一个可以被异常终止的线程类(因为我试图让线程等待一个事件): import sys class testThread(threading.Thread): def
我正在用 Haskell 编写服务器,我想在客户端断开连接后显式关闭它们。当我调用 hClose ,线程将阻塞,直到客户端关闭其一侧的句柄。有没有办法让它在不阻塞的情况下关闭? 提前致谢! 最佳答案
这个问题已经有答案了: 已关闭12 年前。 Possible Duplicate: garbage collection Operation 我有几个相关问题。 1.JAVA垃圾收集器运行时,是否占用
我有一个 Angular 函数,它在初始 URL 中查找“列表”参数,如果找到,就会出去获取信息。否则我想获得地理位置。如果存在 URL 参数,我不想获取地理位置。我使用的术语是否正确? constr
我读了很多关于锁定数据库、表和行的文章,但我想要较低的锁定,比如只锁定“操作”,我不知道如何调用它,假设我在 php 中有函数: function update_table() { //que
在我的多线程 mfc 应用程序中,m_view->SetScrollPos 处于阻塞状态并且所有应用程序都被卡住。 View 是在另一个线程中创建的,这是这种行为的原因吗? //SetScrollPo
FreeSwitch 软件在几天内运行良好(~3 - 5 天),然后由于 FreeSwitch 被阻止,新的来电请求被接受!!正在进行的调用继续他们的 session ,他们的调用似乎没有受到影响,但
我有一组按钮,当鼠标悬停在这些按钮上时,它们会改变颜色。这些的 CSS 以这种方式运行: #navsite ul li button { height: 60px; width: 60
由于某些原因,当我调用 WSARecvFrom 时,该函数在接收到某些内容之前不会返回。 _socket = WSASocket(AF_INET, SOCK_DGRAM, IPPROTO_UDP, N
我了解一些关于 Oracle 阻塞的知识——更新如何阻塞其他更新直到事务完成,写入者如何不阻塞读取者等。 我理解悲观和乐观锁定的概念,以及有关丢失更新等典型银行教科书示例。 我也理解 JDBC 事务隔
在两个代码点之间,我是否可以判断进程是否已被内核抢占,或者更确切地说,当时是否有任何其他代码在同一处理器上运行? //Point A some_type capture = some_capture(
这是我在 Oracle 的面试问题。 有一个堆栈,即使堆栈已满,push 操作也应该等到它完成,即使堆栈为空,pop 操作也应该等到它完成。 我们怎样才能做到这一点? 我的回答 让一个线程做push
我想知道是否有人可以告诉我如何有效地使用循环平铺/循环阻塞进行大型密集矩阵乘法。我正在用 1000x1000 矩阵做C = AB。我按照 Wikipedia 上的循环平铺示例进行操作,但使用平铺得到的
我正在阅读有关绿色线程的内容,并且能够理解这些线程是由 VM 或在运行时创建的,而不是由操作系统创建的,但我无法理解以下语句 When a green thread executes a blocki
我正在创建的 JavaScript API 具有以下结构: var engine = new Engine({ engineName: "TestEngine", engineHost
ChildWindow 是一个模态窗口,但它不会阻塞。有没有办法让它阻塞?我基本上想要一个 ShowDialog() 方法,该方法将调用 ChildWindow.Show() 但在用户关闭 Child
我需要一些关于如何调试 10.6 版本下的 Cocoa 并发问题的指导。我正在将“for”循环转换为使用 NSOperations,但大多数时候,代码只是在循环的某个时刻卡住。我可以在控制台中看到 N
我正在使用 ReportViewer 控件和自定义打印作业工作流程,这给我带来了一些问题。我的代码看起来有点像这样: ids.ForEach(delegate(Guid? guid)
我有以下成功复制文件的代码。但是,它有两个问题: progressBar.setValue() 之后的 System.out.println() 不会打印 0 到 100 之间的间隔(仅打印“0”直到
我是一名优秀的程序员,十分优秀!