Issue in http ListenAndServe

Hi,

I am trying to start new server using following code. somehow the server is not coming up and not showing any error informaiton also. Can you please help me here.


package main

import (
code.google.com/p/go.net/websocket
“flag”
“fmt”
“log”
“net/http”
“os”
“time”
)

var htdocsDir = flag.String(“htdocs”, “.”, “htdocs directory”)
var msg string
func main() {
log.Println("…")
log.Println(“Server Starting…step-1”)
flag.Parse()
fmt.Println(“Server Starting…step-2”)

http.Handle("/ws/audio", websocket.Handler(handleWebsocket))
fmt.Println("Server Starting...step-3")

http.HandleFunc("/livestream", streamLiveData)
fmt.Println("Server Starting...step-4")

http.HandleFunc("/openrecfile", openrecfile)
fmt.Println("Server Starting...step-5")

http.Handle("/", http.FileServer(http.Dir(*htdocsDir)))
fmt.Println("Server Starting...step-6")


 err := http.ListenAndServe(":9000", nil)
	
  fmt.Println(err )
  
	
if err != nil {
	log.Fatal("ListenAndServe: ", err)
}
if err != nil {
	fmt.Println("Server started...")
}
fmt.Println("Go Server Started...")

}

func handleWebsocket(s websocket.Conn) {
log.Printf("Opened WebSocket----------------------%s
***************",s.Request().URL.Query().Get(“recording”))
msg := s.Request().URL.Query().Get(“recording”)
log.Printf(“message set----------------------%s****************”,msg)
startTime := time.Now()
/*

if msg == "" {
	errr := websocket.Message.Receive(s, &msg)
	if errr != nil {
		log.Printf("file name not recieved %v", errr)
		return
	}
	log.Printf("if condition")
	
}
*/
log.Printf("file name::::::::%s",msg)
f, err := os.OpenFile(fmt.Sprintf("%s",msg), os.O_APPEND|os.O_CREATE|os.O_RDWR, 0644)
if err != nil {
	log.Printf("os.OpenFile failed: %v", err)
	return
}
/*
err1:= f.Chmod(0777)
if err1 != nil {
	log.Printf("os.Chmode failed: %v", err1)
	return
}*/








defer f.Close()

fmt.Println("ProcessSocket: got message", msg)
//service := msg

sum := 0
count := 0

for {
	data := make([]byte, 8192)
	n, err := s.Read(data)
	if err != nil {
		log.Printf("s.Read failed: %v", err)
		break
	}
	broadcastData(data[:n])
	log.Printf("Received WebSocket frame: %d bytes", n)
	count++
	sum += n
	if _, err := f.Write(data[:n]); err != nil {
		log.Printf("f.Write failed: %v", err)
	}
}

endBroadcast()

duration := time.Since(startTime)

log.Printf("Closed WebSocket, received %d frames (%d bytes), took %s (%.3f kb/s)", count, sum, duration, (float64(sum) / duration.Seconds()) / float64(1024))

}

func openrecfile(w http.ResponseWriter, r *http.Request){

log.Printf("Opened WebSocket %s",r.FormValue("fname"))
ch := make(chan []byte)
registerClient(ch)
defer unregisterClient(ch)

//f := w.(http.Flusher)
//connClosed := w.(http.CloseNotifier).CloseNotify()
w.Header().Set("Content-Type", "audio/mpeg")
w.WriteHeader(http.StatusOK)

}

func streamLiveData(w http.ResponseWriter, r *http.Request) {
ch := make(chan []byte)
registerClient(ch)
defer unregisterClient(ch)

f := w.(http.Flusher)
connClosed := w.(http.CloseNotifier).CloseNotify()
w.Header().Set("Content-Type", "audio/mpeg")
w.WriteHeader(http.StatusOK)

for {
	select {
	case data, ok := <-ch:
		if !ok {
			log.Printf("End of transmission.")
			return
		}
		if _, err := w.Write(data); err != nil {
			log.Printf("Writing data to client failed: %v", err)
			return
		}
		f.Flush()
	case <-connClosed:
		log.Printf("Connection closed, stopping transmission.")
		return
	}
}

}

var clients = make(map[chan []byte]struct{})

func registerClient(ch chan []byte) {
clients[ch] = struct{}{}
log.Printf(“Registered client %p”, ch)
}

func unregisterClient(ch chan []byte) {
delete(clients, ch)
log.Printf(“Unregistered client %p”, ch)
}

func broadcastData(data []byte) {
//log.Printf(“broadcastData data::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::”)
for ch, _ := range clients {

	select {
	case ch <- data:
	}
}

}

func endBroadcast() {
for ch, _ := range clients {
close(ch)
}
}

Thanks

Can you clarify this part: “the server is not coming up and not showing any error informaiton”. Do you see any of your log.Println calls at all? For example the “Server Starting… step-1”?

Can you provide some more environment details? Go version and OS/arch?

The call to http.ListenAndServe should be the last call in your main as it blocks. Anything after it, including any “server started” messages, won’t be executed.

3 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.