-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added examples golang net lib, tcp and udp
- Loading branch information
Showing
6 changed files
with
143 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package client | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
"bufio" | ||
) | ||
|
||
func TCPClient() { | ||
buf := make([]byte, 1024) | ||
conn, err := net.Dial("tcp", "localhost:1900") | ||
if err != nil { | ||
fmt.Printf("Connection error: %v", err) | ||
return | ||
} | ||
fmt.Fprintf(conn, "Some message to server!") | ||
_, err = bufio.NewReader(conn).Read(buf) | ||
if err == nil { | ||
fmt.Printf("Some in buf: %s", buf) | ||
} else { | ||
fmt.Printf("Read error: %s", err) | ||
} | ||
conn.Close() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package client | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
"bufio" | ||
) | ||
|
||
func UDPClient() { | ||
buf := make([]byte, 1024) | ||
conn, err := net.Dial("udp", "localhost:1901") | ||
if err != nil { | ||
fmt.Printf("Connection error: %v", err) | ||
return | ||
} | ||
fmt.Fprintf(conn, "Some message to UDP server!") | ||
_, err = bufio.NewReader(conn).Read(buf) | ||
if err == nil { | ||
fmt.Printf("Some in buf: %s", buf) | ||
} else { | ||
fmt.Printf("Read error: %s", err) | ||
} | ||
conn.Close() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module 06e_network | ||
|
||
go 1.23.2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
// "golang/cource/06e_network/servers" | ||
"golang/cource/06e_network/clients" | ||
) | ||
|
||
func main() { | ||
fmt.Println("Started test servers.") | ||
// server.TCPServer() | ||
// server.UDPServer() | ||
// client.TCPClient() | ||
client.UDPClient() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package server | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
"os" | ||
) | ||
|
||
func TCPServer() { | ||
listener, err := net.Listen("tcp", "localhost:1900") | ||
if err != nil { | ||
fmt.Println("Bind listener error: ", err.Error()) | ||
os.Exit(1) | ||
} | ||
defer listener.Close() | ||
fmt.Println("Bind on: localhost:1900") | ||
for { | ||
conn, err := listener.Accept() | ||
if err != nil { | ||
fmt.Println("Error accepting: ", err.Error()) | ||
os.Exit(1) | ||
} | ||
go handleRequest(conn) | ||
} | ||
} | ||
|
||
func handleRequest(conn net.Conn) { | ||
buf := make([]byte, 1024) | ||
|
||
reqLen, err := conn.Read(buf) | ||
if err != nil { | ||
fmt.Println("Error reading: ", err.Error()) | ||
} | ||
fmt.Println("Some recieved:", reqLen) | ||
bytesWritten, err := conn.Write([]byte("Something recieved.")) | ||
if err != nil { | ||
fmt.Println("Error writing: ", err.Error()) | ||
} | ||
fmt.Printf("Bytes written: %d\n", bytesWritten) | ||
conn.Close() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package server | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
) | ||
|
||
func sendResponse(conn *net.UDPConn, addr *net.UDPAddr) { | ||
_, err := conn.WriteToUDP([]byte("Response from server..."), addr) | ||
if err != nil { | ||
fmt.Printf("Couldn't send response: %v\n", err) | ||
} | ||
} | ||
|
||
func UDPServer() { | ||
buf := make([]byte, 2048) | ||
addr := net.UDPAddr{ | ||
Port: 1901, | ||
IP: net.ParseIP("127.0.0.1"), | ||
} | ||
listener, err := net.ListenUDP("udp", &addr) | ||
if err != nil { | ||
fmt.Printf("Listen UDP error: %v\n", err) | ||
return | ||
} | ||
fmt.Println("listening on localhost:1901.") | ||
for { | ||
_, remoteaddr, err := listener.ReadFromUDP(buf) | ||
fmt.Printf("Buffer recieved: %s", buf) | ||
if err != nil { | ||
fmt.Printf("Read UDP error: %v\n", err) | ||
return | ||
} | ||
go sendResponse(listener, remoteaddr) | ||
} | ||
} |