//先前代码对中文处理有问题,以下代码做了修正,
//go语言服务器代码
package main
import(
"fmt"
"net"
"strconv"
"runtime" //go线程库
"strings"
)
var conns map[string]net.Conn
//
//错误检查
//
func checkError(err error,info string) (res bool) {
if(err != nil){
fmt.Println(info+" " + err.Error())
//checkOnlineUser()
return false
}
return true
}
//
//服务器端接收数据线程
//参数:
// 数据连接 conn
// 通讯通道 messages
//
func Handler(conn net.Conn,messages chan string){
//fmt.Println("connection is connected from ...",conn.RemoteAddr().String())
buf := make([]byte,1024)
for{
lenght, err := conn.Read(buf)
if(checkError(err,"Connection")==false){
delete(conns,conn.RemoteAddr().String())
conn.Close()
checkOnlineUser()
break
}
if lenght > 0{
buf[lenght]=0
}
fmt.Println("Rec[",conn.RemoteAddr().String(),"] Say :" ,buf[:lenght])
say := conn.RemoteAddr().String() + " say: ";
s := []byte(say)
name := buf[8:lenght]
id := buf[4:8]
shead := buf[0:4]
sh := string(shead[:])
sh = strings.Replace(sh, " ", "", -1)
i2,_ := strconv.Atoi(sh)
i2 = i2 + len(say)
slen := strconv.Itoa(i2)
sl := fmt.Sprintf("%4s",slen)
head := []byte(sl)
content := make([]byte,2048)
for i := 0; i < len(head); i++{
content[i] = head[i]
}
j := 0
for i:= len(head); i < len(id) + len(head) ; i++{
content[i] = id[j]
j++
}
j=0
for i:=len(head)+len(id); i < len(s) + len(head) + len(id) ; i++{
content[i] = s[j]
j++
}
j = 0
for i:=len(head)+len(id)+len(s); i < len(s) + len(head) + len(id) + len(name); i++{
content[i] = name[j]
j++
}
sum := len(s) + len(head) + len(id) + len(name)
reciveStr := string(content[:sum])
messages <- reciveStr
}
}
//
//
//检测在线用户
//
//
func checkOnlineUser(){
var msg string
var slen string
//for{
for key,_ := range conns {
//fmt.Println("connection is connected from ...",key)
msg += key;
msg += "-";
}
l1:=len([]rune(msg))
if l1==0{
return
}
msg = string(msg[0:l1-1])
slen = strconv.Itoa(l1-1)
//fmt.Println(slen)
sl := fmt.Sprintf("%4s",slen)
// fmt.Println(sl)
stype := "1000"
msg = sl + stype + msg
for _,value := range conns {
//fmt.Println("发送内容",msg)
runtime.Gosched();
_,err :=value.Write([]byte(msg))
if(err != nil){
fmt.Println(err.Error(),"checkOnlineUser")
//delete(conns,key)
}else{
fmt.Println("发送用户列表了")
}
}
// }
}
//
//服务器发送数据的线程
//
//参数
// 连接字典 conns
// 数据通道 messages
//
func echoHandler(conns *map[string]net.Conn,messages chan string){
for{
msg:= <- messages
for _,value := range *conns {
//fmt.Println("connection is connected from ...",key)
_,err :=value.Write([]byte(msg))
if(err != nil){
fmt.Println(err.Error(),"123")
//delete(*conns,key)
}
}
}
}
//
//启动服务器
//参数
// 端口 port
//
func StartServer(port string){
service:=":"+port //strconv.Itoa(port);
tcpAddr, err := net.ResolveTCPAddr("tcp4", service)
checkError(err,"ResolveTCPAddr")
l,err := net.ListenTCP("tcp",tcpAddr)
checkError(err,"ListenTCP")
//conns=make(map[string]net.Conn)
messages := make(chan string)
//imsgstart = 1
//启动服务器广播线程
go echoHandler(&conns,messages)
for {
fmt.Println("msg Listening ...")
conn,err := l.Accept()
checkError(err,"Accept")
fmt.Println("Accepting ...")
conns[conn.RemoteAddr().String()]=conn
//fmt.Println(conn.RemoteAddr().String())
checkOnlineUser()
//启动一个新线程
go Handler(conn,messages)
}
}
func main(){
conns=make(map[string]net.Conn)
go StartServer("7777");
fmt.Println("服务启动完成")
for{
}
}
// qunliaoDlg.cpp : 实现文件
//
#include "stdafx.h"
#include "qunliao.h"
#include "qunliaoDlg.h"
#include <atlbase.h>
#include <boost/format.hpp>
#include <boost/tokenizer.hpp>
#include <boost/algorithm/string.hpp>
#include < boost/locale/encoding.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#define BOOST_DATE_TIME_SOURCE
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// 用于应用程序“关于”菜单项的 CAboutDlg 对话框
c