关闭

go server

162人阅读 评论(0) 收藏 举报

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)
 }
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:2387次
    • 积分:131
    • 等级:
    • 排名:千里之外
    • 原创:10篇
    • 转载:3篇
    • 译文:1篇
    • 评论:0条
    文章存档