分类 Demo 下的文章

“各种示例”

sendto(),把UDP数据报发给指定地址和端口。
函数原型
int sendto (int s, const void buf, int len, unsigned int flags, const struct sockaddr to, int tolen);
参数说明
s: socket描述符。
buf: UDP数据报缓存地址。
len: UDP数据报长度。
flags: 该参数一般为0。
to: sendto()函数参数,struct sockaddr_in类型,指明UDP数据发往哪里报。
tolen: 对方地址长度,一般为:sizeof(struct sockaddr_in)。
函数返回值
对于sendto()函数,成功则返回实际传送出去的字符数,失败返回-1,错误原因存于errno 中。

发送udp数据示例

int udp_send(string ip, int port, string data)
{
    int sockfd;
    if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
    {
        printf("socket error\n");
        return -1;
    }
    struct sockaddr_in addrto;
    bzero(&addrto, sizeof(struct sockaddr_in));
    addrto.sin_family = AF_INET;
    addrto.sin_addr.s_addr = htonl(INADDR_ANY);
    addrto.sin_port = htons(9999);
    //设置广播
    int yes = 1;
    setsockopt(m_nsockfd, SOL_SOCKET, SO_BROADCAST, &yes, sizeof(yes));    
    if(bind(m_nsockfd, (struct sockaddr *) & (addrto), sizeof(struct sockaddr_in)) == -1)
    {
        printf("udp_listen bind error...\n");
        return -1;
    }  
    int addrlen = sizeof(sockaddr_in);
    struct sockaddr_in addr;
    bzero(&addr, sizeof(struct sockaddr_in));    
    addr.sin_family = AF_INET;
    addr.sin_port = htons(port);
    addr.sin_addr.s_addr = inet_addr(ip.c_str());
    return sendto(m_nsockfd, data.data(), data.length(), 0, (struct sockaddr *)&addr, addrlen);
}

php 实现udp发送数据,主要使用 socket_sendto就可以了。

  $socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
  //PHP UDP socket_ recv() timeout
  socket_set_option($socket,SOL_SOCKET,SO_RCVTIMEO,array("sec"=>2,"usec"=>0));

  //Set socket options.
  socket_set_nonblock($socket);
  socket_set_option($socket, SOL_SOCKET, SO_BROADCAST, 1);
  socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1);
  if (defined('SO_REUSEPORT'))
      socket_set_option($socket, SOL_SOCKET, SO_REUSEPORT, 1);

  //Bind to any address & rand port 
  socket_bind($socket, '0.0.0.0', 9999)
  
  $senddata = "hello";
  socket_sendto($socket,$sendcmd, strlen($senddata), 0, "127.0.0.1", 54321);

php 设置socket 非阻塞

socket_set_nonblock($socket);

php 设置udp 广播

socket_set_option($socket, SOL_SOCKET, SO_BROADCAST, 1);

php 设置 udp 地址端口重用

socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1);

php 设置udp_recv接收超时

  socket_set_option($socket,SOL_SOCKET,SO_RCVTIMEO,array("sec"=>2,"usec"=>0));

php 字符串转大写

uppercase

php 判断http post

 function is_post()
{
    return isset($_SERVER['REQUEST_METHOD']) && strtoupper($_SERVER['REQUEST_METHOD'])=='POST';    
}

php 取随机数

echo(rand();
echo(rand();
echo(rand(10,100))

php socket 官方链接地址
https://www.php.net/manual/en/book.sockets.php

php explode 示例

$arr = explode(":", $buf);

php trim 示例 

$str = "Hello Const!";
echo $str . "<br>";
echo trim($str,"Const!");

在golang中使用cgo调用openssl库的md5方法,最简单的方法

package main

/*
#cgo CFLAGS: -I ./include
#cgo LDFLAGS: -L ./lib -lcrypto
#include <stdlib.h>
#include <openssl/md5.h>
*/
import "C"

import (
    "encoding/hex"
    "fmt"
    "strings"
    "unsafe"
)

func main() {

    fmt.Println("go openssl md5 demo.")
    data := []byte("const.net.cn")
    md := make([]byte, 16)
    C.MD5((*C.uchar)(unsafe.Pointer(&data[0])), C.size_t(len(data)), (*C.uchar)(unsafe.Pointer(&md[0])))
    md5Str := hex.EncodeToString(md)
    md5Str = strings.ToUpper(md5Str)
    fmt.Printf("md5Str = %s\n", md5Str)
}

输出:

go run .
go openssl md5 demo.
md5Str = FA8424A7B72EB90BF04685205ECC5760
echo -n "const.net.cn" | md5sum 
fa8424a7b72eb90bf04685205ecc5760  -

在golang中使用cgo调用openssl库的md5方法,使用MD5_Init, MD5_Update, MD5_Final.

package main

/*
#cgo CFLAGS: -I ./include
#cgo LDFLAGS: -L ./lib -lcrypto
#include <stdlib.h>
#include <openssl/md5.h>
*/
import "C"

import (
    "encoding/hex"
    "fmt"
    "strings"
    "unsafe"
)

func main() {
    var ctx C.MD5_CTX
    C.MD5_Init(&ctx)
    data := []byte("https://const.net.cn")
    C.MD5_Update(&ctx, unsafe.Pointer(&data[0]), C.size_t(len(data)))
    md := make([]byte, 16)
    C.MD5_Final((*C.uchar)(unsafe.Pointer(&md[0])), &ctx)
    md5Str := hex.EncodeToString(md)
    md5Str = strings.ToUpper(md5Str)
    fmt.Printf("md5Str = %s\n", md5Str)
}

输出:

go run .
md5Str = 682D2C63236AF6E721794B2988FC1D44
echo -n "https://const.net.cn" | md5sum 
682d2c63236af6e721794b2988fc1d44  -

在golang中使用cgo调用openssl库的md5方法,不使用var定义,使用new来定义C.MD5_CTX

package main

/*
#cgo CFLAGS: -I ./include
#cgo LDFLAGS: -L ./lib -lcrypto
#include <stdlib.h>
#include <openssl/md5.h>
*/
import "C"

import (
    "encoding/hex"
    "fmt"
    "strings"
    "unsafe"
)

func main() {
    ctx := new(C.MD5_CTX)
    C.MD5_Init(ctx)
    data := []byte("const.net.cn")
    C.MD5_Update(ctx, unsafe.Pointer(&data[0]), C.size_t(len(data)))
    md := make([]byte, 16)
    C.MD5_Final((*C.uchar)(unsafe.Pointer(&md[0])), ctx)
    md5Str := hex.EncodeToString(md)
    md5Str = strings.ToUpper(md5Str)
    fmt.Printf("md5Str = %s\n", md5Str)
}

输出:
go run .
md5Str = FA8424A7B72EB90BF04685205ECC5760
echo -n "const.net.cn" | md5sum
fa8424a7b72eb90bf04685205ecc5760 -