码迷,mamicode.com
首页 > 系统相关 > 详细

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

时间:2014-06-24 17:53:37      阅读:399      评论:0      收藏:0      [点我收藏+]

标签:golang   go语言   socket   linux   网络编程   

这里给出一个服务端和客户端,服务端可以接受多个连接,并且利用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, 0, 0, 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, err = Socket(AF_INET, SOCK_STREAM, IPPROTO_IP); err != nil {
		fmt.Println("Client Socket() called error:", err.Error())
		return
	}
	defer Shutdown(clientsock, SHUT_RDWR)

	serveraddr.Addr = [4]byte{127, 0, 0, 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




Golang在Linux环境下的POSIX风格socket编程,布布扣,bubuko.com

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

标签:golang   go语言   socket   linux   网络编程   

原文地址:http://blog.csdn.net/gophers/article/details/33313959

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!