繁体   English   中英

如何使用客户端、服务器-客户端、套接字编码、使用 C 显示相机馈送? (Linux)

[英]How to display camera feed using client, server-client, socket coding, using C? (Linux)

我是 Linux 和摄像头插座编码的新手,英语不是我的母语。 但我会尽力解释。

我正在使用借来的 Axis 相机。 我正在使用 Linux。 我正在尝试使用 sockets 和 TCP。

我有一个 serverside.c 文件和一个 client.c 文件。

serverside.c 应该将从相机(以及图像大小和 FPS)捕获的图像发送到客户端。 它作为数组中的字节执行。

The client.c is supposed to recieve this data and output it to the user, in a window, as a camera stream. 使用客户端,用户最终还可以调整图像大小和 FPS。

现在解决眼前的问题:我试图找到如何让客户端显示图像提要的答案,但到目前为止没有运气。 有没有人有关于如何做到这一点的任何提示、指示或操作方法?

服务器端.c

#define _GNU_SOURCE -D
#include <in.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/time.h>
#include <syslog.h>
#include <capture.h>
#include <sys/sendfile.h>
#include <pthread.h>
//define where the network camera is located
#define PORT_NUMBER 1025
#define SERVER_ADDRESS "localhost"

void *msg_handler(void *);

//step 1
/* The server is supposed to send a list of available resolutions and frames per second, so the client can choose*/

//we create a handler to each client

void *client_handler(int server_socket)

{
    int serverSocket = *(int *)server_socket; // this is a pointer to the server socket
    char *message, cli_message[2000];
    message = capture_get_resolutions_list(0);     //list of available image resolutions;
    write(serverSocket, message, strlen(message)); //send to client the list of resolutions
    write(serverSocket, "\n", strlen("\n"));       //breaking line so the client can see the list
    memset(message, 0, strlen(message));           //resetting the message variable

    //variables related to the image
    media_stream *imageStream;

    recv(serverSocket, cli_message, 2000, 0); // receive information from client regarding resolution
    media_frame *imageFrame;
    void *data;
    size_t imageSize;
    int arrayRow = 0; //related to the data array - we need it if we want to loop later through the array

    //opening the stream in order to capture image
    imageStream = capture_open_stream(IMAGE_JPEG, cli_message);

    //send image to client with chosen resolution
    while (1) //condition always true, i.e. infinite loop
    {
        imageFrame = capture_get_frame(imageStream);
        data = capture_frame_data(imageFrame);

        imageSize = capture_frame_size(imageFrame);
        sprintf(message, "%zu\n", imageSize);          //converting the size to a char , and send to client
        write(serverSocket, message, strlen(message)); //sending the size to the client

        unsigned char arrayRow_data[imageSize];

        for (arrayRow = 0; arrayRow < imageSize; arrayRow++)
        {

            arrayRow_data[arrayRow] = ((unsigned char *)data)[arrayRow];
        }

        //try to send data to client ---> see the possible errors
        int sendDataError = write(serverSocket, arrayRow_data, sizeof(arrayRow_data));

        if (sendDataError < 0)
        {
            syslog(LOG_INFO, "oops, connection with client lost");
            break;
        }

        //restoring variables to ZERO
        memset(data, 0, sizeof(data));
        memset(arrayRow_data, 0, sizeof(arrayRow_data));

        capture_frame_free(imageFrame);
    }
    syslog(LOG_INFO, "ERROR:CAN NOT SEND IMAGE");
    capture_close_stream(imageStream);
    close(serverSocket);
    return 0;
}

//main function

//creating a socket
int main(void)
{
    //char server_message[256]= "Congrats, you have reached the server";
    int server_socket;
    int client_socket;
    int *new_socket;
    int conn;
    struct sockaddr_in server, client;

    server_socket = socket(AF_INET, SOCK_STREAM, 0);

    server.sin_family = AF_INET;
    server.sin_addr.s_addr = INADDR_ANY;
    server.sin_port = htons(1025);

    //bind the socket to the specified IP and port
    bind(server_socket, (struct sockaddr *)&server, sizeof(server));
    conn = sizeof(struct sockaddr_in);
    listen(server_socket, 5);

    //accept the connection from the client and send the message

    while ((client_socket = accept(server_socket, (struct sockaddr *)&client, (socklen_t *)&conn)))
    {

        pthread_t client_thread;
        new_socket = malloc(sizeof *new_socket);
        new_socket = client_socket;

        //establishing a message thread and possible errors
        if (pthread_create(&client_thread, NULL, client_handler, (void *)new_socket) < 0)
        {
            syslog(LOG_INFO, "ERROR. MESSAGE THREAD NOT ESTABLISHED");
            return 1;
        }
    }

    if (client_socket < 0)
    {
        syslog(LOG_INFO, "Sorry, client socket declined");
        free(new_socket);
        return 1;
    }

    return 0;
}

客户端.c

#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <arpa/inet.h>

//The port this client will connect to:
#define PORT 1025

//Set MAX number of bytes the client can recieve
//at a time.
#define MAXDATASIZE 2000

//Main method
int main (int argc, char *argv[])
{
    //Variables
    int sockfd, numbytes;
    char buf[MAXDATASIZE];
    struct hostent *he;
    //Connector's address information
    struct sockaddr_in their_addr;

    //Get the HOST-info
    if((he = gethostbyname(argv[1])) == NULL)
    {
        //No host? ERROR.
        perror("gethostbyname()");
        exit(1);
    }
    else
    {
        //Print the HOST-data.
        printf("Client-The remote host is: %s\n", argv[1]);
    }

    //Check for socket
    if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
    {
        //Socket error?
        perror("socket()");
        exit(1);
    }
    else
    {
        //Print socket-is-OK status.
        printf("Client-The socket() sockfd is OK...\n");
    }

    //Host byte order
    their_addr.sin_family = AF_INET;

    //Short, network byte order
    printf("Server-Using %s and port %d...\n", argv[1], PORT);
    their_addr.sin_port = htons(PORT);
    their_addr.sin_addr = *((struct in_addr*)he->h_addr);

    //Zero the rest of the struct
    memset(&(their_addr.sin_zero), '\0', 8);

    //Is everything still OK with the connection...?
    if(connect(sockfd, (struct sockaddr *)&their_addr, sizeof(struct sockaddr)) == -1)
    {
        //Connection error!
        perror("connect()");
        exit(1);
    }
    else
    {
        printf("Client-The connect() is OK... \n");
    }

    //Data recieved?
    if((numbytes = recv(sockfd, buf, MAXDATASIZE - 1, 0)) == -1)
    {
        //Error recieving
        perror("recv()");
        exit(1);
    }
    else
    {
        //Data recieved OK!
        printf("Client-The recv() is OK... \n");
    }

    //Deal with recieved data, print recieved data.
    buf[numbytes] = '\0';
    printf("Client-Recieved: %s", buf);

    printf("Client-No more data. Closing sockfd.\n");
    close(sockfd);
    return 0;
}

谢谢您的帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM