-
Notifications
You must be signed in to change notification settings - Fork 0
/
sever.go
101 lines (84 loc) · 1.88 KB
/
sever.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
***********************************************************
// Describe : TCP socket 通信 服务器端
// date : 2016.12.28
// Author :
*********************************************************
*/
package main
import (
"fmt"
"go-file/Go_Chat/mysql"
"log"
"net"
)
var conns [5]net.Conn
func main() {
//启动服务器&连接数据库
StartSever()
}
func StartSever() {
fmt.Println("Starting the server ...")
//创建listener 用来监听和接收客户端的请求
listener, err := net.Listen("tcp", "localhost:50001")
if err != nil {
fmt.Println("Error listening", err.Error())
return
}
//连接数据库
mysql.OpenMysql()
//监听并接受来自客户端的连接
for {
//客户端请求时会产生net.Conn类型的连接变量
conn, err := listener.Accept()
if err != nil {
fmt.Println("Error accepting", err.Error())
return
}
//保存连接信息
for i := 0; i < 5; i++ {
if conns[i] == nil {
conns[i] = conn
break
} else {
//给在线的人提示有人上线了
onLineNotice := "Someone online!"
SendAndSaveMsg(conns[i], onLineNotice)
}
}
go handleConnection(conn)
}
}
func handleConnection(conn net.Conn) {
var sendConn net.Conn
for {
//将收到的消息放进buf
buf := make([]byte, 2048)
len, err := conn.Read((buf))
if err != nil {
fmt.Println("error Reading", err.Error())
return
}
fmt.Printf("%v\n", string(buf[:len]))
//将收到的消息转发给另一客户端
for i := 0; i < 5; i++ {
if conns[i] != nil {
sendConn = conns[i]
//发送和保存
SendAndSaveMsg(sendConn, string(buf[:len]))
continue
}
}
}
}
func SendAndSaveMsg(conn net.Conn, sendData string) {
//LogInfo(conn.RemoteAddr().String())
_, err := conn.Write([]byte(sendData))
if err != nil {
fmt.Println("Send Filed", err.Error())
return
}
}
func LogInfo(loginfor string) {
log.Println(loginfor)
}