Commit 487bcda2 authored by Sushant Mahajan's avatar Sushant Mahajan

initial chat client server working (not using gob)

parent fb0580cf
package main
import (
"bufio"
"fmt"
"net"
"os"
"bufio"
"encoding/gob"
)
func client() {
func main() {
conn, err := net.Dial("tcp", "localhost:5000")
//defer conn.Close()
if err != nil {
fmt.Println("Err:", err)
return
}
reader := bufio.NewReader(os.Stdin)
for {
msg, _ := reader.ReadString('\n')
fmt.Println(msg)
err = gob.NewEncoder(conn).Encode(msg)
msg, err := reader.ReadString('\n')
if err != nil {
fmt.Println("Err:", err)
fmt.Println("Err: ", err)
}
fmt.Println(msg)
conn.Write([]byte(msg))
}
}
func main() {
go client()
var input string
fmt.Scanln(&input)
conn.Close()
}
......@@ -2,8 +2,8 @@ package main
import (
"fmt"
"encoding/gob"
"net"
"bytes"
)
func startServer() {
......@@ -26,13 +26,17 @@ func startServer() {
}
func handleClient(conn net.Conn) {
//defer conn.Close()
var msg string
for {
err := gob.NewDecoder(conn).Decode(&msg)
buf := make([]byte, 1024)
_, err := conn.Read(buf)
if err != nil {
//fmt.Println("Err: ", err)
} else {
fmt.Println("Err: ", err)
}
n := bytes.Index(buf, []byte{0})
if n != 0 {
msg := string(buf[:n-1])
fmt.Println("Received: ", msg)
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment