GO语言UDP小笔记

GO语言UDP小笔记,第1张

概述package mainimport ( "bytes" "encoding/json" "fmt" "io" "net" "os")type Fileinfo struct { Name string Path string}func server() { laddr := &net.UDPAddr{IP: []byte{127, 0, 0, 1}, Port:
package mainimport (	"bytes"	"enCoding/Json"	"fmt"	"io"	"net"	"os")type fileinfo struct {	name string	Path string}func server() {	laddr := &net.UDPAddr{IP: []byte{127,1},Port: 1789}	con,err := net.ListenUDP("udp",laddr)	if err != nil {		fmt.Println(err)		return	}	defer con.Close()	for {		Buf := make([]byte,1024)		n,raddr,_ := con.ReadFromUDP(Buf)		var info fileinfo		Json.Unmarshal(Buf[:n],&info)		if info.name != "" {			if info.Path != "" {				e := os.Chdir(info.Path)				if e != nil {					con.Writeto([]byte(e.Error()),raddr)					continue				}			}			con.WritetoUDP([]byte("OK"),raddr)			file,_ := os.Create(info.name)			defer file.Close()			for {				Buf := make([]byte,4096)				n,r,_ := con.ReadFromUDP(Buf)				if r.String() != raddr.String() {					continue				}				if n == 4 && bytes.Contains([]byte{0,69,79,70},Buf[:n]) {					file.Sync()					file.Close()					break				}				file.Write(Buf[:n])			}		}	}}func clIEnt() {	con,e := net.Dial("udp","127.0.0.1:1789")	if e != nil {		fmt.Println(e)		return	}	defer con.Close()	var info fileinfo = fileinfo{name: "WinMD5.exe",Path: "D:"}	b,_ := Json.Marshal(info)	con.Write([]byte(b))	Buf := make([]byte,1024)	n,_ := con.Read(Buf)	if string(Buf[:n]) == "OK" {		file,_ := os.Open("3.go")		for {			Buf := make([]byte,4096)			n,e := file.Read(Buf)			if e != nil {				if e == io.EOF {					break				}				fmt.Println(e)				break			}			con.Write(Buf[:n])		}		con.Write([]byte{0,70})	}}
总结

以上是内存溢出为你收集整理的GO语言UDP小笔记全部内容,希望文章能够帮你解决GO语言UDP小笔记所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1287034.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-09
下一篇 2022-06-09

发表评论

登录后才能评论

评论列表(0条)

保存