This may seem a childish problem. I am trying to establish the connection with a TCP server from 1 function and trying to send some value in another function from a client. Can I able to do that? For example, I have a client looks like following.
var conn net.Conn
func main() {
conn, err := net.Dial("tcp", "127.0.0.1:8081" )
if err != nil {
fmt.Println("Error accepting: ", err.Error())
}
}
Another function(){
send_some_value_to_server(conn)
}
My server code looks like this. It can handle multiple clients.
func (s *server) recvAndEcho(c net.Conn) {
//do_something
}
type server struct {
clients []io.Writer
}
func (s *server) addClient(c net.Conn) {
s.clients = append(s.clients, c)
}
func (s *server) broadcastMsg(msg string, u_id string) {
//do_something
}
func main() {
fmt.Println("Launching server...")
srv := &server{clients: make([]io.Writer, 0)}
// listen on all interfaces
ln, _ := net.Listen("tcp", ":8081")
for {
// Listen for an incoming connection.
conn, err := ln.Accept()
if err != nil {
fmt.Println("Error accepting: ", err.Error())
continue
}
srv.addClient(conn)
go srv.recvAndEcho(conn)
}
}
I am not sure I am able to do that. Because when I try to run that I am getting run time error. Though if I try to establish the connection and send data from the same function of client I am able to. But I am trying to do that from the different function for some purpose. The error I am founding is
panic: runtime error: invalid memory address or nil pointer dereference [signal 0xc0000005 code=0x0 addr=0x18 pc=0x46e8a2]
User contributions licensed under CC BY-SA 3.0