Hi All,

I am looking for a design approach to implement TCP socket server which is going to serve requests based on multiple ports.

(1) port1 : dedicated for video streaming
(2) port 2: dedicated for audio streaming
(3) port 3: dedicated for data streaming.

In above scenario do i need to create single server to take care with multiple port request or diff-2 server needs to be created for diff-2
request based on port configuration.

Is there any design strategy where connection pool of sockets can be created to serve above mentioned port based requests?

i appreciate if any samples of code to take care of above design will be shared.

Recommended Answers

All 8 Replies

As I read this I have the impression you are new to multiple core systems and how an app can spawn itself to respond to each port handler.

That is, all skill levels arrive here so in your app, just go ahead and code your system to handle it as you wish and then use the IP API calls to register your app to handle the requests. The OS might spawn your app and use another CPU or thread without you working hard.

In parting, I take it you need to revisit how OSes like Linux do this today. That is, I never code to replace what the host OS does for me.

Thanks for your reply. My question is whether 3 server needs to be created or one server can handle all three port requests. if we create one server i.e (video) then that server will be always busy to handle continous video streaming and only will be free if client request to stop the video streaming and then only can handle other requests.

I want all the data (video ,audio,Text) has to be served altogether.

Your design, your choice. But on modern OSes you can have the same app do all three and let the OS run 3 instances. I don't know how long you're been working with Linux and more but the concept of free makes me think you are a DOS thinker where we only had one thread or CPU.

That's OK, you'll catch up soon enough.

"But on modern OSes you can have the same app do all three and let the OS run 3 instances"

Could you through some light on this in order to create different sockets . i am trying with same using multithreading in linux but not sure how to incorporate video and audio stuff in below code by creating multiple threads. even i am not able to exit by typing exit in client terminal. i believe i need to create two more tasks i.e. task2-video and task3-Audio needs to be created. Please coorect me if i am worng.

   #include <string.h>
    #include <unistd.h>
    #include <stdio.h>
    #include <netdb.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <iostream>
    #include <fstream>
    #include <strings.h>
    #include <stdlib.h>
    #include <string>
    #include <pthread.h>
    using namespace std;

    void *task1(void *);

    static int connFd;

    int main(int argc, char* argv[])
    {
        int pId, portNo, listenFd;
        socklen_t len; //store size of the address
        bool loop = false;
        struct sockaddr_in svrAdd, clntAdd;

        pthread_t threadA[3];

        if (argc < 2)
        {
            cerr << "Syntam : ./server <port>" << endl;
            return 0;
        }

        portNo = atoi(argv[1]);

        if((portNo > 65535) || (portNo < 2000))
        {
            cerr << "Please enter a port number between 2000 - 65535" << endl;
            return 0;
        }

        //create socket
        listenFd = socket(AF_INET, SOCK_STREAM, 0);

        if(listenFd < 0)
        {
            cerr << "Cannot open socket" << endl;
            return 0;
        }

        bzero((char*) &svrAdd, sizeof(svrAdd));

        svrAdd.sin_family = AF_INET;
        svrAdd.sin_addr.s_addr = INADDR_ANY;
        svrAdd.sin_port = htons(portNo);

        //bind socket
        if(bind(listenFd, (struct sockaddr *)&svrAdd, sizeof(svrAdd)) < 0)
        {
            cerr << "Cannot bind" << endl;
            return 0;
        }

        listen(listenFd, 5);

        len = sizeof(clntAdd);

        int noThread = 0;

        while (noThread < 3)
        {
            cout << "Listening" << endl;

            //this is where client connects. svr will hang in this mode until client conn
            connFd = accept(listenFd, (struct sockaddr *)&clntAdd, &len);

            if (connFd < 0)
            {
                cerr << "Cannot accept connection" << endl;
                return 0;
            }
            else
            {
                cout << "Connection successful" << endl;
            }

            pthread_create(&threadA[noThread], NULL, task1, NULL); 

            noThread++;
        }

        for(int i = 0; i < 3; i++)
        {
            pthread_join(threadA[i], NULL);
        }

    }

    void *task1 (void *dummyPt)
    {
        cout << "Thread No: " << pthread_self() << endl;
        char test[300];
        bzero(test, 301);
        bool loop = false;
        while(!loop)
        {    
            bzero(test, 301);

            read(connFd, test, 300);

            string tester (test);
            cout << tester << endl;

            if(tester == "exit")
                break;
        }
        cout << "\nClosing thread and conn" << endl;
        close(connFd);
    }

Sorry but you get to code this. It's advanced and my mistake in omitting to ask why do this?

I have to ask since a single connection can pump 1080p video, 5.1 audio and text in less than a 5 megabit link. I can't imagine why you would create a socket for each today.

That out of the way, VLC is open source and ready to serve. http://www.videolan.org/vlc/streaming.html

Very little experience on streaming audio and video and that experience was largely unsuccessful, so this post comes from that perspective. That said, a few points...

  1. This is clearly Intro To Sockets skeleton code that you have posted. If you are new to sockets, I'd advise that you learn basic socket programming first, THEN move on to video and audio streaming. Add in multi-threading, multi-core, high bandwidth, etc., and it gets a lot more complex fast. You can't just scale "Hello World" socket code.
  2. The ".h" in your includes along with your bzero function instead of memset suggests this is rather old code. Audio/video streaming has changed radically in recent years, so you might need to update your learning source.
  3. There are a whole bunch of libraries out there. I don't see any of them included in your code. Make sure you aren't re-inventing the wheel. I certainly did, learning sound technology from the ground up, not understanding it, trying again, etc., etc., then finding a library that took one percent of the effort, one percent of the time, one percent of the knowledge, and worked a thousand times better than my pathetic code. Yes, I was able to write some functioning code pasted together with duct tape, but I don't get a medal for being able to do that from scratch where most couldn't. I get a dunce camp for doing it from scratch.
commented: +1 for Hello World scaling. +12

i am using opencv library for video streaming and it is working perfectly .

I have to ask since a single connection can pump 1080p video, 5.1 audio and text in less than a 5 megabit link. I can't imagine why you would create a socket for each today.

video and audio needs to be send altogether so we need three sockets as multiple client can connect to server and request the data.

Today, right now, video and audio is sent over a single socket without any work by our programmers. I can not give you code as it's owned by the company but I can reveal that one transport uses RTP. Read https://en.wikipedia.org/wiki/Real-time_Transport_Protocol

I would never ask our staff to code that from scratch.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.