package sircd import ( "fmt" "net" "os" ) type sircd struct { Running bool netName string ircPort uint16 httpPort uint16 ircListener net.Listener ircClients []*clientIRCConnection } type clientIRCConnection struct { conn net.Conn } const ( CONN_HOST = "localhost" CONN_PORT = "6667" CONN_TYPE = "tcp" ) func NewSircd() *sircd { s := new(sircd) s.Running = true return s } func (s *sircd) startServer() { var err error s.ircListener, err = net.Listen(CONN_TYPE, CONN_HOST+":"+CONN_PORT) if err != nil { fmt.Println("Error listening:", err.Error()) os.Exit(1) } defer s.ircListener.Close() fmt.Println("Listening on " + CONN_HOST + ":" + CONN_PORT) fmt.Printf("%T\n",s.ircListener) for { // Listen for an incoming connection. conn, err := s.ircListener.Accept() if err != nil { fmt.Println("Error accepting: ", err.Error()) os.Exit(1) } // Handle connections in a new goroutine. go handleRequest(conn) } } func (s *sircd) Start() { go func() { s.startServer() }() } // Handles incoming requests. func handleRequest(conn net.Conn) { // Make a buffer to hold incoming data. buf := make([]byte, 1024) // Read the incoming connection into the buffer. byteLen, err := conn.Read(buf) if err != nil { fmt.Println("Error reading:", err.Error()) } // Send a response back to person contacting us. conn.Write([]byte("Message received.")) fmt.Println("Received message (%i bytes): %s", byteLen, buf) // Close the connection when you're done with it. conn.Close() }