Browse Source

Added a socket client, just for test.

tags/0.0.1
mikespook 13 years ago
parent
commit
d7a1ea0e6b
2 changed files with 91 additions and 0 deletions
  1. +17
    -0
      test/Makefile
  2. +74
    -0
      test/client.go

+ 17
- 0
test/Makefile View File

@@ -0,0 +1,17 @@
# Copyright 2009 The Go Authors. All rights reserved.
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.

include $(GOROOT)/src/Make.inc

TARG=client
GOFILES=\
client.go\

CLEANFILES+=client

include $(GOROOT)/src/Make.pkg

%: install %.go
$(GC) $*.go
$(LD) -o $@ $*.$O

+ 74
- 0
test/client.go View File

@@ -0,0 +1,74 @@
package main

import (
"net"
"log"
"fmt"
)

type Client struct {
c * net.TCPConn
w chan []byte
r chan []byte
}

func NewClient() * Client {
client := new(Client)
wch := make(chan []byte, 2048)
rch := make(chan []byte, 2048)
client.w = wch
client.r = rch
addr, err := net.ResolveTCPAddr("127.0.0.1:4730")
if err != nil {
log.Panic(err.String())
}
c, err := net.DialTCP("tcp", nil, addr)
client.c = c
if err != nil {
log.Panic(err.String())
}
go client.ReadLoop()
go client.WriteLoop()
return client
}

func (client * Client) ReadLoop() {
for {
buf := make([]byte, 2048)
_, err := client.c.Read(buf)
if err != nil {
client.c.Close()
break;
}
client.r <- buf
}
}

func (client * Client) WriteLoop() {
for {
select {
case buf := <- client.w:
client.c.Write(buf)
}
}
}

func (client * Client) Write(data []byte) {
client.w <- data
}

func (client * Client) Read() []byte{
var buf []byte
select {
case buf = <- client.r:
}
return buf
}

func main() {
client := NewClient()
client.Write([]byte("TEST\r\n"))
for {
fmt.Println(client.Read())
}
}

Loading…
Cancel
Save