本文整理汇总了Golang中syscall.Listen函数的典型用法代码示例。如果您正苦于以下问题:Golang Listen函数的具体用法?Golang Listen怎么用?Golang Listen使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Listen函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: listenStream
func listenStream(netw, addr string) (l net.Listener, err error) {
var (
file *os.File
)
fd, err := listen(netw, addr)
if err != nil {
return nil, err
}
// Set backlog size to the maximum
if err = syscall.Listen(fd, syscall.SOMAXCONN); err != nil {
syscall.Close(fd)
return nil, err
}
file = os.NewFile(uintptr(fd), filePrefix+strconv.Itoa(os.Getpid()))
if l, err = net.FileListener(file); err != nil {
syscall.Close(fd)
return nil, err
}
if err = file.Close(); err != nil {
syscall.Close(fd)
l.Close()
return nil, err
}
return l, err
}
示例2: Listen
// Listen wraps syscall.Listen.
func (sw *Switch) Listen(s syscall.Handle, backlog int) (err error) {
so := sw.sockso(s)
if so == nil {
return syscall.Listen(s, backlog)
}
sw.fmu.RLock()
f, _ := sw.fltab[FilterListen]
sw.fmu.RUnlock()
af, err := f.apply(so)
if err != nil {
return err
}
so.Err = syscall.Listen(s, backlog)
if err = af.apply(so); err != nil {
return err
}
sw.smu.Lock()
defer sw.smu.Unlock()
if so.Err != nil {
sw.stats.getLocked(so.Cookie).ListenFailed++
return so.Err
}
sw.stats.getLocked(so.Cookie).Listened++
return nil
}
示例3: so_listen
func so_listen(fd int, queue int) error {
if queue < 1 {
return os.NewSyscallError("so_listen", syscall.Listen(fd, syscall.SOMAXCONN))
}
return os.NewSyscallError("so_listen", syscall.Listen(fd, queue))
}
示例4: ListenAX25
// ListenAX25 announces on the local port axPort using mycall as the local address.
func ListenAX25(axPort, mycall string) (net.Listener, error) {
if _, err := loadPorts(); err != nil {
return nil, err
}
// Setup local address (via callsign of supplied axPort)
localAddr := newAX25Addr(mycall)
if err := localAddr.setPort(axPort); err != nil {
return nil, err
}
// Create file descriptor
var socket fd
if f, err := syscall.Socket(syscall.AF_AX25, syscall.SOCK_SEQPACKET, 0); err != nil {
return nil, err
} else {
socket = fd(f)
}
if err := socket.bind(localAddr); err != nil {
return nil, err
}
if err := syscall.Listen(int(socket), syscall.SOMAXCONN); err != nil {
return nil, err
}
return ax25Listener{
sock: fd(socket),
localAddr: AX25Addr{localAddr},
}, nil
}
示例5: Bind
// Create a tcp socket, setting the TCP_FASTOPEN socket option.
func (s *TFOServer) Bind() (err error) {
s.fd, err = syscall.Socket(syscall.AF_INET, syscall.SOCK_STREAM, 0)
if err != nil {
if err == syscall.ENOPROTOOPT {
err = errors.New("TCP Fast Open server support is unavailable (unsupported kernel).")
}
return
}
err = syscall.SetsockoptInt(s.fd, syscall.SOL_TCP, TCP_FASTOPEN, 1)
if err != nil {
err = errors.New(fmt.Sprintf("Failed to set necessary TCP_FASTOPEN socket option: %s", err))
return
}
sa := &syscall.SockaddrInet4{Addr: s.ServerAddr, Port: s.ServerPort}
err = syscall.Bind(s.fd, sa)
if err != nil {
err = errors.New(fmt.Sprintf("Failed to bind to Addr: %v, Port: %d, Reason: %s", s.ServerAddr, s.ServerPort, err))
return
}
log.Printf("Server: Bound to addr: %v, port: %d\n", s.ServerAddr, s.ServerPort)
err = syscall.Listen(s.fd, LISTEN_BACKLOG)
if err != nil {
err = errors.New(fmt.Sprintf("Failed to listen: %s", err))
return
}
return
}
示例6: main
func main() {
acceptingFd, err := syscall.Socket(syscall.AF_INET, syscall.SOCK_STREAM, 0)
check(err)
addr := &syscall.SockaddrInet4{Port: 3000, Addr: [4]byte{0, 0, 0, 0}}
err = syscall.Bind(acceptingFd, addr)
check(err)
err = syscall.Listen(acceptingFd, 100)
check(err)
for {
connectionFd, _, err := syscall.Accept(acceptingFd)
check(err)
fmt.Println("Accepted new connectrion")
data := make([]byte, 1024)
_, err = syscall.Read(connectionFd, data)
check(err)
fmt.Printf("Received: %s\n", string(data))
_, err = syscall.Write(connectionFd, data)
check(err)
err = syscall.Close(connectionFd)
check(err)
}
}
示例7: ProxyNetListen
func (io *NetIOManager) ProxyNetListen(sa syscall.Sockaddr) error {
serverfd, err := syscall.Socket(syscall.AF_INET, syscall.SOCK_STREAM,
syscall.IPPROTO_TCP)
if err != nil {
goto Error
}
err = syscall.Bind(serverfd, sa)
if err != nil {
goto Cleanup
}
err = syscall.Listen(serverfd, io.max_backlog)
if err != nil {
goto Cleanup
}
err = syscall.EpollCtl(io.epoll_fd, syscall.EPOLL_CTL_ADD, serverfd,
&syscall.EpollEvent{Events: syscall.EPOLLIN, Fd: int32(serverfd)})
if err != nil {
goto Cleanup
}
io.proxy_server_fd = serverfd
return nil
Cleanup:
syscall.Close(serverfd)
Error:
return err
}
示例8: CreateTCPSocket
func CreateTCPSocket(proto, addr string) (net.Listener, error) {
var err error
var laddr *net.TCPAddr
laddr, err = net.ResolveTCPAddr(proto, addr)
if err != nil {
return nil, err
}
family, ipv6only := favoriteTCPAddrFamily(proto, laddr, "listen")
var socketAddr syscall.Sockaddr
if socketAddr, err = ipToSockaddr(family, laddr.IP, laddr.Port, laddr.Zone); err != nil {
panic(err)
return nil, err
}
var s int
if s, err = sysSocket(family, syscall.SOCK_STREAM, 0); err != nil {
return nil, err
}
if err = setDefaultSockopts(s, family, syscall.SOCK_STREAM, ipv6only); err != nil {
closesocket(s)
return nil, err
}
if err = setDefaultListenerSockopts(s); err != nil {
closesocket(s)
return nil, err
}
if err = syscall.SetsockoptInt(s, syscall.SOL_SOCKET, SO_REUSEPORT, 1); err != nil {
closesocket(s)
panic(err)
return nil, err
}
if err = syscall.Bind(s, socketAddr); err != nil {
closesocket(s)
return nil, err
}
if err = syscall.Listen(s, maxListenerBacklog()); err != nil {
closesocket(s)
return nil, err
}
file := os.NewFile(uintptr(s), "listener-"+laddr.String())
defer file.Close()
var socketListener net.Listener
if socketListener, err = net.FileListener(file); err != nil {
return nil, err
}
return socketListener, nil
}
示例9: Listen
// Listen returns TCP listener with SO_REUSEPORT option set.
//
// Only tcp4 network is supported.
//
// ErrNoReusePort error is returned if the system doesn't support SO_REUSEPORT.
func Listen(network, addr string) (l net.Listener, err error) {
var (
soType, fd int
file *os.File
sockaddr syscall.Sockaddr
)
if sockaddr, soType, err = getSockaddr(network, addr); err != nil {
return nil, err
}
syscall.ForkLock.RLock()
fd, err = syscall.Socket(soType, syscall.SOCK_STREAM, syscall.IPPROTO_TCP)
if err == nil {
syscall.CloseOnExec(fd)
}
syscall.ForkLock.RUnlock()
if err != nil {
return nil, err
}
if err = syscall.SetsockoptInt(fd, syscall.SOL_SOCKET, syscall.SO_REUSEADDR, 1); err != nil {
syscall.Close(fd)
return nil, err
}
if err = syscall.SetsockoptInt(fd, syscall.SOL_SOCKET, soReusePort, 1); err != nil {
syscall.Close(fd)
return nil, &ErrNoReusePort{err}
}
if err = syscall.Bind(fd, sockaddr); err != nil {
syscall.Close(fd)
return nil, err
}
if err = syscall.Listen(fd, syscall.SOMAXCONN); err != nil {
syscall.Close(fd)
return nil, err
}
name := fmt.Sprintf("reuseport.%d.%s.%s", os.Getpid(), network, addr)
file = os.NewFile(uintptr(fd), name)
if l, err = net.FileListener(file); err != nil {
file.Close()
return nil, err
}
if err = file.Close(); err != nil {
l.Close()
return nil, err
}
return l, err
}
示例10: listen2
func listen2(path string) int {
_, err := os.Stat(path)
if err == nil {
os.Remove(path)
}
fd, _ := syscall.Socket(syscall.AF_LOCAL,
syscall.SOCK_STREAM|syscall.SOCK_CLOEXEC, 0)
addr := &syscall.SockaddrUnix{Name: path}
syscall.Bind(fd, addr)
syscall.Listen(fd, 1)
return fd
}
示例11: NewReusablePortListener
// NewReusablePortListener returns net.FileListener that created from
// a file discriptor for a socket with SO_REUSEPORT option.
func NewReusablePortListener(proto, addr string) (l net.Listener, err error) {
var (
soType, fd int
file *os.File
sockaddr syscall.Sockaddr
)
if sockaddr, soType, err = getSockaddr(proto, addr); err != nil {
return nil, err
}
syscall.ForkLock.RLock()
if fd, err = syscall.Socket(soType, syscall.SOCK_STREAM, syscall.IPPROTO_TCP); err != nil {
return nil, err
}
syscall.ForkLock.RUnlock()
if err = syscall.SetsockoptInt(fd, syscall.SOL_SOCKET, syscall.SO_REUSEADDR, 1); err != nil {
syscall.Close(fd)
return nil, err
}
if err = syscall.SetsockoptInt(fd, syscall.SOL_SOCKET, reusePort, 1); err != nil {
syscall.Close(fd)
return nil, err
}
if err = syscall.Bind(fd, sockaddr); err != nil {
syscall.Close(fd)
return nil, err
}
// Set backlog size to the maximum
if err = syscall.Listen(fd, listenerBacklogMaxSize); err != nil {
syscall.Close(fd)
return nil, err
}
file = os.NewFile(uintptr(fd), getSocketFileName(proto, addr))
if l, err = net.FileListener(file); err != nil {
syscall.Close(fd)
return nil, err
}
if err = file.Close(); err != nil {
syscall.Close(fd)
return nil, err
}
return l, err
}
示例12: TuneAndListenTCP
// TuneAndListenTCP announces on the TCP address laddr and returns a TCP
// listener. The configuration config indicates additional socket options
// set on the listener socket.
func TuneAndListenTCP(net string, laddr *TCPAddr, tuners ...Tuner) (Listener, error) {
var err error
family, ipv6only := favoriteTCPAddrFamily(net, laddr, "listen")
var socketAddr syscall.Sockaddr
if socketAddr, err = ipToSockaddr(family, laddr.IP, laddr.Port, laddr.Zone); err != nil {
return nil, err
}
var s int
if s, err = sysSocket(family, syscall.SOCK_STREAM, 0); err != nil {
return nil, err
}
if err = setDefaultSockopts(s, family, syscall.SOCK_STREAM, ipv6only); err != nil {
closesocket(s)
return nil, err
}
if err = setDefaultListenerSockopts(s); err != nil {
closesocket(s)
return nil, err
}
for _, tuner := range tuners {
if err := tuner(s); err != nil {
return nil, err
}
}
if err = syscall.Bind(s, socketAddr); err != nil {
closesocket(s)
return nil, err
}
if err = syscall.Listen(s, maxListenerBacklog()); err != nil {
closesocket(s)
return nil, err
}
file := os.NewFile(uintptr(s), "listener-"+laddr.String())
defer file.Close()
var socketListener Listener
if socketListener, err = FileListener(file); err != nil {
return nil, err
}
return socketListener, nil
}
示例13: NewReusablePortListener
// NewReusablePortListener returns net.FileListener that created from a file discriptor for a socket with SO_REUSEPORT option.
func NewReusablePortListener(proto, addr string) (l net.Listener, err error) {
var (
soType, fd int
file *os.File
sockaddr syscall.Sockaddr
)
if sockaddr, soType, err = getSockaddr(proto, addr); err != nil {
return nil, err
}
if fd, err = syscall.Socket(soType, syscall.SOCK_STREAM, syscall.IPPROTO_TCP); err != nil {
return nil, err
}
defer func() {
if err != nil {
syscall.Close(fd)
}
}()
if err = syscall.SetsockoptInt(fd, syscall.SOL_SOCKET, syscall.SO_REUSEADDR, 1); err != nil {
return nil, err
}
if err = syscall.SetsockoptInt(fd, syscall.SOL_SOCKET, reusePort, 1); err != nil {
return nil, err
}
if err = syscall.Bind(fd, sockaddr); err != nil {
return nil, err
}
// Set backlog size to the maximum
if err = syscall.Listen(fd, listenerBacklog); err != nil {
return nil, err
}
// File Name get be nil
file = os.NewFile(uintptr(fd), filePrefix+strconv.Itoa(os.Getpid()))
if l, err = net.FileListener(file); err != nil {
return nil, err
}
if err = file.Close(); err != nil {
return nil, err
}
return l, err
}
示例14: ListenTCP
// ListenTCP announces on the TCP address laddr and returns a TCP listener.
// Net must be "tcp", "tcp4", or "tcp6".
// If laddr has a port of 0, it means to listen on some available port.
// The caller can use l.Addr() to retrieve the chosen address.
func ListenTCP(net string, laddr *TCPAddr) (*TCPListener, error) {
fd, err := internetSocket(net, laddr.toAddr(), nil, syscall.SOCK_STREAM, 0, "listen", sockaddrToTCP)
if err != nil {
return nil, err
}
err = syscall.Listen(fd.sysfd, listenerBacklog)
if err != nil {
closesocket(fd.sysfd)
return nil, &OpError{"listen", net, laddr, err}
}
l := new(TCPListener)
l.fd = fd
return l, nil
}
示例15: ListenTCP
// ListenTCP announces on the TCP address laddr and returns a TCP listener.
// Net must be "tcp", "tcp4", or "tcp6".
// If laddr has a port of 0, it means to listen on some available port.
// The caller can use l.Addr() to retrieve the chosen address.
func ListenTCP(net string, laddr *TCPAddr) (l *TCPListener, err os.Error) {
fd, err := internetSocket(net, laddr.toAddr(), nil, syscall.SOCK_STREAM, "listen", sockaddrToTCP)
if err != nil {
return nil, err
}
errno := syscall.Listen(fd.sysfd, listenBacklog())
if errno != 0 {
syscall.Close(fd.sysfd)
return nil, &OpError{"listen", "tcp", laddr, os.Errno(errno)}
}
l = new(TCPListener)
l.fd = fd
return l, nil
}