Last active
December 28, 2015 22:59
-
-
Save kitmenke/7575512 to your computer and use it in GitHub Desktop.
TCP client and server written in Go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bytes" | |
"flag" | |
"fmt" | |
"math/rand" | |
"os" | |
"time" | |
"net" | |
"io" | |
) | |
func main() { | |
var address = flag.String("address", "localhost:6666", "The server address to send data to via TCP.") | |
var clients = flag.Int("clients", 1, "The number of clients to simulate.") | |
//if len(os.Args) < 2 { | |
// fmt.Printf("usage: %s [-address] [-clients]\n", os.Args[0]) | |
// flag.PrintDefaults() | |
// os.Exit(0) | |
//} | |
flag.Parse() | |
// seed the random number generator | |
rand.Seed(time.Now().UTC().UnixNano()) | |
// start up the number of clients they requested to send data | |
for i := 1; i <= *clients; i++ { | |
fmt.Printf("Starting client #%d\n", i) | |
go handleClient(*address, i) | |
time.Sleep(time.Second) | |
} | |
os.Exit(0) | |
} | |
func handleClient(address string, id int) { | |
fmt.Printf("Client %d connecting to server (%s)...\n", id, address) | |
tcpAddr, err := net.ResolveTCPAddr("tcp", address) | |
if err != nil { | |
println("ResolveTCPAddr failed:", err.Error()) | |
return | |
} | |
// connect to the server | |
conn, err := net.DialTCP("tcp", nil, tcpAddr) | |
if err != nil { | |
println("DialTCP failed:", err.Error()) | |
return | |
} | |
defer conn.Close() | |
for i := 0; i < 10; i++ { | |
var message string | |
// write the message to the server | |
// create a message | |
message = fmt.Sprintf("Sent from client #%d, number is %d\n", id, i) | |
_, err = io.Copy(conn, bytes.NewBufferString(message)) | |
if err != nil { | |
println("Copy failed:", err.Error()) | |
return | |
} | |
fmt.Printf("%d --> %s", id, message) | |
// commenting out this sleep line makes it work normally | |
//time.Sleep(3*time.Second) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"net" | |
"os" | |
"io" | |
"bufio" | |
) | |
const ( | |
RECV_BUF_LEN = 1024 | |
) | |
func main() { | |
println("Starting the server") | |
listener, err := net.Listen("tcp", "0.0.0.0:6666") | |
if err != nil { | |
println("error listening:", err.Error()) | |
os.Exit(1) | |
} | |
for { | |
conn, err := listener.Accept() | |
if err != nil { | |
println("Error accept:", err.Error()) | |
continue | |
} | |
go handleConnection(conn) | |
} | |
} | |
func handleConnection(conn net.Conn) { | |
println("New connection received!") | |
for { | |
//buf := make([]byte, RECV_BUF_LEN) | |
line, err := bufio.NewReader(conn).ReadBytes('\n') | |
//n, err := conn.Read(buf) | |
if err == io.EOF { | |
println("Client connection closed.") | |
return | |
} | |
if err != nil { | |
println("Error reading:", err.Error()) | |
return | |
} | |
println("--> ", string(line)) | |
//println("Received ", n, " bytes: ", string(buf)) | |
// send reply | |
/*_, err = conn.Write(buf) | |
if err != nil { | |
println("Error send reply:", err.Error()) | |
} else { | |
println("Reply sent") | |
}*/ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment