Skip to content

Send and receive binary data, including structs, strings, int32, slices, and folders, using the TCP protocol in Go.

Notifications You must be signed in to change notification settings

rafaelsouzaribeiro/go-socket

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

36 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

To customize the struct, go to pkg/global/index.go and modify the Custom struct. You also need to fix the println statements in the channel.

To send data in string

client:

factories := factory.NewClient(factory.FactoryClient{
			TypeString: "Message",
		})
buffer, err := factories.GetClient()

if err != nil {
    panic(err)
}

_, err = conn.Write(buffer)
if err != nil {
    panic(err)
}

Server:
fmt.Printf("Server started at %s:%s \n", connect.Host, connect.Port)
factories := factory.NewServer(factory.String)

channel := make(chan string)
factories.ChannelString = channel

go func() {
    for p := range channel {
        fmt.Printf("Message received: %s \n", p)
    }
}()

for {
    conn, err := listener.AcceptTCP()
    if err != nil {
        log.Fatal(err)
    }

    go factories.GetServer(connect, conn)
}

To send data in slice

client:

people := []global.Custom{
   {Name: "Rafael", Age: 38},
   {Name: "Maria", Age: 30},
   {Name: "João", Age: 25},
}
factories := factory.NewClient(factory.FactoryClient{
   	TypeSlice: people,
   })
buffer, err := factories.GetClient()

if err != nil {
   panic(err)
}

_, err = conn.Write(buffer)
if err != nil {
   panic(err)
}

Server:
fmt.Printf("Server started at %s:%s \n", connect.Host, connect.Port)
factories := factory.NewServer(factory.Slice)

channel := make(chan global.Custom)
factories.ChannelCustom = channel

go func() {
   for p := range channel {
   	fmt.Printf("Message received. Name: %s, Age: %d\n", p.Name, p.Age)
   }
}()

for {
   conn, err := listener.AcceptTCP()
   if err != nil {
   	log.Fatal(err)
   }

   go factories.GetServer(connect, conn)
}

To send data in struct

client:

factories := factory.NewClient(factory.FactoryClient{
		TypeStruct: global.Custom{Name: "Rafael", Age: 38},
	})
buffer, err := factories.GetClient()

if err != nil {
	panic(err)
}

_, err = conn.Write(buffer)
if err != nil {
	panic(err)
}

Server:
fmt.Printf("Server started at %s:%s \n", connect.Host, connect.Port)
factories := factory.NewServer(factory.Struct)

channel := make(chan global.Custom)
factories.ChannelCustom = channel

go func() {
	for p := range channel {
		fmt.Printf("Message received. Name: %s, Age: %d\n", p.Name, p.Age)
	}
}()

for {
	conn, err := listener.AcceptTCP()
	if err != nil {
		log.Fatal(err)
	}

	go factories.GetServer(connect, conn)
}

To send data in int32

client:

valueint := 5
factories := factory.NewClient(factory.FactoryClient{
		TypeInt: &valueint,
	})
buffer, err := factories.GetClient()

if err != nil {
	panic(err)
}

_, err = conn.Write(buffer)
if err != nil {
	panic(err)
}

Server:
fmt.Printf("Server started at %s:%s \n", connect.Host, connect.Port)
factories := factory.NewServer(factory.Int32)
channel := make(chan int32)
factories.ChannelInt = channel

go func() {
	for p := range channel {
		fmt.Printf("Message received: %d\n", p)
	}
}()

for {
	conn, err := listener.AcceptTCP()
	if err != nil {
		log.Fatal(err)
	}

	go factories.GetServer(connect, conn)
}

How to send a folder?

client:

factories := factory.NewClient(factory.FactoryClient{
	TypeFolder: "../../internal",
})
buffer, err := factories.GetClient()

if err != nil {
	panic(err)
}

_, err = conn.Write(buffer)
if err != nil {
	panic(err)
}

Server:
fmt.Printf("Server started at %s:%s \n", connect.Host, connect.Port)
factories := factory.NewServer(factory.Folder)
channel := make(chan string)
factories.ChannelString = channel

go func() {
	for p := range channel {
		fmt.Printf("Message received: %s", p)
	}
}()

i := 0
for {
	factories.OutputFolderFileName = fmt.Sprintf("../../cmd/received%d.zip", i)

	conn, err := listener.AcceptTCP()
	if err != nil {
		log.Fatal(err)
	}

	go factories.GetServer(connect, conn)
	i++
}

About

Send and receive binary data, including structs, strings, int32, slices, and folders, using the TCP protocol in Go.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages