Esempio n. 1
0
//发送信息函数
func sendMsg(con net.Conn) {
	for {
		var strv [1024]byte
		C.myscan(unsafe.Pointer(&strv[0]))
		l := 1024
		for {
			if (strv[l-1] < 'a' || strv[l-1] > 'z') && (strv[l-1] < 'A' || strv[l-1] > 'Z') && (strv[l-1] < '0' || strv[l-1] > '9') {
				l = l - 1
			} else {
				break
			}
		}
		str := string(strv[0:l])

		//fmt.Scanf("%s\n", &str) //屏幕输入
		//如果输入quit则结束连接
		if str == "quit" {
			ColorEdit(12)
			fmt.Println("Communication terminated.")
			os.Exit(1)
		}

		in, err := con.Write([]byte(str)) //将输入的内容发送给服务器端
		if err != nil {
			ColorEdit(12)
			fmt.Printf("Error when send to server: %d\n", in)
			os.Exit(0)
		}
	}
	c <- 1
}
Esempio n. 2
0
//发送信息函数,send message function
func sendMsg(con net.Conn) {
	for {
		/*
			声明一个大小为1024的byte型变量strv
			declare a variable(strv) which size is 1024 and belong to byte type
		*/
		var strv [1024]byte
		/*
			调用C语言函数从屏幕输入,将strv首位指针作为参数传递给myscan()
			call gets() function which belongs to C programing language
			aim to get the information of the user input.
			transfer a parameter to myscan(),the parameter is the first pointer of strv
		*/
		C.myscan(unsafe.Pointer(&strv[0]))
		/*
			声明一个整数变量L,用于将要取出的strv的长度
			declare a variable(L) aim to get the length of strv
		*/
		l := 1024

		/*
			从strv的末尾开始判断ASCII值,如果为0,则长度L减1
			judge ASCII value from the end of strv,if it is 0,the length decrease 1
		*/
		for {
			if strv[l-1] == 0 {
				l = l - 1
			} else {
				break
			}
		}
		/*
			类型转换,取strv的首位到L的长度转换为string
			type conversion,get a length from begining to L aim to convert to the type of string
		*/
		str = string(strv[:l])

		if str == "quit" { //如果输入quit则结束连接
			ColorEdit(12) //红色,12 represents red color
			fmt.Println("Communication terminated.")
			os.Exit(1)
		}

		in, err := con.Write([]byte(str)) //将输入的内容发送给服务器端,send the input message to server
		if err != nil {
			ColorEdit(12) //红色,12 represents red color
			fmt.Printf("Error when send to server: %d\n", in)
			os.Exit(0)
		}
	}
	c <- 1
}
Esempio n. 3
0
func main() {

	readFile(fName) //读取配置文件,read the configuration file
	/*
		提示即将连接服务器
		Tip "the server will be connect"
	*/
	fmt.Println("You will connect the server : ", remote)

	ColorEdit(12) //红色,12 represents red color
	for {
		//输入昵称,enter nickname

		fmt.Printf("Enter a nicename:")
		/*
			声明一个大小为1024的byte型变量strv
			declare a variable(strv) which size is 1024 and belong to byte type
		*/
		var strv [1024]byte
		/*
			调用C语言gets函数获取用户输入的信息,将strv首位指针作为参数传递给myscan()
			call gets() function which belongs to C programing language
			aim to get the information of the user input.
			transfer a parameter to myscan(),the parameter is the first pointer of strv
		*/
		C.myscan(unsafe.Pointer(&strv[0]))
		/*
			声明一个整数变量L,用于将要取出的strv的长度
			declare a variable(L) aim to get the length of strv
		*/
		l := 1024
		/*
			从strv的末尾开始判断ASCII值,如果为0,则长度L减1
			judge ASCII value from the end of strv,if it is 0,the length decrease 1
		*/
		for {
			if strv[l-1] == 0 {
				l = l - 1
			} else {
				break
			}
		}
		/*
			类型转换,取strv的首位到L的长度转换为string
			type conversion,get a length from begining to L aim to convert to the type of string
		*/
		str = string(strv[:l])

		if str == "" {
			fmt.Printf("Please input a right nicename!\n")
			continue
		}
		if str == "query" {
			fmt.Printf("query is a keyword , please input a right nicename!\n")
			continue
		}
		break
	}
	/*
		与远程服务器建立连接,返回连接con
		a connection with the remote server,return connection value con
	*/
	con, err := net.Dial("tcp", remote)
	defer con.Close() //延迟关闭连接,delay to close connection

	if err != nil {
		ColorEdit(12) //红色,12 represents red color
		fmt.Println("Server not found.")
		os.Exit(-1)
	}
	/*
		将昵称发送给服务器端
		send the nickname to server
	*/
	in, err := con.Write([]byte(str))
	if err != nil {
		ColorEdit(12) //红色,12 represents red color
		fmt.Printf("Error when send to server: %d\n", in)
		os.Exit(0)
	}

	ColorEdit(11) //青色,11 represents cyan color
	fmt.Println("Connection OK.")

	length, err := con.Read(msg) //接收欢迎信息,receive welcome message
	if err != nil {
		ColorEdit(12) //红色,12 represents red color
		fmt.Printf("Error when read from server.\n")
		os.Exit(0)
	}
	str = string(msg[0:length]) //格式转换,format conversion
	fmt.Println(str)            //将信息显示到屏幕,display the message on screen

	ColorEdit(10)      //绿色,10 represents green color
	go receiveMsg(con) //接收信息goroutine,receive the message
	go sendMsg(con)    //发送信息goroutine,send the message

	<-c
	<-c

}
Esempio n. 4
0
func main() {

	readFile(fName)

	fmt.Println("You will connect the server : ", remote)

	//输入昵称
	ColorEdit(12)
	for {
		fmt.Printf("Enter a nicename:")

		var strv [1024]byte
		C.myscan(unsafe.Pointer(&strv[0]))
		l := 1024
		for {
			if (strv[l-1] < 'a' || strv[l-1] > 'z') && (strv[l-1] < 'A' || strv[l-1] > 'Z') && (strv[l-1] < '0' || strv[l-1] > '9') {
				l = l - 1
			} else {
				break
			}
		}
		str := string(strv[0:l])
		fmt.Printf(str)
		//fmt.Scanf("%s\n", &str) //屏幕输入
		if str == "" {
			fmt.Printf("Please input a right nicename!\n")
			continue
		}
		if str == "query" {
			fmt.Printf("query is a keyword , please input a right nicename!\n")
			continue
		}
		break
	}

	con, err := net.Dial("tcp", remote) //与远程服务器监理连接,返回连接con
	defer con.Close()                   //延迟关闭连接

	if err != nil {
		ColorEdit(12)
		fmt.Println("Server not found.")
		os.Exit(-1)
	}

	//in, err := con.Write(strv[0:l]) //将昵称发送给服务器端
	in, err := con.Write([]byte(str)) //将昵称发送给服务器端
	if err != nil {
		ColorEdit(12)
		fmt.Printf("Error when send to server: %d\n", in)
		os.Exit(0)
	}

	ColorEdit(11)
	fmt.Println("Connection OK.")

	//接收欢迎信息
	length, err := con.Read(msg)
	if err != nil {
		ColorEdit(12)
		fmt.Printf("Error when read from server.\n")
		os.Exit(0)
	}
	str = string(msg[0:length]) //格式转换
	fmt.Println(str)            //将信息显示到屏幕

	ColorEdit(10)
	go receiveMsg(con) //接收信息goroutine
	go sendMsg(con)    //发送信息,不用goroutine的原因是为了防止main结束

	<-c
	<-c

}