接收消息

更多Go内容请见: https://blog.csdn.net/weixin_39777626/article/details/85066750

net/http标准库

组成部分
在这里插入图片描述

构建服务器

Go Web服务器
最简单的Web服务器

package main

import (
	"net/http"
)

func main() {
	http.ListenAndServe("",nil)
}

附加配置的Web服务器

package main

import (
	"net/http"
)

func main() {
	server := http.Server{
		Addr:    "127.0.0.1:8080",
		Handler: nil,
	}
	server.ListenAndServe()
}

HTTPS提供服务

  • cert.pem:SSL证书
  • key.pem:私钥
  • SSL:(Secure Socket
    Layer,安全套接字层)通过公钥基础设施为通信双方提供数据加密和身份验证的协议,后更名为TLS(Transport Layer
    Security,传输层安全协议)
  • HTTPS:在SSL之连接上层进行HTTP通信

通过HTTPS提供服务

package main

import (
	"net/http"
)

func main() {
	server := http.Server{
		Addr:    "127.0.0.1:8080",
		Handler: nil,
	}
	server.ListenAndServeTLS("cert.pem", "key.pem")
}

生成个人使用的SSL证书以及服务器私钥

package main

import (
	"crypto/rand"
	"crypto/rsa"
	"crypto/x509"
	"crypto/x509/pkix"
	"encoding/pem"
	"math/big"
	"net"
	"os"
	"time"
)

func main() {
	max:=new(big.Int).Lsh(big.NewInt(1),128)
	serialNumber,_:=rand.Int(rand.Reader,max)
	subject:=pkix.Name{
		Organization:		[]string{"Manning Publications Co,"},
		OrganizationalUnit:	[]string{"Books"},
		CommonName:			"Go Web Programming",
	}
	
	template:=x509.Certificate{
		SerialNumber:	serialNumber,	//证书序列号
		Subject:		subject,				//证书标题
		//证书有效期:一年
		NotBefore:		time.Now(),
		NotAfter:		time.Now().Add(365*24*time.Hour),
		KeyUsage:		x509.KeyUsageKeyEncipherment|x509.KeyUsageDigitalSignature,
		ExtKeyUsage:	[]x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},	//X.509证书用于服务器验证操作
		IPAddresses:	[]net.IP{net.ParseIP("127.0.0.1")},						//证书只能在127.0.0.1上运行
	}
	
	//生成RSA私钥
	pk,_:=rsa.GenerateKey(rand.Reader,2048)
	
	//创建出经过DER编码格式化的字节切片
	derBytes,_:=x509.CreateCertificate(rand.Reader,&template,&template,&pk.PublicKey,pk)
	
	//将证书编码到cert.pem文件里
	certOut,_:=os.Create("cert.pem")
	pem.Encode(certOut,&pem.Block{Type:"CERTIFICATE",Bytes:derBytes})
	certOut.Close()
	
	//将密钥编码并保存到key.pem文件里
	keyOut,_:=os.Create("key.pem")
	pem.Encode(keyOut,&pem.Block{Type:"RSA PIRVATE KEY",Bytes:x509.MarshalPKCS1PrivateKey(pk)})
	keyOut.Close()
}

处理器和处理函数

处理请求
DefaulltServeMux
在这里插入图片描述

package main

import (
	"fmt"
	"net/http"
)

type MyHandleer struct{}

func (h *MyHandleer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, "Hello World!")
}
func main() {
	handler := MyHandleer{}
	server := http.Server{
		Addr:    "127.0.0.1:8080",
		Handler: &handler,
	}
	server.ListenAndServe()
}

使用多个处理器
在这里插入图片描述

package main

import (
	"fmt"
	"net/http"
)

type HelloHandleer struct{}

func (h *HelloHandleer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, "Hello!")
}

type WorldHandleer struct{}

func (h *WorldHandleer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, "World!")
}

func main() {
	hello := HelloHandleer{}
	world := WorldHandleer{}
	server := http.Server{
		Addr: "127.0.0.1:8080",
	}

	http.Handle("/hello", &hello)
	http.Handle("/world", &world)

	server.ListenAndServe()
}

处理器函数

package main

import (
	"fmt"
	"net/http"
)

func hello(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, "Hello!")
}

func world(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, "World!")
}

func main() {
	server := http.Server{
		Addr: "127.0.0.1:8080",
	}

	http.HandleFunc("/hello", hello)
	http.HandleFunc("/world", world)

	server.ListenAndServe()
}

串联多个处理器和处理器函数

串联两个二处理器函数

package main

import (
	"fmt"
	"net/http"
	"reflect"
	"runtime"
)

func hello(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, "Hello!")
}

func log(h http.HandlerFunc) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		name := runtime.FuncForPC(reflect.ValueOf(h).Pointer()).Name()
		fmt.Println("Handler function called - " + name)
		h(w, r)
	}
}

func main() {
	server := http.Server{
		Addr: "127.0.0.1:8080",
	}

	http.HandleFunc("/hello", log(hello))

	server.ListenAndServe()
}

串联多个处理器

package main

import (
	"fmt"
	"net/http"
)

type HelloHandleer struct{}

func (h HelloHandleer) ServeHTTP (w http.ResponseWriter,r *http.Request){
	fmt.Fprintf(w, "Hello!")
}

func log(h http.HandlerFunc) http.HandlerFunc {
	return http.HandlerFunc (func(w http.ResponseWriter, r *http.Request){
		fmt.Printf("Handler called - %T\n",h)
		h.ServeHTTP (w,r)
	})
}	
	
func protect(h http.Handler) http.Handler{
	return http.HandlerFunc(func(w http.ResponseWriter,r *http.Request){
		h.ServeHTTP (w,r)
	})
}

func main() {
	server := http.Server{
		Addr: "127.0.0.1:8080",
	}
	
	hello:=HelloHandleer{}
	http.Handle("/hello",protect(log(hello)))
	server.ListenAndServe()
}

ServeMux和DefaultServeMux

ServeMuxDefaultServeMux
实质HTTP请求多路复用器ServeMux的一个实例
功能接收HTTP请求并根据请求中的URL将请求重定向到正确的处理器引入net/http标准库的程序都可以使用这个实例

使用其他多路复用器

注意:编译前,先安装git,然后在终端执行以下命令,以安装HttpRouter库

go get github.com/julienschmidt/httprouter

使用HttpRouter实现的服务器

package main

import (
	"fmt"
	"net/http"

	"github.com/julienschmidt/httprouter"
)

func hello(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
	fmt.Fprintf(w, "hello,%s!\n", p.ByName("name"))
}

func main() {
	mux := httprouter.New()
	mux.GET("/hello/:name", hello)

	server := http.Server{
		Addr:    "127.0.0.1:8080",
		Handler: mux,
	}
	server.ListenAndServe()
}

更多Go内容请见: https://blog.csdn.net/weixin_39777626/article/details/85066750

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这段代码定义了一个名为`ReceiverThread`的线程类,用于接收来自其他用户的消息。具体解释如下: 1. `class ReceiverThread extends Thread`定义了一个名为`ReceiverThread`的线程类,继承自`Thread`类,表示可以在独立的线程中运行。 2. `public void run()`是`Thread`类中的一个方法,表示线程运行时需要执行的逻辑代码。在这里,`run()`方法中编写了接收消息的逻辑代码。 3. `byte[] data = new byte[1024]`创建了一个长度为1024的字节数组,用于存放接收到的消息。 4. `while(true)`表示一个死循环,表示不断地接收消息。 5. `DatagramPacket packet = new DatagramPacket(data,data.length)`创建了一个`DatagramPacket`对象,用于存储接收到的消息。其中,`data`参数表示接收到的数据存放在`data`字节数组中,`data.length`表示数据的最大长度。 6. `socket.receive(packet)`等待接收来自其他用户的消息,并将消息存储在`packet`对象中。如果没有接收消息,这个方法会一直阻塞等待,直到接收消息为止。 7. `int len = packet.getLength()`获取接收到的消息的长度。 8. `String msg = new String(data,0,len)`将字节数组中的消息内容转换为字符串。 9. `System.out.println("收到消息了:"+msg)`在控制台输出接收到的消息。 10. `msgAddToMsgPanel(msg,FlowLayout.LEFT)`将接收到的消息添加到聊天界面中的消息面板中,`FlowLayout.LEFT`表示将消息靠左对齐。 11. `catch(IOException e)`捕获可能出现的异常,并抛出`RuntimeException`异常。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值