本文整理匯總了Golang中net.TCPAddr.Port方法的典型用法代碼示例。如果您正苦於以下問題:Golang TCPAddr.Port方法的具體用法?Golang TCPAddr.Port怎麽用?Golang TCPAddr.Port使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類net.TCPAddr
的用法示例。
在下文中一共展示了TCPAddr.Port方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: 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
}
}
}
示例2: 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
}
示例3: ListenTCP
// ListenTCP requests the remote peer open a listening socket
// on laddr. Incoming connections will be available by calling
// Accept on the returned net.Listener.
func (c *ClientConn) ListenTCP(laddr *net.TCPAddr) (net.Listener, error) {
m := channelForwardMsg{
"tcpip-forward",
true, // sendGlobalRequest waits for a reply
laddr.IP.String(),
uint32(laddr.Port),
}
// send message
resp, err := c.sendGlobalRequest(m)
if err != nil {
return nil, err
}
// If the original port was 0, then the remote side will
// supply a real port number in the response.
if laddr.Port == 0 {
port, _, ok := parseUint32(resp.Data)
if !ok {
return nil, errors.New("unable to parse response")
}
laddr.Port = int(port)
}
// Register this forward, using the port number we obtained.
//
// This does not work on OpenSSH < 6.0, which will send a
// ChannelOpenMsg with port number 0, rather than the actual
// port number.
ch := c.forwardList.add(*laddr)
return &tcpListener{laddr, c, ch}, nil
}
示例4: ListenTCP
// ListenTCP requests the remote peer open a listening socket
// on laddr. Incoming connections will be available by calling
// Accept on the returned net.Listener.
func (c *ClientConn) ListenTCP(laddr *net.TCPAddr) (net.Listener, error) {
if laddr.Port == 0 && isBrokenOpenSSHVersion(c.serverVersion) {
return c.autoPortListenWorkaround(laddr)
}
m := channelForwardMsg{
"tcpip-forward",
true, // sendGlobalRequest waits for a reply
laddr.IP.String(),
uint32(laddr.Port),
}
// send message
resp, err := c.sendGlobalRequest(m)
if err != nil {
return nil, err
}
// If the original port was 0, then the remote side will
// supply a real port number in the response.
if laddr.Port == 0 {
port, _, ok := parseUint32(resp.Data)
if !ok {
return nil, errors.New("unable to parse response")
}
laddr.Port = int(port)
}
// Register this forward, using the port number we obtained.
ch := c.forwardList.add(*laddr)
return &tcpListener{laddr, c, ch}, nil
}
示例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: ListenTCP
// ListenTCP requests the remote peer open a listening socket
// on laddr. Incoming connections will be available by calling
// Accept on the returned net.Listener.
func (c *ClientConn) ListenTCP(laddr *net.TCPAddr) (net.Listener, error) {
m := channelForwardMsg{
"tcpip-forward",
true, // sendGlobalRequest waits for a reply
laddr.IP.String(),
uint32(laddr.Port),
}
// send message
resp, err := c.sendGlobalRequest(m)
if err != nil {
return nil, err
}
// fixup laddr. If the original port was 0, then the remote side will
// supply one in the resp.
if laddr.Port == 0 {
port, _, ok := parseUint32(resp.Data)
if !ok {
return nil, errors.New("unable to parse response")
}
laddr.Port = int(port)
}
// register this forward
ch := c.forwardList.add(laddr)
return &tcpListener{laddr, c, ch}, nil
}
示例7: ListenTCP
// ListenTCP requests the remote peer open a listening socket
// on laddr. Incoming connections will be available by calling
// Accept on the returned net.Listener.
func (c *Client) ListenTCP(laddr *net.TCPAddr) (net.Listener, error) {
if laddr.Port == 0 && isBrokenOpenSSHVersion(string(c.ServerVersion())) {
return c.autoPortListenWorkaround(laddr)
}
m := channelForwardMsg{
laddr.IP.String(),
uint32(laddr.Port),
}
// send message
ok, resp, err := c.SendRequest("tcpip-forward", true, Marshal(&m))
if err != nil {
return nil, err
}
if !ok {
return nil, errors.New("ssh: tcpip-forward request denied by peer")
}
// If the original port was 0, then the remote side will
// supply a real port number in the response.
if laddr.Port == 0 {
var p struct {
Port uint32
}
if err := Unmarshal(resp, &p); err != nil {
return nil, err
}
laddr.Port = int(p.Port)
}
// Register this forward, using the port number we obtained.
ch := c.forwards.add(*laddr)
return &tcpListener{laddr, c, ch}, nil
}
示例8: 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
}
示例9: 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")
}
示例10: webserver
// Start a webserver serving files from the given path. Take the address given and attach the HTTP
// server to a random port. If portCh is non-nil, sends the chosen port on this channel.
func webserver(path string, addr net.TCPAddr, portCh chan int) {
addr.Port = 0
listener, err := net.ListenTCP("tcp", &addr)
if portCh != nil {
portCh <- listener.Addr().(*net.TCPAddr).Port
}
log.Printf("webserver listening on: %s", listener.Addr())
err = http.Serve(listener, http.FileServer(http.Dir(path)))
if err != nil {
panic(err)
}
}
示例11: 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)
}
示例12: Listen
// Opens a TCP server socket and returns a stream listener, ready to accept. If
// an auto-port (0) is requested, the port is updated in the argument.
func Listen(addr *net.TCPAddr) (*Listener, error) {
// Open the server socket
sock, err := net.ListenTCP("tcp", addr)
if err != nil {
return nil, err
}
addr.Port = sock.Addr().(*net.TCPAddr).Port
// Initialize and return the listener
return &Listener{
socket: sock,
Sink: make(chan *Stream),
quit: make(chan chan error),
}, nil
}
示例13: autoPortListenWorkaround
// autoPortListenWorkaround simulates automatic port allocation by
// trying random ports repeatedly.
func (c *Client) autoPortListenWorkaround(laddr *net.TCPAddr) (net.Listener, error) {
var sshListener net.Listener
var err error
const tries = 10
for i := 0; i < tries; i++ {
addr := *laddr
addr.Port = 1024 + portRandomizer.Intn(60000)
sshListener, err = c.ListenTCP(&addr)
if err == nil {
laddr.Port = addr.Port
return sshListener, err
}
}
return nil, fmt.Errorf("ssh: listen on random port failed after %d tries: %v", tries, err)
}
示例14: main
func main() {
fmt.Printf("Hello, 世界\n")
// socket einrichten und auf verbindung warten
var listenAddr *net.TCPAddr = new(net.TCPAddr)
listenAddr.Port = INPORT
listenSock, err := net.ListenTCP("tcp4", listenAddr)
if err != nil {
fmt.Printf(err.String())
}
for {
//establisch connection
conn, _ := listenSock.AcceptTCP()
go handleConnection(conn) // add 'go' to allow multiple connections
}
}
示例15: 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
}