示例#1
0
文件: udt.go 项目: kambeena/udtgo
func Connect(socket *Socket, host string, portno int) (retval int, err error) {

	addrs, err := net.LookupHost(host)

	if err != nil {
		return -1, fmt.Errorf("Unable to connect to the socket: %s", err)
	}

	resolvedHost := addrs[0]

	if socket.af == syscall.AF_INET6 {
		resolvedHost = fmt.Sprintf("[%s]", resolvedHost)
	}

	rsa, salen, err := createSockaddr(resolvedHost, portno)

	if err != nil {
		return -1, fmt.Errorf("could not convert syscall.Sockaddr to syscall.RawSockaddrAny %s", err)
	}

	retval = int(C.udt_connect(socket.sock, (*C.struct_sockaddr)(unsafe.Pointer(rsa)),
		C.int(salen)))
	if retval != 0 {
		return retval, udtErrDesc("Unable to connect to the socket")
	}
	return
}
示例#2
0
func (fd *udtFD) connect(raddr *UDTAddr) error {
	if err := fd.incref(); err != nil {
		return err
	}
	defer fd.decref()

	_, sa, salen, err := raddr.socketArgs()
	if err != nil {
		return err
	}
	csa := (*C.struct_sockaddr)(unsafe.Pointer(sa))

	if C.udt_connect(fd.sock, csa, C.int(salen)) == C.ERROR {
		err := lastError()
		return fmt.Errorf("error connecting: %s", err)
	}

	fd.raddr = raddr
	return fd.setAsyncOpts()
}