This commit is contained in:
weizhihong 2023-11-21 10:27:18 +08:00
commit 1c8379ada7
2 changed files with 42 additions and 0 deletions

41
third_party/tcp/tcp_client.go vendored Normal file
View File

@ -0,0 +1,41 @@
package tcp
import "net"
type client struct {
laddr *net.TCPAddr //local address
raddr *net.TCPAddr //remote address
con *net.TCPConn //tcp 连接
}
func (c *client) NewClientWithLocal(remoteAddr string, localAddr string) *client {
ra, raEr := net.ResolveTCPAddr("tcp", remoteAddr)
if raEr != nil {
panic(raEr)
}
c.raddr = ra
//
la, laEr := net.ResolveTCPAddr("tcp", localAddr)
if laEr != nil {
panic(laEr)
}
c.laddr = la
return c
}
func (c *client) NewClient(remoteAddr string) *client {
ra, raEr := net.ResolveTCPAddr("tcp", remoteAddr)
if raEr != nil {
panic(raEr)
}
c.raddr = ra
c.con.LocalAddr()
return c
}
func (c *client) Start() error {
con, e := net.DialTCP(c.raddr.Network(), c.laddr, c.raddr)
if e != nil {
return e
}
c.con = con
return nil
}

1
third_party/tcp/tcp_server.go vendored Normal file
View File

@ -0,0 +1 @@
package tcp