Chinaunix首页 | 论坛 | 博客
  • 博客访问: 355893
  • 博文数量: 43
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 585
  • 用 户 组: 普通用户
  • 注册时间: 2013-03-29 20:57
文章分类

全部博文(43)

文章存档

2013年(43)

我的朋友

分类: Windows平台

2013-06-18 11:29:46


鉴于Go语言有着强大的多线程能力,所以就写个最简单的socket例子供大家参考。

server.go

点击(此处)折叠或打开

  1. package main

  2. import (
  3.     "fmt"
  4.     "net"
  5.     "os"
  6. )

  7. func main() {
  8.     var (
  9.         host = "127.0.0.1"
  10.         port = "8080"
  11.         remote = host + ":" + port
  12.         data = make([]byte, 1024)
  13.     )
  14.     fmt.Println("Initiating server... (Ctrl-C to stop)")

  15.     lis, err := net.Listen("tcp", remote)
  16.     defer lis.Close()

  17.     if err != nil {
  18.         fmt.Println("Error when listen: ", remote)
  19.         os.Exit(-1)
  20.     }

  21.     for {
  22.         var res string
  23.         conn, err := lis.Accept()
  24.         if err != nil {
  25.             fmt.Println("Error accepting client: ", err.Error())
  26.             os.Exit(0)
  27.         }

  28.         go func(con net.Conn) {
  29.             fmt.Println("New connection: ", con.RemoteAddr())
  30.             for {
  31.                 length, err := con.Read(data)
  32.                 if err != nil {
  33.                     fmt.Printf("Client %v quit.\n", con.RemoteAddr())
  34.                     con.Close()
  35.                     return
  36.                 }
  37.                 res = string(data[0:length])
  38.                 fmt.Printf("%s said: %s\n", con.RemoteAddr(), res)
  39.                 res = "You said:" + res
  40.                 con.Write([]byte(res))
  41.             }
  42.         }(conn)
  43.     }
  44. }


client.go

点击(此处)折叠或打开

  1. package main

  2. import (
  3.     "fmt"
  4.     "net"
  5.     "os"
  6. )

  7. var str string
  8. var msg = make([]byte, 1024)

  9. func main() {
  10.     var (
  11.         host = "127.0.0.1"
  12.         port = "8080"
  13.         remote = host + ":" + port
  14.     )

  15.     con, err := net.Dial("tcp", remote)
  16.     defer con.Close()

  17.     if err != nil {
  18.         fmt.Println("Server not found.")
  19.         os.Exit(-1)
  20.     }
  21.     fmt.Println("Connection OK.")

  22.     for {
  23.         fmt.Printf("Enter a sentence:")
  24.         fmt.Scanf("%s\n", &str)
  25.         if str == "quit" {
  26.             fmt.Println("Communication terminated.")
  27.             os.Exit(1)
  28.         }

  29.         in, err := con.Write([]byte(str))
  30.         if err != nil {
  31.             fmt.Printf("Error when send to server: %d\n", in)
  32.             os.Exit(0)
  33.         }

  34.         length, err := con.Read(msg)
  35.         if err != nil {
  36.             fmt.Printf("Error when read from server.\n")
  37.             os.Exit(0)
  38.         }
  39.         str = string(msg[0:length])
  40.         fmt.Println(str)
  41.     }
  42. }

[转载自:http://duyizhuo.com/blog/?p=372]

阅读(6537) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~