mirror of
https://github.com/vlang/v.git
synced 2025-08-03 17:57:59 -04:00
net: fix TcpConn.peer_ip/0 to only return the ip address, without the port number (#21831)
This commit is contained in:
parent
ca07347430
commit
547c056bf4
@ -297,7 +297,15 @@ pub fn (c &TcpConn) peer_addr() !Addr {
|
|||||||
|
|
||||||
// peer_ip retrieves the ip address used by the peer, and returns it as a string
|
// peer_ip retrieves the ip address used by the peer, and returns it as a string
|
||||||
pub fn (c &TcpConn) peer_ip() !string {
|
pub fn (c &TcpConn) peer_ip() !string {
|
||||||
return c.peer_addr()!.str()
|
address := c.peer_addr()!.str()
|
||||||
|
if address.contains(']:') {
|
||||||
|
// ipv6 addresses similar to this: '[::1]:46098'
|
||||||
|
ip := address.all_before(']:').all_after('[')
|
||||||
|
return ip
|
||||||
|
}
|
||||||
|
// ipv4 addresses similar to '127.0.0.1:7346'
|
||||||
|
ip := address.all_before(':')
|
||||||
|
return ip
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (c &TcpConn) addr() !Addr {
|
pub fn (c &TcpConn) addr() !Addr {
|
||||||
|
@ -64,6 +64,10 @@ fn test_socket() {
|
|||||||
|
|
||||||
fn test_socket_write_and_read() {
|
fn test_socket_write_and_read() {
|
||||||
mut server, mut client, mut socket := setup()
|
mut server, mut client, mut socket := setup()
|
||||||
|
addr := socket.peer_addr()!
|
||||||
|
ip := socket.peer_ip()!
|
||||||
|
assert ip in ['::1', 'localhost', '127.0.0.1']
|
||||||
|
println('> ip: ${ip} | addr: ${addr}')
|
||||||
defer {
|
defer {
|
||||||
cleanup(mut server, mut client, mut socket)
|
cleanup(mut server, mut client, mut socket)
|
||||||
}
|
}
|
||||||
|
@ -35,6 +35,9 @@ fn echo(address string) ! {
|
|||||||
|
|
||||||
println('local: ' + c.addr()!.str())
|
println('local: ' + c.addr()!.str())
|
||||||
println(' peer: ' + c.peer_addr()!.str())
|
println(' peer: ' + c.peer_addr()!.str())
|
||||||
|
ip := c.peer_ip()!
|
||||||
|
println(' ip: ${ip}')
|
||||||
|
assert ip in ['::1', 'localhost', '127.0.0.1']
|
||||||
|
|
||||||
data := 'Hello from vlib/net!'
|
data := 'Hello from vlib/net!'
|
||||||
c.write_string(data)!
|
c.write_string(data)!
|
||||||
|
Loading…
x
Reference in New Issue
Block a user