Skip to content

Commit

Permalink
修正TCP客户端获取对端ip端口失败的问题 (ZLMediaKit#182)
Browse files Browse the repository at this point in the history
正在连接对端时,获取不到对端ip和端口;
修改为连接成功后再获取信息可修复此问题。
  • Loading branch information
ljx0305 authored Sep 18, 2023
1 parent 5d74e09 commit 28b7aea
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/Network/Socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,16 +166,19 @@ void Socket::connect_l(const string &url, uint16_t port, const onErrCB &con_cb_i
return;
}

strong_self->setSock(strong_self->makeSock(sock, SockNum::Sock_TCP));
// 监听该socket是否可写,可写表明已经连接服务器成功
int result = strong_self->_poller->addEvent(sock, EventPoller::Event_Write, [weak_self, sock, con_cb](int event) {
if (auto strong_self = weak_self.lock()) {
// socket可写事件,说明已经连接服务器成功
// socket可写事件,说明已经连接服务器成功
strong_self->setSock(strong_self->makeSock(sock, SockNum::Sock_TCP));
strong_self->onConnected(sock, con_cb);
} else {
CLOSE_SOCK(sock);
}
});

if (result == -1) {
CLOSE_SOCK(sock);
con_cb(SockException(Err_other, "add event to poller failed when start connect"));
}
});
Expand Down

0 comments on commit 28b7aea

Please sign in to comment.