本文整理匯總了Golang中net.TCPAddr.IP方法的典型用法代碼示例。如果您正苦於以下問題:Golang TCPAddr.IP方法的具體用法?Golang TCPAddr.IP怎麽用?Golang TCPAddr.IP使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類net.TCPAddr
的用法示例。
在下文中一共展示了TCPAddr.IP方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: Handle
func (s *Server) Handle(conn net.Conn) {
defer conn.Close()
ccfb := cipher.NewCFBDecrypter(s.ClientCipher, iv)
scfb := cipher.NewCFBEncrypter(s.ServerCipher, iv)
out := make([]byte, 1024)
readAndDecode(conn, out[:4], ccfb)
if out[1] != 1 {
log.Print("unsupport command")
return
}
addr := new(net.TCPAddr)
switch out[3] { //atyp
case 1: //ipv4
readAndDecode(conn, out[:4], ccfb)
addr.IP = net.IPv4(out[0], out[1], out[2], out[3])
case 3: //domain
readAndDecode(conn, out[:1], ccfb)
l := out[0]
readAndDecode(conn, out[:l], ccfb)
host := string(out[:l])
addrs, err := net.LookupIP(host)
if err != nil || len(addrs) == 0 {
log.Print("cannot resolve ", host)
return
}
addr.IP = addrs[0]
default:
log.Print("unsupport address type")
return
}
readAndDecode(conn, out[:2], ccfb)
addr.Port = nl2int(out)
sConn, err := net.DialTCP("tcp", nil, addr)
if err != nil {
log.Print("cannot connect to server", addr.String())
encodeAndWrite(conn, out[:10], scfb)
return
}
defer sConn.Close()
response := make([]byte, 10)
response[0] = 5
response[1] = 0
response[2] = 0
response[3] = 1
copy(response[4:], sConn.LocalAddr().(*net.TCPAddr).IP)
copy(response[8:], int2nl(sConn.LocalAddr().(*net.TCPAddr).Port))
encodeAndWrite(conn, response, scfb)
go HandleStream(conn, sConn, scfb)
HandleStream(sConn, conn, ccfb)
}
示例2: ProxyRelay
func ProxyRelay(port string, relays map[string]Relay) error {
tcpAddr := net.TCPAddr{}
tcpAddr.IP = net.ParseIP("127.0.0.1")
tcpAddr.Port, _ = strconv.Atoi(port)
ln, err := net.ListenTCP("tcp", &tcpAddr)
if err != nil {
msg := fmt.Sprintf("Proxy listener error on port %s: %s",
port, err)
LogWriter.Err(msg)
return fmt.Errorf(msg)
}
msg := fmt.Sprintf("Started proxy listener on port: %s",
port)
LogWriter.Info(msg)
defer ln.Close()
for {
conn, err := ln.AcceptTCP()
if err != nil {
msg := fmt.Sprintf("Error accepting TCP connection on port %s: %s",
port, err)
LogWriter.Err(msg)
continue
}
conn.SetKeepAlive(true)
if err != nil {
msg := fmt.Sprintf("Error setting keep-alive on connection: %s",
err)
LogWriter.Err(msg)
}
go acceptClient(conn, relays)
}
return nil
}
示例3: MakeConn
func MakeConn(ip string) (*net.TCPConn, error) {
var addr net.TCPAddr
addr.IP = net.ParseIP(ip)
addr.Port = Port
c, err := net.DialTCP("tcp4", nil, &addr)
return c, err
}
示例4: ts6_main
func ts6_main(msg chan string, exit chan int) {
// No configuration, so this is fixed.
var addr net.TCPAddr
addr.IP = net.IPv4(127, 0, 0, 1)
addr.Port = 3725
// Start our listener.
l, err := net.ListenTCP("tcp4", &addr)
if err != nil {
fmt.Printf("No bind: %s\n", err)
exit <- 0
} else {
go listen(l)
// Again, no configuration.
addr.Port = 13725
c, err := net.DialTCP("tcp4", nil, &addr)
if err != nil {
fmt.Printf("No connection: %s\n", err)
} else {
go link(c, true)
}
}
// Handle messages.
for message := range msg {
if message == "exit" {
exit <- 0
}
}
}
示例5: main
func main() {
if len(os.Args) != 2 {
fmt.Fprintf(os.Stderr, "Usage: %s message", os.Args[0])
os.Exit(1)
}
message := os.Args[1]
serverIP := "127.0.0.1" //サーバ側のIP
serverPort := "55555" //サーバ側のポート番號
myIP := "127.0.0.1" //クライアント側のIP
myPort := 55556 //クライアント側のポート番號
tcpAddr, err := net.ResolveTCPAddr("tcp", serverIP+":"+serverPort)
checkError(err)
myAddr := new(net.TCPAddr)
myAddr.IP = net.ParseIP(myIP)
myAddr.Port = myPort
conn, err := net.DialTCP("tcp", myAddr, tcpAddr)
checkError(err)
defer conn.Close()
conn.SetWriteDeadline(time.Now().Add(10 * time.Second))
conn.Write([]byte(message))
readBuf := make([]byte, 1024)
conn.SetReadDeadline(time.Now().Add(10 * time.Second))
readlen, err := conn.Read(readBuf)
checkError(err)
fmt.Println("server: " + string(readBuf[:readlen]))
}
示例6: TCPAddr
func TCPAddr(buf []byte) (net.TCPAddr, int) {
var value net.TCPAddr
value.IP = make([]byte, 16)
copy(value.IP, buf[:16])
value.Port = int(binary.BigEndian.Uint16(buf[16:18]))
return value, Size
}
示例7: main
func main() {
var localaddr net.TCPAddr
var remoteaddr net.TCPAddr
localaddr.IP = net.ParseIP("192.168.0.109")
localaddr.Port = 0
remoteaddr.IP = net.ParseIP("192.168.0.1")
remoteaddr.Port = 80
if localaddr.IP == nil || remoteaddr.IP == nil {
fmt.Println("error")
}
if _, err := net.DialTCP("tcp", &localaddr, &remoteaddr); err != nil {
fmt.Println(err)
}
fmt.Println("End")
}
示例8: Connect
func (bot *Bot) Connect() (conn net.Conn, err error) {
r, err := net.ResolveTCPAddr("tcp", bot.server+":"+bot.port)
var l net.TCPAddr
l.IP = net.ParseIP(bot.local_ip)
conn, err = net.DialTCP("tcp", nil, r)
if err != nil {
log.Fatal("unable to connect to IRC server ", err)
}
bot.conn = conn
log.Printf("Connected to IRC server %s (%s)\n", bot.server, bot.conn.RemoteAddr())
return bot.conn, nil
}
示例9: Dail
func (this *MsgBusServer) Dail() error {
glog.Infof("Dail to [%s]->[%s]\n", this.localAddr, this.remoteAddr)
var (
err error
tcpLocalAddr net.TCPAddr
tcpRemoteAddr *net.TCPAddr
)
ip, err := net.ResolveIPAddr("ip", this.localAddr)
if err != nil || ip.IP == nil {
glog.Fatalf("Resovle Local TcpAddr [%s], error: %v\n", this.localAddr, err)
}
tcpLocalAddr.IP = ip.IP
//tcpLocalAddr, err = net.ResolveTCPAddr("tcp", this.localAddr)
//if err != nil {
// glog.Errorf("Resovle Local TcpAddr [%s] [%s]\n", this.localAddr, err.Error())
// return err
//}
tcpRemoteAddr, err = net.ResolveTCPAddr("tcp", this.remoteAddr)
if err != nil {
glog.Errorf("Resovle Remote TcpAddr [%s] [%s]\n", this.remoteAddr, err.Error())
return err
}
this.conn, err = net.DialTCP("tcp", &tcpLocalAddr, tcpRemoteAddr)
if err != nil {
glog.Errorf("Dail [%s]->[%s] [%s]\n", this.localAddr, this.remoteAddr, err.Error())
return err
}
buf := make([]byte, 1)
buf[0] = byte(gCometType)
_, err = this.conn.Write(buf)
if err != nil {
glog.Errorf("Send info error [%s]->[%s] [%s]\n", this.localAddr, this.remoteAddr, err.Error())
return err
}
n, err := this.conn.Read(buf)
if err != nil || n != 1 {
glog.Errorf("Read ack error [%s]<-[%s] [%s]\n", this.localAddr, this.remoteAddr, err.Error())
return err
}
// glog.Infof("Dail to [%s] ok\n", this.addr)
return nil
}
示例10: slimprotoConnect
// Connect to slimproto
func slimprotoConnect(addr net.IP, port int) {
sbsAddr := new(net.TCPAddr)
sbsAddr.IP = addr
sbsAddr.Port = port
var err error
slimproto.Conn, err = net.DialTCP("tcp", nil, sbsAddr)
checkError(err)
//slimproto.Conn.SetDeadline(time.Time(10e9))
if *debug {
log.Println("Connected to slimproto")
}
return
}
示例11: startListening
func (l *Listener) startListening(config *core.GeneralConfig) (*net.TCPListener, error) {
var addr net.TCPAddr
addr.IP = net.ParseIP(config.AdminBind)
if addr.IP == nil {
return nil, fmt.Errorf("The admin bind address specified is not a valid IP address")
}
addr.Port = config.AdminPort
listener, err := net.ListenTCP("tcp", &addr)
if err != nil {
return nil, err
}
return listener, nil
}
示例12: Dial
func Dial(netw, addr string, options *heroshi.RequestOptions) (net.Conn, error) {
addrString, addrPort := heroshi.SplitPort(addr)
var ip net.IP
addrs, resolveTime, err := heroshi.ResolveName(addrString, NameServerAddress)
if err != nil {
log.Println("Dial: resolve error:", err.Error(), "in", resolveTime.String())
return nil, err
}
if len(addrs) > 0 {
ip = addrs[0]
// log.Printf("Dial: resolved: %s -> %s in %s", addr, ip.String(), resolveTime.String())
addr = ip.String()
}
if options != nil && options.Stat != nil {
var tcpAddr net.TCPAddr
tcpAddr.IP = ip
options.Stat.RemoteAddr = &tcpAddr
options.Stat.ResolveTime = resolveTime
}
// Return port back
addr += ":" + addrPort
var conn net.Conn
if options != nil && options.ConnectTimeout != 0 {
conn, err = net.DialTimeout(netw, addr, options.ConnectTimeout)
} else {
conn, err = net.Dial(netw, addr)
}
if err != nil {
return conn, err
}
tcpConn, ok := conn.(*net.TCPConn)
if !ok {
return conn, errors.New("Dial: conn->TCPConn type assertion failed.")
}
tcpConn.SetKeepAlive(true)
tcpConn.SetLinger(0)
tcpConn.SetNoDelay(true)
return tcpConn, err
}
示例13: server_A
func server_A(ready chan int) {
ch := []byte{0}
var n int
var listener net.TCPAddr
listener.IP = net.ParseIP("127.0.0.1")
listener.Port = 8888
l, err := net.ListenTCP("tcp4", &listener)
if err != nil {
fmt.Printf("ListenTCP() failed with err: %v\n", err)
os.Exit(1)
}
defer l.Close()
fmt.Printf("server() listener started ok, ready to accept TCP connections\n")
ready <- 1 // send ready signal back to main()
for {
c, err := l.AcceptTCP()
if err != nil {
fmt.Printf("server() accept TCP failed with err: %v\n", err)
os.Exit(1)
}
fmt.Printf("server() accepted TCP connection from client\n")
n, err = c.Read(ch)
if err != nil || n != 1 {
fmt.Printf("server() read TCP failed with err: %v\n", err)
os.Exit(1)
}
ch[0] *= 10 // modify input data and send it back
n, err = c.Write(ch)
if err != nil || n != 1 {
fmt.Printf("server() write to TCP failed with err: %v\n", err)
os.Exit(1)
}
fmt.Printf("server() wrote byte value = %d to TCP\n", ch)
c.Close()
}
}
示例14: connect
func (peer *Peer) connect() bool {
debugl.Println("Calling connect(): ", peer)
p := peer.torrent_peer
dest_addr := new(net.TCPAddr)
dest_addr.IP = net.ParseIP(p.ip)
if dest_addr.IP == nil {
errorl.Println("Couldn't get a valid IP")
return false
}
dest_addr.Port = p.port
var err error
peer.connection, err = net.DialTCP("tcp", nil, dest_addr)
if err != nil {
errorl.Println("Couldn't connect: ", err)
return false
} else {
debugl.Println("Connected to a peer: ", dest_addr.IP, dest_addr.Port)
}
peer.connection.SetKeepAlive(true)
peer.connected = true
return true
}
示例15: clientLoop
func clientLoop(client *net.TCPConn) {
addr := client.RemoteAddr()
defer client.Close()
var versionMethod [2]byte
_, err := io.ReadFull(client, versionMethod[:])
if err != nil {
log.Printf("%v: Failed to read the version and methods number: %v", addr, err)
return
}
if versionMethod[0] != 0x05 {
log.Printf("%v: Only implemented socks5 proxy currently: %X.", addr, versionMethod[0])
return
}
nMethods := versionMethod[1]
if nMethods == 0 {
log.Printf("%v: Must provide one method at least.", addr)
return
}
methods := make([]byte, nMethods)
_, err = io.ReadFull(client, methods)
if err != nil {
log.Printf("%v: Failed to read the methods: %v", addr, err)
return
}
hasMethod0 := false
for i := 0; i < int(nMethods); i++ {
if methods[i] == 0x00 {
hasMethod0 = true
break
}
}
if !hasMethod0 {
log.Printf("%v: Only implemented 'no authentication required' method currently.", addr)
return
}
versionMethod[1] = 0x00
nw, err := client.Write(versionMethod[:])
if err != nil || nw != len(versionMethod) {
log.Printf("%v: Failed to write version and method back to the client: %v", addr, err)
return
}
var requestHeader [4]byte
_, err = io.ReadFull(client, requestHeader[:])
if err != nil {
log.Printf("%v: Failed to read the request header: %v", addr, err)
return
}
var reply [22]byte
reply[0] = 0x05 // VER
reply[2] = 0x00 // RSV
if requestHeader[0] != 0x05 {
log.Printf("%v: Version number in the request does not match the previous one: %X", addr, requestHeader[0])
return
}
if requestHeader[1] != 0x01 {
log.Printf("%v: Only implemented CONNECT command currently.", addr)
reply[1] = 0x07
client.Write(reply[:4])
return
}
if requestHeader[2] != 0x00 {
log.Printf("%v: RESERVED field must be 0.", addr)
return
}
remoteAddress := new(net.TCPAddr)
switch requestHeader[3] {
case 0x01, 0x04:
{
ipLen := 4 * requestHeader[3]
buf := make([]byte, ipLen+2)
_, err = io.ReadFull(client, buf)
if err != nil {
log.Printf("%v: Failed to read requested address: %v", addr, err)
return
}
remoteAddress.IP = buf[:ipLen]
remoteAddress.Port = int(buf[ipLen])<<8 + int(buf[ipLen+1])
reply[3] = requestHeader[3]
}
case 0x03:
{
var hostLen [1]byte
_, err = io.ReadFull(client, hostLen[:])
if err != nil {
log.Printf("%v: Failed to read requested host len: %v", addr, err)
return
}
host := make([]byte, hostLen[0])
_, err = io.ReadFull(client, host)
if err != nil {
log.Printf("%v: Failed to read requested host name: %v", addr, err)
//.........這裏部分代碼省略.........