推荐一下,准备写一段同样功能的代码比较一下体验
更多评论
```
#include "iostream"
#include "boost/asio.hpp"
int main(void) {
boost::asio::io_service ios;
boost::asio::ip::tcp::endpoint endpotion(boost::asio::ip::tcp::v4(), 8080);
boost::asio::ip::tcp::acceptor acceptor(ios, endpotion);
while (true) {
boost::asio::ip::tcp::socket socket(ios);
acceptor.accept(socket);
std::string msg;
socket.read_some(boost::asio::buffer(msg));
std::cout << "client : " << msg.c_str() << std::endl;
socket.write_some(boost::asio::buffer(msg));
}
}
std::cout << "server end." << std::endl;
getchar();
return 0;
}
-------------
package main
import (
"fmt"
"net"
"os"
)
func main() {
netListen, err := net.Listen("tcp", "localhost:8080")
CheckError(err)
defer netListen.Close()
for {
conn, err := netListen.Accept()
if err != nil {
continue
}
handleConnection(conn)
}
}
func handleConnection(conn net.Conn) {
buffer := make([]byte, 2048)
for {
n, err := conn.Read(buffer)
CheckError(err)
fmt.Println("client : ", string(buffer[:n]))
conn.Write(buffer[:n])
}
fmt.Println("server end.")
}
func CheckError(err error) {
if err != nil {
fmt.Fprintf(os.Stderr, "Fatal error: %s", err.Error())
os.Exit(1)
}
}
```
#2
@tablecell</a> 就这?还不速速整一个文件服务器?
我看你asio异步不是挺厉害的?还不快点多线程?搁这写单线程玩具玩啥?
我看是你c++写的舒服还是golang写的舒服?
反正我是都写过的。
对了,要求能轻松交叉编译,至少拷贝部署无障碍。
提醒你,把include补上。你这个不可能编译过。
#3