go server

zhangjinru123 · · 1280 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

package main

import (
 "database/sql"
 "fmt"
 _ "github.com/go-sql-driver/mysql"
 "net"
 "strings"
)

var maps map[string]net.Conn = make(map[string]net.Conn)

func find(findId string) string {
 db, err := sql.Open("mysql", "root:123@/liaotian?charset=utf8")
 check(err)

 rows, err := db.Query("SELECT * FROM user")
 check(err)

 defer rows.Close()
 for rows.Next() {

  var id int
  var name string
  var pwd string

  err := rows.Scan(&id, &name, &pwd)
  check(err)

  str := fmt.Sprintf("%d", id)

  if strings.EqualFold(str, findId) {
   return "success"
  }
 }
 return "fail"
}

func login(userId, upwd string) string {
 db, err := sql.Open("mysql", "root:123@/liaotian?charset=utf8")
 check(err)

 rows, err := db.Query("SELECT * FROM user")
 check(err)

 defer rows.Close()

 for rows.Next() {

  var id int
  var name string
  var pwd string

  err := rows.Scan(&id, &name, &pwd)
  check(err)
       //将int转换成string
  str := fmt.Sprintf("%d", id)

  if strings.EqualFold(str, userId) && strings.EqualFold(pwd, upwd) {
   return "success"
  }
 }
 return "fail"
}

func regist(name, pwd string) int64 {
 db, err := sql.Open("mysql", "root:123@/liaotian?charset=utf8")
 check(err)

 stmt, err := db.Prepare(`INSERT user (username,password) values (?,?)`)
 check(err)

 defer stmt.Close()
 res, err := stmt.Exec(name, pwd)

 if err != nil {
  fmt.Println(err)
  return 0
 }

 cuid, err := res.LastInsertId()
 return cuid
}

func check(err error) {
 if err != nil {
  fmt.Println(err)
  return
 }
}

func writetext(conn net.Conn, con string) {

 j := "3-" + con + "\n"
 a := []byte(j)
 conn.Write(a)
}

func writefind(conn net.Conn, re, findId string) {

 j := "44-" + re + "-" + findId + "\n"
 a := []byte(j)
 conn.Write(a)
}

func writelogin(conn net.Conn, re, userId, pwd string) {

 j := "2-" + re + "-" + userId + "-" + pwd + "\n"
 a := []byte(j)
 conn.Write(a)
}

func writeregist(conn net.Conn, userId int64) {

 str := fmt.Sprintf("%d", userId)
 j := "1-" + str + "\n"
 a := []byte(j)
 conn.Write(a)
}

func read(conn net.Conn) string {

 var str string
 name := make([]byte, 512)
 size, err := conn.Read(name)
 str = string(name)
 fmt.Println(size)
 check(err)
 return str
}

func doServerStuff(conn net.Conn) {
 
 remote := conn.RemoteAddr().String()
 fmt.Println(remote, " connected!")

 for {

  var s []string

  var types string = read(conn)
  s = strings.Split(types, "-&*%$")

  var ty = s[0]

  switch {
  case strings.EqualFold(ty, "regist"):

   var name = s[1]
   var pwd = s[2]
   var userId int64 = regist(name, pwd)

   writeregist(conn, userId)

  case strings.EqualFold(ty, "login"):

   var id = s[1]
   var pwd = s[2]

   ret := login(id, pwd)
   writelogin(conn, ret, id, pwd)

   if strings.EqualFold(ret, "success") {
    maps[id] = conn
    fmt.Println(id)
   }

  case strings.EqualFold(ty, "find"):

   var finderid = s[1]
   ret := find(finderid)
   writefind(conn, ret, finderid)

  case strings.EqualFold(ty, "text"):

   var con = s[1]
   var finderid = s[2]
   fmt.Println(finderid)
   fconn := maps[finderid]
   writetext(fconn, con)

  case strings.EqualFold(ty, "uuid"):

   var uuid = s[1]
   maps[uuid] = conn

  case strings.EqualFold(ty, "exit"):

   var uuid = s[1]
   var uid = s[2]
   fmt.Println(uid)

   delete(maps, uuid)

   if !(strings.EqualFold(uid, "null")) {
    delete(maps, uid)
   }
   conn.Close()
   return
  default:
   fmt.Println("fali.....")
  }
 }
}

func main() {
 fmt.Println("Starting the server...")
 listener, err := net.Listen("tcp", ":8888")
 check(err)

 for {
  conn, err := listener.Accept()
  check(err)

  go doServerStuff(conn)
 }
}


有疑问加站长微信联系(非本文作者)

本文来自:CSDN博客

感谢作者:zhangjinru123

查看原文:go server

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

1280 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传