-
-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
237 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,7 @@ | ||
--- | ||
title: '5.1 شبکه چیست' | ||
slug: network-basic | ||
weight: 9001 | ||
--- | ||
|
||
شبکه چیست؟؟؟ |
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,7 @@ | ||
--- | ||
title: '5.10 پروتکل quic' | ||
slug: go-quic-prtoocol | ||
weight: 9010 | ||
--- | ||
|
||
پروتکل quic |
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,131 @@ | ||
--- | ||
title: '5.2 سرور tcp مقدماتی' | ||
slug: go-tcp-server-begginer | ||
weight: 9002 | ||
--- | ||
|
||
ما میتوانیم در گو با استفاده از کتابخانه net یک سرور [tcp](https://fa.wikipedia.org/wiki/%D9%BE%D8%B1%D9%88%D8%AA%DA%A9%D9%84_%D9%87%D8%AF%D8%A7%DB%8C%D8%AA_%D8%A7%D9%86%D8%AA%D9%82%D8%A7%D9%84) ایجاد کنیم | ||
بعد از تکمیل شدن سرور با استفاده از دستور `telnet` به آن متصل میشویم | ||
|
||
در قطعه کد زیر | ||
با تایع `acceptLoop()` درخواست های اتصال را مپذیریم | ||
و با تایع `readLoop()` پیام های اتصال را میخوانیم | ||
|
||
|
||
```go | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"net" | ||
) | ||
|
||
// ساختار هر پیام در سرور | ||
type Message struct { | ||
// ادرس ip ارسال کننده پیام | ||
from string | ||
// متن و محتوای پیام | ||
payload []byte | ||
} | ||
|
||
// ساختار سرور | ||
type Server struct { | ||
// ادرس و یا پورت سرور | ||
listenAddr string | ||
// listener | ||
ln net.Listener | ||
// چنل پیام برای انتقال پیام های دریفاتی از اتصال ها بین گوروتین ها | ||
msgch chan Message | ||
} | ||
|
||
// ایجاد یک سرور جدید | ||
func newServer(listenAddr string) *Server { | ||
return &Server{ | ||
listenAddr: listenAddr, | ||
msgch: make(chan Message, 10), | ||
} | ||
} | ||
|
||
// شروع سرور و دریافت اتصال های جدید | ||
func (s *Server) start() error { | ||
// شروع سرور | ||
ln, err := net.Listen("tcp", s.listenAddr) | ||
if err != nil { | ||
return err | ||
} | ||
defer ln.Close() | ||
// مقدار دهی listener | ||
s.ln = ln | ||
// با تابع acceptLoop اتصال های جدید به سرور را مدیریت میکنیم | ||
// با استفاده از go هر اتصال را روی یک گوروتین مجزا مدیریت میکنیم | ||
go s.acceptLoop() | ||
|
||
return nil | ||
} | ||
|
||
func (s *Server) acceptLoop() { | ||
for { | ||
// اتصال های موجود را تایید میکنیم متغییر conn را با اتصال مورد نظر مقدار دهی میکنیم | ||
conn, err := s.ln.Accept() | ||
if err != nil { | ||
fmt.Println("accept error:", err) | ||
continue | ||
} | ||
// با استفاده از این تابع مقادیر ارسال شده توسط اتصال را به چنل message میدهیم | ||
go s.readLoop(conn) | ||
} | ||
} | ||
|
||
func (s *Server) readLoop(conn net.Conn) { | ||
defer conn.Close() | ||
buf := make([]byte, 2048) | ||
|
||
for { | ||
// پیام ارسال شده توسط هر اتصال را به متغییر buf میدهیم | ||
n, err := conn.Read(buf) | ||
if err != nil { | ||
fmt.Println("read error:", err) | ||
continue | ||
} | ||
|
||
s.msgch <- Message{ | ||
// ادرس ip ارسال کننده پیام از نوع net.IP | ||
from: conn.RemoteAddr().String(), | ||
// متن پیام | ||
payload: buf[:n], | ||
} | ||
|
||
// بعد از دریافت هر پیام یک پیام به عنوان پاسخ ارسال میکنیم | ||
conn.Write([]byte("your message recived!\n")) | ||
} | ||
} | ||
|
||
func main() { | ||
// ساخت سرور | ||
server := newServer(":3000") | ||
|
||
go func() { | ||
// در ازای هر پیام مقادیر آن را چاپ میکنیم | ||
for msg := range server.msgch { | ||
fmt.Printf("recived new from connection(%s): %s\n", msg.from, msg.payload) | ||
} | ||
}() | ||
|
||
// شروع سرور | ||
log.Fatal(server.start()) | ||
|
||
} | ||
|
||
``` | ||
|
||
بعد از پایان پیاده سازی سرور tcp | ||
با دستور زیر سرور خود را اجرا میکنیم: | ||
`go run main.go` | ||
|
||
و با دستور زیر در یک ترمینال مجزا به سرور متصل میشویم: | ||
`telnet localhost 3000` | ||
|
||
حال با نوشتن پیام و ارسال آن در ترمینال `telnet` متن پیام و ادرس اتصال در ترمینال سرور قابل مشاهده است. | ||
|
||
توجه داشته باشید که دستور `telnet` در ویندوز نیاز به فعال سازی دارد. |
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,7 @@ | ||
--- | ||
title: '5.3 سرور tcp پیشرفته' | ||
slug: go-tcp-server-advanced | ||
weight: 9003 | ||
--- | ||
|
||
شبکه tcp پیشرفته |
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,7 @@ | ||
--- | ||
title: '5.4 سرور udp مقدماتی' | ||
slug: go-udp-server-begginer | ||
weight: 9004 | ||
--- | ||
|
||
شبکه udp مقدماتی |
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,7 @@ | ||
--- | ||
title: '5.5 سرور udp پیشرفته' | ||
slug: go-udp-server-advanced | ||
weight: 9004 | ||
--- | ||
|
||
شبکه udp پیشرفته |
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,50 @@ | ||
--- | ||
title: '5.6 کتابخانه http سمت سرور مقدماتی' | ||
slug: go-http-server-begginer | ||
weight: 9006 | ||
--- | ||
|
||
آموزش کتابخانه http سمت سرور مقدماتی... | ||
|
||
یکی از قابلیت های زبان گو این است که می توان با استفاده از کتابخانه http داخلی گو استفاده کرده و وب سرور پیاده سازی کرد. | ||
|
||
|
||
برای پیاده سازی یک وب سرور حداقل به 3 چیز نیاز داریم: 1) مسیر 2) درگاه وب سرور 3) مقدار برگشتی | ||
|
||
|
||
مسیر: عبارت است از URL منحصر به فرد برای ارسال و دریافت اطلاعات ورودی | ||
|
||
درگاه وب سرور: هر وب سرور نیاز دارد که درگاه (Port) خاصی را در اختیار داشته باشد و همواره به آن درگاه گوش بسپارد. | ||
|
||
مقدار برگشتی: هرگاه کاربر به مسیری وارد میشود یا اطلاعاتی را برای وب سرور ارسال میکند، مقداری از سمت سرور برای کاربر ارسال میشود. | ||
|
||
|
||
|
||
یک نمونه وب سرور در گو | ||
|
||
```go | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
) | ||
|
||
func main() { | ||
|
||
http.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) { | ||
_, err := fmt.Fprint(writer, "<h1>Hellow World!</h1>") | ||
if err != nil { | ||
return | ||
} | ||
}) | ||
|
||
fmt.Println("Starting The Server on :3000...") | ||
err := http.ListenAndServe(":3000", nil) | ||
if err != nil { | ||
return | ||
} | ||
|
||
} | ||
|
||
``` |
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,7 @@ | ||
--- | ||
title: '5.7 کتابخانه http سمت سرور پیشرفته' | ||
slug: go-http-server-advanced | ||
weight: 9007 | ||
--- | ||
|
||
آموزش کتابخانه http سمت سرور پیشرفته... |
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,7 @@ | ||
--- | ||
title: '5.8 کتابخانه http سمت کلاینت مقدماتی' | ||
slug: go-http-client-begginer | ||
weight: 9008 | ||
--- | ||
|
||
آموزش کتابخانه http سمت کلاینت مقدماتی... |
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,7 @@ | ||
--- | ||
title: '5.9 کتابخانه http سمت کلاینت پیشرفته' | ||
slug: go-http-client-advanced | ||
weight: 9009 | ||
--- | ||
|
||
آموزش کتابخانه http سمت کلاینت پیشرفته... |