Relationship GO network programming and Linux Socket API's

Go to show how a simple program, and then analyze the relationship GO API and the Linux API. Like the concept of a simple socket, etc. are not documented, and do not know what to Baidu.

server.go 

package main

import "net"
import "fmt"
import "bufio"
import "strings" // only needed below for sample processing

func main() {

  fmt.Println("Launching server...")
  // listening port 8081
  ln, _ := net.Listen("tcp", ":8081")
  // access
  conn, _ := ln.Accept()
  for {
    message, _ := bufio.NewReader(conn).ReadString('\n')
    fmt.Print("Message Received:", string(message))
    // processing at the data received
    newmessage := strings.ToUpper(message)
    // write back to the client
    conn.Write([]byte(newmessage + "\n"))
  }
}

client.c

package main

import "net"
import "fmt"
import "bufio"
import "os"

func main() {

  // connect to the server
  conn, _ := net.Dial("tcp", "127.0.0.1:8081")
  for { 
    // read the data from the standard input stream
    reader := bufio.NewReader(os.Stdin)
    fmt.Print("Text to send: ")
    text, _ := reader.ReadString('\n')
    // send to the socket
    fmt.Fprintf(conn, text + "\n")
    // Listen to write back data server
    message, _ := bufio.NewReader(conn).ReadString('\n')
    if message == ""{
        fmt.Print("message from client is nil")
        break
    }
    fmt.Print("Message from server: "+message)
  }
}

Use go language communication between a simple client-server. The client is left, the right is the server.

The link between Linux Socket API and go API

go language to write network program is very simple, and C language compared to omit a number of steps, from C language to create a socket - related information filled>, and then use the function bind () bind a socket ---> then listen listening port ---> Finally, use accept receive connections. go language preceding two steps are omitted, the Listen directly () listening port and bound port, and then use the Accept () receives the connection, then go to Listen, Accept function of the C language and bind, listen, accept what links it.

Listen

Do not say so much nonsense, show the picture!

                        Diagram 1 go Listen and Linux Socket API function of

图片凑活的看,可以看到其实go语言的Listen函数底层是直接调用了Linux网络编程API的socket()创建一个套接字,并且将数据绑定到这个套接字上,最后在这个端口上监听,可谓是一举完成了三件事情,真的是非常的方便。

在阅读代码和查看资料的时候,发现go是将socket使用epoll来进行异步通知的。说到这里,其实逻辑很清晰,go语言通过一步到位的方式使用Listen将创建套接字、绑定、监听都完成了,最后还顺便将socket加入到epoll的监听队列中,从而完成了异步网络编程。

Accept

                                           

                              图2 go Accept函数和Linux Socket API的关系

相比较我们上面讨论的Listen来说,Accept就简单很多了。在连接建立后,还是会将这个得到socket加入到咱们的epoll队列中,从而有事件到来的时候,异步的通知我们处理此socket的线程。

 总结

  其实总的来说,不管是JAVA、PHP、还是我们讨论的GO语言,调用来调用去,封装来封装去,到最后还是调用操作系统提供的系统调用来完成我们的工作,而系统调用又需要调用网络驱动程序来完成对网卡或者其他硬件的读和写。套用一句很有名气的话,"计算机世界的所有问题,都可以通过向上一层进行抽象封装来解决"。如果有,那就两层解决。回想我们编程,也正是如此。我们一开始使用面向过程的编程思想,写函数,然后出现了高阶函数,又出现了面向对象的思想,这何尝不是一种我们应用程序员进行的封装呢?

 

 

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/luhaipeng/p/11997639.html