Exemple #1
0
func CreateSocket(network string, isStream bool) (socket *Socket, err error) {
	var n C.int

	if network == "ip4" {
		n = C.int(syscall.AF_INET)
	} else if network == "ip6" {
		n = C.int(syscall.AF_INET6)
	} else {
		return nil, fmt.Errorf("network must be either ip4 or ip6")
	}

	var trnType C.int
	if isStream {
		trnType = C.SOCK_STREAM //data streaming
	} else {
		trnType = C.SOCK_DGRAM //messaging
	}

	sock := C.udt_socket(n, trnType, 0)

	if C.int(sock) == C.int(C.UDT_INVALID_SOCK) {
		return nil, udtErrDesc("Invalid socket")
	}

	socket = &Socket{
		sock: sock,
		af:   n,
	}

	return
}
Exemple #2
0
func socket(addrfamily int) (sock C.UDTSOCKET, reterr error) {

	sock = C.udt_socket(C.int(addrfamily), C.SOCK_STREAM, 0)
	if sock == (C.UDTSOCKET)(C.INVALID_SOCK) {
		return (C.UDTSOCKET)(C.INVALID_SOCK), fmt.Errorf("invalid socket: %s", lastError())
	}

	return sock, nil
}