HTTP server from TCP socket (Go)


AjMyyra :

I'm trying to create a Go TCP socket, bind it to a VRF interface , and set up an HTTP server on that specific interface. The VRF binding works correctly, but upon starting the HTTP server returns an error stating "Accept TCP 127.0.0.1:80: Accept: Invalid argument" . Am I right to assume that the socket is some kind of defect that I created wrong?

Below is a simplified version to reproduce the problem. The VRF part is commented out because it doesn't affect the actual problem, but I'm taking it here as someone I'm trying to avoid telling me to just use net.Listen instead of sockets. VRF needs to be bound first before you can use it, so net.Listen is not an unfortunate option.

package main

import (
    "fmt"
    "net"
    "net/http"
    "os"
    "syscall"
)

func main() {
    fd, err := syscall.Socket(syscall.AF_INET, syscall.SOCK_STREAM, syscall.IPPROTO_TCP)
    if err != nil {
        fmt.Printf("Error creating socket: %v", err)
        os.Exit(1)
    }

    // if err = syscall.SetsockoptString(fd, syscall.SOL_SOCKET, syscall.SO_BINDTODEVICE, "vrfiface"); err != nil {
    //  fmt.Printf("Error binding to vrf: %v", err)
    //  os.Exit(1)
    // }

    sockAddr := &syscall.SockaddrInet4{
        Port: 80,
        Addr: [4]byte{127, 0, 0, 1},
    }

    if err = syscall.Bind(fd, sockAddr); err != nil {
        fmt.Printf("Error binding to IP and port: %v", err)
        os.Exit(1)
    }

    file := os.NewFile(uintptr(fd), "socketfile")
    if file == nil {
        fmt.Println("Error creating file")
        os.Exit(1)
    }

    listener, err := net.FileListener(file)
    if err != nil {
        fmt.Printf("Error creating a listener: %v", err)
        os.Exit(1)
    }

    http.HandleFunc("/", TestServer)
    if err = http.Serve(listener, nil); err != nil {
        fmt.Printf("Error serving HTTP requests: %v", err)
    }
}

func TestServer(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "Test, %s!", r.URL.Path[1:])
}

Any pointers to fix this would be greatly appreciated. Thanks!

Stephen Ulrich:

As a comment mentioned C Han endlessly: you have to listen. The order in which the server is created is to create the socket, bind the IP and port, listen for the call, and then accept incoming connections. If you forget to listen then you will get the error you are seeing. thereby:

if err = syscall.Bind(fd, sockAddr); err != nil {
     ...
}

if err = syscall.Listen(fd, 10); err != nil {
    fmt.Printf("Error listening %v", err)
    os.Exit(1)
}

Related


HTTP server from TCP socket (Go)

AjMyyra : I'm trying to create a Go TCP socket, bind it to a VRF interface , and set up an HTTP server on that specific interface. The VRF binding works correctly, but upon starting the HTTP server returns an error stating "Accept TCP 127.0.0.1:80: Accept: Inv

HTTP server from TCP socket (Go)

AjMyyra : I'm trying to create a Go TCP socket, bind it to a VRF interface , and set up an HTTP server on that specific interface. The VRF binding works correctly, but upon starting the HTTP server returns an error stating "Accept TCP 127.0.0.1:80: Accept: Inv

HTTP server from TCP socket (Go)

AjMyyra : I'm trying to create a Go TCP socket, bind it to a VRF interface , and set up an HTTP server on that specific interface. The VRF binding works correctly, but upon starting the HTTP server returns an error stating "Accept TCP 127.0.0.1:80: Accept: Inv

HTTP server from TCP socket (Go)

AjMyyra : I'm trying to create a Go TCP socket, bind it to a VRF interface , and set up an HTTP server on that specific interface. The VRF binding works correctly, but upon starting the HTTP server returns an error stating "Accept TCP 127.0.0.1:80: Accept: Inv

HTTP server from TCP socket (Go)

AjMyyra : I'm trying to create a Go TCP socket, bind it to a VRF interface , and set up an HTTP server on that specific interface. The VRF binding works correctly, but upon starting the HTTP server returns an error stating "Accept TCP 127.0.0.1:80: Accept: Inv

Server not receiving data from TCP client in GO

travel: Hello I have implemented a server in GO which reads data from the client and prints it. In order to read the network stream, I am reading the conn.Read() method on the server. Below is my code to read a byte from the network stream // return a single b

Server not receiving data from TCP client in GO

travel: Hello I have implemented a server in GO which reads data from the client and prints it. In order to read the network stream, I am reading the conn.Read() method on the server. Below is my code to read a byte from the network stream // return a single b

Server not receiving data from TCP client in GO

travel: Hello I have implemented a server in GO which reads data from the client and prints it. In order to read the network stream, I am reading the conn.Read() method on the server. Below is my code to read a byte from the network stream // return a single b

Server not receiving data from TCP client in GO

travel: Hello I have implemented a server in GO which reads data from the client and prints it. In order to read the network stream, I am reading the conn.Read() method on the server. Below is my code to read a byte from the network stream // return a single b

Server not receiving data from TCP client in GO

travel: Hello I have implemented a server in GO which reads data from the client and prints it. In order to read the network stream, I am reading the conn.Read() method on the server. Below is my code to read a byte from the network stream // return a single b

Server not receiving data from TCP client in GO

travel: Hello I have implemented a server in GO which reads data from the client and prints it. In order to read the network stream, I am reading the conn.Read() method on the server. Below is my code to read a byte from the network stream // return a single b

UDP socket not read from server in Go

MrDSon: I'm working on a fast dns client just for a problem. But I'm having trouble reading the server response because it never comes, and I do know it's because WireShark is turned on and the packets are read. Here is the code sample (8.8.8.8 is Google DNS,

UDP socket not read from server in Go

MrDSon: I'm working on a fast dns client just for a problem. But I'm having trouble reading the server response because it never comes, and I do know it's because WireShark is turned on and the packets are read. Here is the code sample (8.8.8.8 is Google DNS,

UDP socket not read from server in Go

MrDSon: I'm working on a fast dns client just for a problem. But I'm having trouble reading the server response because it never comes, and I do know it's because WireShark is turned on and the packets are read. Here is the code sample (8.8.8.8 is Google DNS,

UDP socket not read from server in Go

MrDSon: I'm working on a fast dns client just for a problem. But I'm having trouble reading the server response because it never comes, and I do know it's because WireShark is turned on and the packets are read. Here is the code sample (8.8.8.8 is Google DNS,

UDP socket not read from server in Go

MrDSon: I'm working on a fast dns client just for a problem. But I'm having trouble reading the server response because it never comes, and I do know it's because WireShark is turned on and the packets are read. Here is the code sample (8.8.8.8 is Google DNS,

TCP socket server

Vikas Sindhi Active Internet connections (w/o servers) Prot Rec Sen Local Address Foreign Address State PID/PXX tcp 1 0 192.169.3.120:64007 192.169.3.104:40968 CLOSE_WAIT - tcp 7 0 192.169.3.120:64007 192.169.3.104:

socket TCP server

User 2922456 I have a question about the internet connection For example, a TCP server supports N simultaneous connections, each of which belongs to other client hosts. The question is how many sockets does the server need? thanks Christian Evanson I think thi

socket TCP server

User 2922456 I have a question about the internet connection For example, a TCP server supports N simultaneous connections, each of which belongs to other client hosts. The question is how many sockets does the server need? thanks Christian Evanson I think thi

TCP socket server

Vikas Sindhi Active Internet connections (w/o servers) Prot Rec Sen Local Address Foreign Address State PID/PXX tcp 1 0 192.169.3.120:64007 192.169.3.104:40968 CLOSE_WAIT - tcp 7 0 192.169.3.120:64007 192.169.3.104:

Recommended way to read from socket in HTTP server

ScrollerBlaster: As a challenge, I have implemented a basic web server in Java. When I open the raw file, InputStreamI immediately enter a blocking read which reads the entire 400 or bytes of the HTTP request into a byte array. This works, but I then don't che

Recommended way to read from socket in HTTP server

ScrollerBlaster: As a challenge, I have implemented a basic web server in Java. When I open the raw file, InputStreamI immediately enter a blocking read which reads the entire 400 or bytes of the HTTP request into a byte array. This works, but I then don't che

http server and web socket from different servers

Alex Configuring the http server (using express) and socket server (socket.io) assigned to it is very easy: var app = require('express')(); var http = require('http').Server(app); var io = require('socket.io')(http); How to run http server and socket server i

http server and web socket from different servers

Alexa Configuring an http server (using express) and a socket server (socket.io) assigned to it is very easy: var app = require('express')(); var http = require('http').Server(app); var io = require('socket.io')(http); How to run http server and socket server

Recommended way to read from socket in HTTP server

ScrollerBlaster: As a challenge, I have implemented a basic web server in Java. When I open the raw file, InputStreamI immediately enter a blocking read which reads the entire 400 or bytes of the HTTP request into a byte array. This works, but I then don't che

http server and web socket from different servers

Alexa Configuring an http server (using express) and a socket server (socket.io) assigned to it is very easy: var app = require('express')(); var http = require('http').Server(app); var io = require('socket.io')(http); How to run http server and socket server

How to flush tcp socket in Go?

Philip Haglund: How to flush tcp socket in Go? I'm sending one message at a time over the socket indicating progress to the client, but the messages are bundled and all sent at the same time. I don't see a flush function anywhere. Messages are sent every few s