h5客户端与服务器的socket怎样相连

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了h5客户端与服务器的socket怎样相连相关的知识,希望对你有一定的参考价值。

参考技术A 服务器端使用 nodejs语句编写:

var http=require('http');
var fs=require('fs');
var io=require('socket.io');
var httpObj=http.createServer(function(req,res)

);
httpObj.listen(8080);
var ws=io.listen(httpObj);
//服务器端接收数据
ws.on('connection',function(socket)
setInterval(function()
socket.emit('time',Date.now());
,1000);本回答被提问者采纳

udp socket: 简单的数据包与视频流传输

简单的包传输

客户端向服务端发送数据包,服务端打印出来。
我们来传送一首诗吧,柳永的《雨霖铃》
服务端:

#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <string.h>
#include <netinet/in.h>
#include <unistd.h>
#include <stdlib.h>
#include <arpa/inet.h>
#define PORT 24000

int main() 
    int s=socket(AF_INET,SOCK_DGRAM,0);
    if(s==-1)
        perror("create socket error: ");
        exit(1);
    
    struct sockaddr_in serv,client;
    bzero(&serv,sizeof(serv));
    serv.sin_family=AF_INET;   
    serv.sin_addr.s_addr=inet_addr("10.21.100.153");// targe IP  
    serv.sin_port=htons(PORT);  
    if(bind(s,(struct sockaddr*)&serv,sizeof(serv))==-1)
        perror("bind error: ");
        exit(1);
    
    int count = 0;
    char *buff = (char *)malloc(100);
    while(count < 10)
        int addr_len = sizeof(client);
        memset(buff,0,100);
        int ret = recvfrom(s,buff,100,0,(struct sockaddr *)&client,&addr_len); 
        if(ret == -1)
            perror("recvfrom error: ");
            exit(1);
        
        else 
            count++;
            printf("%s\\n",buff);
        
    
    close(s);
    if(buff) free(buff);
    return 0;

客户端:

#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <string.h>
#include <netinet/in.h>
#include <unistd.h>
#include <stdlib.h>
#include <arpa/inet.h>
#define PORT 24000

int main() 
        int s=socket(AF_INET,SOCK_DGRAM,0);
        if(s==-1)
             perror("create socket error: ");
             exit(1);
        
        struct sockaddr_in serv;
        bzero(&serv,sizeof(serv));
        serv.sin_family=AF_INET;   
        serv.sin_addr.s_addr = inet_addr("10.21.100.153");    // targe IP 
        serv.sin_port=htons(PORT);
        char *buff = NULL;
        int i = 0;
        char str[10][80]="雨霖铃","","寒蝉凄切,对长亭晚,骤雨初歇。","都门帐饮无绪,留恋处,兰舟摧发。",
            "执手相看泪眼,竟无语凝噎。","念去去千里烟波,暮霭沈沈楚天阔。","多情自古伤离别,更那堪冷落清秋节。",
        "今宵酒醒何处,杨柳岸、晓风残月。","此去经年,应是良辰好景虚设。","便纵有千种风情,更与何人说。";
        while(i < 10)
             buff = str[i];
             sendto(s,buff,strlen(buff),0,(struct sockaddr *)&serv,sizeof(serv));
             sleep(1);
             i++;
        
        close(s);
        buff = NULL;
        return 0;
./client
./server
雨霖铃

寒蝉凄切,对长亭晚,骤雨初歇。
都门帐饮无绪,留恋处,兰舟摧发。
执手相看泪眼,竟无语凝噎。
念去去千里烟波,暮霭沈沈楚天阔。
多情自古伤离别,更那堪冷落清秋节。
今宵酒醒何处,杨柳岸、晓风残月。
此去经年,应是良辰好景虚设。
便纵有千种风情,更与何人说。

视频传输

现在,我们用vlc传输视频给一个IP,然后转发给另一个IP。这就类似于给一台服务器传视频,服务器转发给终端。
vlc发送视频:
媒体——打开多个文件——添加,选择视频——流——下一步——new destination, UDP——添加,填写IP和相应的端口——profile, video-H.264+MP3(TS)——下一步——stream all elementary streams——stream
接收:
窗口:打开网络串流–填写udp://@:port
命令行: vlc udp://@IP:port 或者 vlc udp://@:port

typeIPPort
转发端10.21.100.1525000
接收端10.21.100.15324000

code:

#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <string.h>
#include <netinet/in.h>
#include <unistd.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <signal.h>
#define PORT1 5000
#define PORT2 24000
void stop(int sig)
    puts("finish data transfer.");
    exit(0);

int main() 
    signal(SIGINT,stop);
    struct sockaddr_in serv1, serv2, client;
    bzero(&serv1,sizeof(serv1));
    serv1.sin_family=AF_INET;   
    serv1.sin_addr.s_addr=inet_addr("10.21.100.152");// serv1 used to recv data and transfer.
    serv1.sin_port=htons(PORT1);  
    int fd1 = socket(AF_INET,SOCK_DGRAM,0);
    if(fd1 == -1)
        perror("create socket fd1 error: ");
        exit(1);
    
    if(bind(fd1,(struct sockaddr*)&serv1,sizeof(serv1))==-1)
        perror("bind serv1 ");
        exit(1);
    
    bzero(&serv2,sizeof(serv2));
    serv2.sin_family=AF_INET;   
    serv2.sin_addr.s_addr=inet_addr("10.21.100.153");// serv2 used to recv data for vlc display.
    serv2.sin_port=htons(PORT2);  

    void *buff = malloc(1500); // vlc number of data bits is 1316
    while(1)
        int addr_len = sizeof(client);
        memset(buff,0,1500);
        int ret = recvfrom(fd1,buff,1500,0,(struct sockaddr *)&client,&addr_len); 
        printf("%5d ",ret);
        if(ret == -1)
           perror("recvfrom error: ");
           exit(1);
        
        else 
           //ret = sendto(fd1,buff,strlen(buff),0,(struct sockaddr *)&serv2,sizeof(serv2)); // not fd2
           ret = sendto(fd1,buff,ret,0,(struct sockaddr *)&serv2,sizeof(serv2)); // not fd2
           printf("%5d\\n",ret);
           if(ret == -1)
               perror("sendto error: ");
               exit(1);
            
        
    
    return 0;

效果:


注意:在sendto()中的字节数参数,不要用strlen(buff),最好使用recvfrom()的返回值。
我们可以打印一些重要的信息来查看内存情况。
code:

#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <string.h>
#include <netinet/in.h>
#include <unistd.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <signal.h>
#define PORT1 5000
#define PORT2 24000
void stop(int sig)
    puts("\\n finish data transfer.");
    exit(0);

int main() 
    signal(SIGINT,stop);
    struct sockaddr_in serv1, serv2, client;
    bzero(&serv1,sizeof(serv1));
    serv1.sin_family=AF_INET;   
    serv1.sin_addr.s_addr=inet_addr("10.21.100.152");// serv1 used to recv data and transfer.
    serv1.sin_port=htons(PORT1);  
    int fd1 = socket(AF_INET,SOCK_DGRAM,0);
    if(fd1 == -1)
        perror("create socket fd1 error: ");
        exit(1);
    
    if(bind(fd1,(struct sockaddr*)&serv1,sizeof(serv1))==-1)
        perror("bind serv1 ");
        exit(1);
    
    bzero(&serv2,sizeof(serv2));
    serv2.sin_family=AF_INET;   
    serv2.sin_addr.s_addr=inet_addr("10.21.100.153");// serv2 used to recv data for vlc display.
    serv2.sin_port=htons(PORT2);  

    void *buff = malloc(1500); // vlc data length is 1316
    while(1)
        int addr_len = sizeof(client);
        memset(buff,0,1500);
        int ret = recvfrom(fd1,buff,1500,0,(struct sockaddr *)&client,&addr_len); 
        int i,j;
        int zero = 0;
        for(i=0;i<100;i++)  //look at memory
            for(j=0;j<15;j++)
                printf("0x%x ",*((char *)(buff)+i*15+j));
                if(*((char *)(buff)+i*15+j) == 0)
                    zero++;
                
            
            puts("");
        
        printf("\\n zero: %d \\n",zero); // zero counts
        printf("%5d ",ret); //recv bits
        if(ret == -1)
           perror("recvfrom error: ");
           exit(1);
        
        else 
           //ret = sendto(fd1,buff,strlen(buff),0,(struct sockaddr *)&serv2,sizeof(serv2)); 
           printf("%5d\\n",strlen(buff)); //strlen calculate bits
           ret = sendto(fd1,buff,ret,0,(struct sockaddr *)&serv2,sizeof(serv2)); 
           sleep(1);
           if(ret == -1)
               perror("sendto error: ");
               exit(1);
            
        
    
    return 0;

log:

0x47 0x40 0x42 0x32 0xffffff90 0x0 0xffffffff 0xffffffff 0xffffffff 0xffffffff 0xffffffff 0xffffffff 0xffffffff 0xffffffff 0xffffffff 
0xffffffff 0xffffffff 0xffffffff 0xffffffff 0xffffffff 0xffffffff 0xffffffff 0xffffffff 0xffffffff 0xffffffff 0xffffffff 0xffffffff 0xffffffff 0xffffffff 0xffffffff 
...
...
...
0x7 0xffffffed 0x26 0x62 0xfffffff3 0x76 0xffffff8b 0x57 0xffffffab 0xffffffcf 0x5 0xffffffa7 0xffffffba 0xffffff80 0xffffffb2 
0x68 0xffffffa5 0xffffffb6 0x1b 0x5d 0x10 0xffffff83 0xffffffcc 0xffffffd5 0xffffffb6 0xffffffb8 0xffffff81 0xffffffa7 0x70 0xffffffe5 
0xffffff8b 0x2a 0x7c 0x33 0x51 0x59 0xffffffe4 0xffffffe6 0x32 0xffffff88 0x76 0xffffff94 0x48 0xffffff95 0xffffffde 
0x39 0xffffff94 0xffffffac 0x33 0x33 0x9 0x6b 0x6e 0xffffff9b 0x6f 0xfffffff3 0x0 0x0 0x0 0x0 
0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 
0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 
0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 
0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 
0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 
0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 
0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 
0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 
0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 
0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 
0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 
0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 

 zero: 229 
 1316     5 

因为接收到的数据不是连续填充的,(注意看第一行前六位),
0x47 0x40 0x42 0x32 0xffffff90 0x0
所以用strlen()得到的结果是远远小于recv的字节数的。如果使用sendto(fd1,buff,strlen(buff),0,(struct sockaddr *)&serv2,sizeof(serv2))转发的数据是很少的。

以上是关于h5客户端与服务器的socket怎样相连的主要内容,如果未能解决你的问题,请参考以下文章

Socket.Io与WebSockt

socket.io websocket

Netty的Socket编程详解-搭建服务端与客户端并进行数据传输

雷达 tcp ip socket c++

怎样使用Boost Asio建立非凡的客户端和服务端应用

java与oracle数据库相连,用的是哪个jar文件?