A new Bee here..Question related with client server communication using TCP connection

Hi All,

I am new to GoLang and trying to learn the language.Below is the explanation of the program which i am trying to write with the code(server and client).This code does not work.I am trying to figure out where am i wrong.

I am trying to create a server that can handle incoming text and print it and in parallel it should be able to send the data which client which is sending as well receiving the incoming data from the server should read it and print.

but when i run my both server and client code.The run but it not able to send and receive any thing.Can you please have a look at my code and point out where am i doing mistake.

Server code is

package main

import (
“net”
“fmt”
“time”
“bufio”
“sync”
)

var wg sync.WaitGroup

func main(){

wg.Add(2)

li,err := net.Listen(“tcp”,":9999")

if err != nil{
panic(err)
}
defer li.Close()

for {
fmt.Println(“Requesting for connection…”)
conn,error := li.Accept()
fmt.Println("Connection request received from ",conn.RemoteAddr())

  if error != nil{
     panic(error)
  }

  fmt.Println("The request for Input")
  handleInputClient(conn)
  fmt.Println("The request for Output")
  handleOutputClient(conn)
  wg.Wait()

  fmt.Println("The wait group is exiting and main program is closing...")

}
}

func handleOutputClient(conn net.Conn){
go func() {
for {
fmt.Println(“This is for the trial…”)
fmt.Fprint(conn,“This is for the trial…”)
time.Sleep(time.Second * 3)
}
wg.Done()
}()
}

func handleInputClient(conn net.Conn) {
go func() {
fmt.Println(“Creating scanner…”)
scanner := bufio.NewScanner(conn)
for scanner.Scan(){
str := scanner.Text()
fmt.Println("Scanning data ",str)
fmt.Println(str)
}
wg.Done()
}()

}

========================================================================

Client code is

package main

import (
“net”
“fmt”
“time”
“sync”
)

var wg sync.WaitGroup

func main(){

wg.Add(1)
conn,err := net.Dial("tcp",":9999")
fmt.Println("The connection is established")

if err != nil{
	panic(err)
}

defer conn.Close()

go func() {
	fmt.Println("Connection establisted with server ",conn.RemoteAddr())

    //connection is established..so first thing client does is introduce itself.
    fmt.Fprint(conn, "The connection is established")
	fmt.Println("The connection is established")

	for  {
		fmt.Fprint(conn,"Lets keep on talking....")
		fmt.Println("Lets keep on talking....")
		time.Sleep(time.Second * 3)
	}
	wg.Done()

}()


wg.Wait()

}

You was almost there :wink: just two small changes:

  1. use fmt.Fprintln instead of fmt.Fprint so a newline gets outputed or Scanner.Scan will not return
  2. the client must have a go-routine reading data also: I just copied the code from your server.

Good work !

server.go

package main

import (
	"bufio"
	"fmt"
	"net"
	"sync"
	"time"
)

var wg sync.WaitGroup

func main() {

	wg.Add(2)

	li, err := net.Listen("tcp", ":9999")

	if err != nil {
		panic(err)
	}
	defer li.Close()

	for {
		fmt.Println("Requesting for connection…")
		conn, error := li.Accept()
		fmt.Println("Connection request received from ", conn.RemoteAddr())

		if error != nil {
			panic(error)
		}

		fmt.Println("The request for Input")
		handleInputClient(conn)
		fmt.Println("The request for Output")
		handleOutputClient(conn)
		wg.Wait()

		fmt.Println("The wait group is exiting and main program is closing...")
	}
}

func handleOutputClient(conn net.Conn) {
	go func() {
		for {
			fmt.Println("This is for the trial…")
			// Use Fprintln so line ends with newline (otherwise will Scanner not work)
			fmt.Fprintln(conn, "This is for the trial…")
			time.Sleep(time.Second * 3)
		}
		wg.Done()
	}()
}

func handleInputClient(conn net.Conn) {
	go func() {
		fmt.Println("Creating scanner…")
		scanner := bufio.NewScanner(conn)
		for scanner.Scan() {
			str := scanner.Text()
			fmt.Println("Scanning data ", str)
			fmt.Println(str)
		}
		wg.Done()
	}()

}

client.go

package main

import (
	"bufio"
	"fmt"
	"net"
	"sync"
	"time"
)

var wg sync.WaitGroup

func main() {

	wg.Add(2)
	conn, err := net.Dial("tcp", ":9999")
	fmt.Println("The connection is established")

	if err != nil {
		panic(err)
	}

	defer conn.Close()

	go func() {
		fmt.Println("Connection establisted with server ", conn.RemoteAddr())

		//connection is established..so first thing client does is introduce itself.
		fmt.Fprint(conn, "The connection is established")
		fmt.Println("The connection is established")

		for {
			// Use Fprintln so line ends with newline (otherwise will Scanner not work)
			fmt.Fprintln(conn, "Lets keep on talking....")
			fmt.Println("Lets keep on talking....")
			time.Sleep(time.Second * 3)
		}
		wg.Done()

	}()

	go func() {
		fmt.Println("Creating scanner…")
		scanner := bufio.NewScanner(conn)
		for scanner.Scan() {
			str := scanner.Text()
			fmt.Println("Scanning data ", str)
			fmt.Println(str)
		}
		wg.Done()
	}()

	wg.Wait()
}
2 Likes

Nice, thanks for this you helped me with a problem inside my server code

1 Like