当前位置:网站首页>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)
}
边栏推荐
- Which securities company is safer to open a stock account
- Output results of convolution operation with multiple tensors and multiple convolution kernels
- Cookie, session, tooken
- Common loss function of deep learning
- js 公共库 cdn 推荐
- Excel PivotTable
- RFID让固定资产盘点更快更准
- 【微信授权登录】uniapp开发小程序,实现获取微信授权登录功能
- cookie、session、tooken
- [bottom pop-up selector] uniapp picker component - scroll selector popped up at the bottom
猜你喜欢

【会议资源】2022年第三届自动化科学与工程国际会议(JCASE 2022)

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

Export default the exported object cannot be deconstructed, and module Differences between exports

XMIND mind map

Datawhale 社区黑板报(第1期)

SSO single sign on implementation.

Advanced skills of testers: a guide to the application of unit test reports

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

gradle

2022 pinduoduo details / pinduoduo product details / pinduoduo SKU details
随机推荐
股票开户哪个证券公司比较安全
Node -- add compressed file
How does schedulerx help users solve the problem of distributed task scheduling?
Leetcode skimming: stack and queue 05 (inverse Polish expression evaluation)
Weather forecast applet source code weather wechat applet source code
AIX存储管理之卷组属性的查看和修改(二)
什么是商业养老保险?商业养老保险安全靠谱吗?
【CTF】bjdctf_2020_babystack2
Collection: comprehensive summary of storage knowledge
【CTF】bjdctf_ 2020_ babystack2
【微信授权登录】uniapp开发小程序,实现获取微信授权登录功能
[eight sorts ①] insert sort (direct insert sort, Hill sort)
【八大排序①】插入排序(直接插入排序、希尔排序)
Common loss function of deep learning
测试人进阶技能:单元测试报告应用指南
449 original code, complement code, inverse code
Promise and modular programming
Leetcode skimming: stack and queue 02 (realizing stack with queue)
The new version of graphic network PDF will be released soon
【八大排序④】归并排序、不基于比较的排序(计数排序、基数排序、桶排序)