IM-System版本0.9实现基本客户端应用

This commit is contained in:
wandoubaba 2024-11-25 12:19:23 +08:00
parent d3a8b24121
commit 93ce7310ba
2 changed files with 46 additions and 0 deletions

View File

@ -1,2 +1,3 @@
/server /server
/client/client
!.gitignore !.gitignore

View File

@ -0,0 +1,45 @@
// 客户端应用
package main
import (
"fmt"
"net"
"time"
)
type Client struct {
ServerIp string
ServerPort int
Name string
conn net.Conn
}
func NewClient(serverIp string, serverPort int) *Client {
// 创建Client对象
client := &Client{
ServerIp: serverIp,
ServerPort: serverPort,
}
// 连接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
// 返回Client对象
return client
}
func main() {
client := NewClient("127.0.0.1", 8888)
if client == nil {
fmt.Println(">>>>>> 连接服务器失败!")
return
}
fmt.Println(">>>>>> 连接服务器成功……")
for {
time.Sleep(1 * time.Second)
}
}