1 client.go
package main
import (
"flag"
"fmt"
"net"
)
type Client struct {
ServerIp string
ServerPort int
Name string
conn net.Conn
flag int
}
func NewClient(serverIp string, serverPort int) *Client {
// 创建客户端对象
client := &Client{
ServerIp: serverIp,
ServerPort: serverPort,
flag: 999,
}
// 连接server
conn, err := net.Dial("tcp", fmt.Sprintf("%s:%d", serverIp, serverPort))
if err != nil {
fmt.Println("net.Dial err:", err)
return nil
}
client.conn = conn
return client
}
func (this *Client) menu() bool {
var flag int
fmt.Println("1.公聊模式")
fmt.Println("2.私聊模式")
fmt.Println("3.更新用户名")
fmt.Println("0.退出")
fmt.Scanln(&flag) // 读取用户输入
if flag >= 0 && flag <= 3 {
this.flag = flag
return true
} else {
fmt.Println("请输入0,1,2,3")
return false
}
}
func (this *Client) Run() {
for this.flag != 0 {
for !this.menu() {}
switch this.flag {
case 1:
fmt.Println("已选择公聊模式")
break
case 2:
fmt.Println("已选择私聊模式")
break
case 3:
fmt.Println("更新用户名")
break
}
}
}
var serverIp string
var serverPort int
// 此函数会在main函数之前被自动运行, client -i 127.0.0.1 -p 8888
func init() {
flag.StringVar(&serverIp, "i", "127.0.0.1", "设置服务器的IP地址(默认是127.0.0.1)")
flag.IntVar(&serverPort, "p", 8888, "设置服务器的端口号(默认是8888)")
flag.Parse() // 命令行解析
}
func main() {
client := NewClient(serverIp, serverPort)
if client == nil {
fmt.Println("连接服务器失败")
return
}
fmt.Println("连接服务器成功")
client.Run()
}