FAQ
Hi,

I am trying to demonstrate client server communication. My server is not
reading from client. Below is the code :


/*
  *
  * server
  */

  package main

  import (
     "fmt"
     "flag"
     "net"
  )

  func main() {
     flag.Parse()
     if flag.NArg()!=2 {
         fmt.Println("Please give two command line arguments : host and
port")
     }
     hostport:=fmt.Sprintf("%s:%s",flag.Arg(0),flag.Arg(1))
     servadrr,err:=net.ResolveTCPAddr("tcp",hostport)
     if err!=nil {
         fmt.Println("error parsing hostport")
     }
     listener,err:=net.ListenTCP("tcp",servadrr)
     if err!=nil {
         fmt.Println("error listening on the given TCPAddr")
     }

     for{
         conn,err:=listener.Accept()
         if err!=nil {
             fmt.Println("not able to accept connections")
         }
         go processRequest(conn)
     }
  }

  func processRequest(conn net.Conn) {
     connFrom:=conn.RemoteAddr().String()
     fmt.Println("Connection from :",connFrom)
     str:="Say something to me"
     _,err:=conn.Write([]byte(str))
     if err!=nil {
        fmt.Println("error in writing to client")
     }
     buf:=make([]byte,4096)
     _,err1:=conn.Read(buf)
     if err1!=nil {
         fmt.Println("error in reading from client")
     }
     fmt.Println("client says :",string(buf))
  }



//client from the book

package main

import (
     "fmt"
     "net"
     "os"
     "bufio"
     "strings"
)

func main() {
     conn,err:=net.Dial("tcp","localhost:50000")
     if err!=nil {
         fmt.Println("error in connecting to server")
         return
     }

     inputReader:=bufio.NewReader(os.Stdin)
     //fmt.Println("Say name to server")
     clientName,_:=inputReader.ReadString('\n')
     trimmedClient := strings.Trim(clientName, "\r\n")


     for {
         fmt.Println("Sending data to server,type Q to quit")
         input,_:=inputReader.ReadString('\n')
         trimmedInput := strings.Trim(input, "\r\n")

         if trimmedInput=="Q" {
             fmt.Println("No more to write")
             return
         }
         _,err:=conn.Write([]byte(trimmedClient+" says :"+trimmedInput))
         if err!=nil {
             fmt.Println("error writing to server")
             return
         }
     }
}


Please let me know, what am I missing?

Thanks & Regards

Abhinav



--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedDec 25, '13 at 10:46p
activeDec 25, '13 at 10:46p
posts1
users1
websitegolang.org

1 user in discussion

Abhinav Srivastava: 1 post

People

Translate

site design / logo © 2021 Grokbase