当前位置:网站首页>970 golang realizes the communication between multithreaded server and client
970 golang realizes the communication between multithreaded server and client
2022-07-02 00:57:00 【-Lin Zeyu】
package main
import (
"net"
"fmt"
"time"
)
func server(){
listen_sock, err := net.Listen("tcp", "0.0.0.0:8888")
if err != nil {
fmt.Println("listen error")
return;
}
defer listen_sock.Close()
conn, err := listen_sock.Accept()
if err != nil {
fmt.Println("accept error")
return;
}
defer conn.Close()
buf := make([]byte, 100)
for(true){
n, err := conn.Read(buf)
if err != nil {
fmt.Println("conn closed")
return
}
fmt.Println("msg size:", n, string(buf[0:n]))
}
}
func client(){
conn, err := net.Dial("tcp", "127.0.0.1:8888")
if err != nil {
fmt.Println("check server")
return
}
defer conn.Close()
for(true){
s := []byte("abc")
conn.Write(s)
time.Sleep(1 * time.Second)
}
}
func main(){
go server()
go client()
time.Sleep(10 * time.Minute)
}
边栏推荐
- Random avatar encyclopedia, multi category wechat applet source code with history_ Support traffic master
- 【八大排序②】选择排序(选择排序,堆排序)
- Creating logical volumes and viewing and modifying attributes for AIX storage management
- Entrepreneurship is a little risky. Read the data and do a business analysis
- UDS bootloader of s32kxxx bootloader
- Which securities company is safer to open a stock account
- @Valid参数校验不生效
- Han Zhichao: real time risk control practice of eBay based on graph neural network
- 【底部弹出-选择器】uniapp Picker组件——底部弹起的滚动选择器
- JS -- image to base code, base to file object
猜你喜欢

2023 Lexus ES products have been announced, which makes great progress this time

Leetcode skimming: stack and queue 02 (realizing stack with queue)

JMeter做接口测试,如何提取登录Cookie

Leetcode skimming: stack and queue 01 (realizing queue with stack)

How does schedulerx help users solve the problem of distributed task scheduling?

Collection: comprehensive summary of storage knowledge

Entrepreneurship is a little risky. Read the data and do a business analysis

【八大排序③】快速排序(动图演绎Hoare法、挖坑法、前后指针法)

Zak's latest "neural information transmission", with slides and videos

Tensorflow tensor convolution, input and convolution kernel dimension understanding
随机推荐
使用 ES 实现疫情地图或者外卖点餐功能(含代码及数据)
Global and Chinese markets for maritime services 2022-2028: Research Report on technology, participants, trends, market size and share
With the acquisition of Xilinx, AMD is more than "walking on two legs" | Jiazi found
Comprehensive broadcast of global and Chinese markets 2022-2028: Research Report on technology, participants, trends, market size and share
[JS download files through url]
股票开户哪个证券公司比较安全
Weather forecast applet source code weather wechat applet source code
Leetcode skimming: binary tree 02 (middle order traversal of binary tree)
测试人进阶技能:单元测试报告应用指南
[wechat authorized login] the small program developed by uniapp realizes the function of obtaining wechat authorized login
excel数据透视表
Slf4j print abnormal stack information
Use es to realize epidemic map or take out order function (including code and data)
Global and Chinese markets for supply chain strategy and operation consulting 2022-2028: Research Report on technology, participants, trends, market size and share
JMeter做接口测试,如何提取登录Cookie
How to extract login cookies when JMeter performs interface testing
工作中非常重要的测试策略,你大概没注意过吧
Node -- add compressed file
2022拼多多详情/拼多多商品详情/拼多多sku详情
New version of free mobile phone, PC, tablet, notebook four terminal Website thumbnail display diagram online one click to generate website source code