Golang在Linux环境下的POSIX风格socket编程

前端之家收集整理的这篇文章主要介绍了Golang在Linux环境下的POSIX风格socket编程前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

这里给出一个服务端和客户端,服务端可以接受多个连接,并且利用Go的杀手特性go和channel来替代select进行数据的接收。


服务端:

package main

import (
	"fmt"
	. "syscall"
)

func RecvRoutine(sockfd int,session chan string) {
	var buffer []byte = make([]byte,3000)
	for {
		if length,err := Read(sockfd,buffer); err == nil {
			session <- string(buffer[:length])
		}
	}
}

func main() {
	var (
		serversock int
		serveraddr SockaddrInet4
		session    chan string = make(chan string,1000)
		err        error
	)

	if serversock,err = Socket(AF_INET,SOCK_STREAM,IPPROTO_IP); err != nil {
		fmt.Println("Server Socket() called error:",err.Error())
		return
	}
	defer Shutdown(serversock,SHUT_RDWR)

	serveraddr.Addr = [4]byte{127,1}
	serveraddr.Port = 3000

	if err = Bind(serversock,&serveraddr); err != nil {
		fmt.Println("Server Bind() called error:",err.Error())
		return
	}

	if err = Listen(serversock,SOMAXCONN); err != nil {
		fmt.Println("Server Listen() called error:",err.Error())
		return
	}

	go func() {
		for {
			fmt.Println(<-session)
		}
	}()

	fmt.Println("server is listening at port 3000...")
	for {
		if clientsock,_,err := Accept(serversock); err == nil {
			go RecvRoutine(clientsock,session)
		}
	}
}


客户端:

package main

import (
	"fmt"
	. "syscall"
)

func main() {
	var (
		clientsock int
		serveraddr SockaddrInet4
		err        error
	)

	if clientsock,IPPROTO_IP); err != nil {
		fmt.Println("Client Socket() called error:",err.Error())
		return
	}
	defer Shutdown(clientsock,1}
	serveraddr.Port = 3000

	if err = Connect(clientsock,&serveraddr); err != nil {
		fmt.Println("Client Connect() called error:",err.Error())
		return
	}

	var msg string
	for {
		fmt.Scanf("%s\r\n",&msg)
		if msg != "\r" && msg != "\n" {
			if _,err = Write(clientsock,[]byte(msg)); err != nil {
				fmt.Println("Send() error:",err.Error())
			}
		}
	}
}



如果转载请注明出处:http://blog.csdn.net/gophers

原文链接:https://www.f2er.com/go/190889.html

猜你在找的Go相关文章