c實現windows socket

來源:互聯網
上載者:User

標籤:style   blog   class   c   code   java   

服務端代碼:?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 /*     * testSocketService.c     *     *  Created on: 2012-8-16     *      Author: 皓月繁星     */ #include <WINSOCK2.H> #include <stdio.h>                       #define PORT           5150  #define MSGSIZE        1024                     #pragma comment(lib, "ws2_32.lib")                       int main()    {        WSADATA wsaData;        SOCKET sListen;        SOCKET sClient;        SOCKADDR_IN local;        SOCKADDR_IN client;        char szMessage[MSGSIZE];        int ret;        int iaddrSize = sizeof(SOCKADDR_IN);        WSAStartup(0x0202, &wsaData);                           sListen = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);                           local.sin_family = AF_INET;        local.sin_port = htons(PORT);        local.sin_addr.s_addr = htonl(INADDR_ANY);        bind(sListen, (struct sockaddr *) &local, sizeof(SOCKADDR_IN));                           listen(sListen, 1);                           sClient = accept(sListen, (struct sockaddr *) &client, &iaddrSize);        printf("Accepted client:%s:%d\n", inet_ntoa(client.sin_addr),                ntohs(client.sin_port));                           while (TRUE) {            ret = recv(sClient, szMessage, MSGSIZE, 0);            szMessage[ret] = ‘\0‘;            printf("Received [%d bytes]: ‘%s‘\n", ret, szMessage);        }        return 0;      }

  

用戶端代碼
/*     * testSocketClient.c     *     *  Created on: 2012-8-16     *      Author: 皓月繁星     */#include <WINSOCK2.H> #include <stdio.h>                     //定義程式中使用的常量    #define SERVER_ADDRESS "127.0.0.1" //伺服器端IP地址    #define PORT           5150         //伺服器的連接埠號碼    #define MSGSIZE        1024         //收發緩衝區的大小    #pragma comment(lib, "ws2_32.lib")                     int main()    {        WSADATA wsaData;        //串連所用套節字        SOCKET sClient;        //儲存遠程伺服器的地址資訊        SOCKADDR_IN server;        //收發緩衝區        char szMessage[MSGSIZE];        //成功接收位元組的個數        int ret;                         // Initialize Windows socket library        WSAStartup(0x0202, &wsaData);                         // 建立用戶端套節字        sClient = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); //AF_INET指明使用TCP/IP協議族;                                                             //SOCK_STREAM, IPPROTO_TCP具體指明使用TCP協議        // 指明遠程伺服器的地址資訊(連接埠號碼、IP地址等)        memset(&server, 0, sizeof(SOCKADDR_IN)); //先將儲存地址的server置為全0        server.sin_family = PF_INET; //聲明地址格式是TCP/IP地址格式        server.sin_port = htons(PORT); //指明串連伺服器的連接埠號碼,htons()用於 converts values between the host and network byte order        server.sin_addr.s_addr = inet_addr(SERVER_ADDRESS); //指明串連伺服器的IP地址                                                            //結構SOCKADDR_IN的sin_addr欄位用於儲存IP地址,sin_addr欄位也是一個結構體,sin_addr.s_addr用於最終儲存IP地址                                                            //inet_addr()用於將 形如的"127.0.0.1"字串轉換為IP地址格式        //連到剛才指明的伺服器上        connect(sClient, (struct sockaddr *) &server, sizeof(SOCKADDR_IN)); //串連後可以用sClient來使用這個串連                                                                            //server儲存了遠程伺服器的地址資訊        while (TRUE) {            printf("Send:");            //從鍵盤輸入            gets(szMessage); //The gets() functionreads characters from stdin and loads them into szMessage            // 發送資料            send(sClient, szMessage, strlen(szMessage), 0); //sClient指明用哪個串連發送; szMessage指明待發送資料的儲存地址 ;strlen(szMessage)指明資料長度        }                         // 釋放串連和進行結束工作        closesocket(sClient);        WSACleanup();        return 0;    }

 http://www.docin.com/p-111227070.html

Java mina和c++ ace做socket長串連 測試報告:

http://www.iteye.com/problems/44682

c++ RTMP server 做流媒體的

 流媒體研究部落格:http://www.cnblogs.com/haibindev

http://www.rtmpd.com/

https://www.google.com.hk/#newwindow=1&q=rtmpc%2B%2B&safe=strict

 用HTTP方式:先通過IIS 將FLV下載到本機快取,然後再通過NetConnection的本地串連來播放這個FLV,這種方法是播放本地的視頻,並不是播放伺服器的視頻。因此在本機快取裡可以找到這個FLV。其優點就是伺服器下載完這個FLV,伺服器就沒有消耗了,節省伺服器消耗。其缺點就是FLV會緩衝在用戶端,對FLV的保密性不好。

用RTMP方式:通過NetConnection串連到FMS/Red5伺服器,並即時播放伺服器的FLV檔案,這種方式可以任意選擇視頻播放點(SEEK()),並不象HTTP方式需要緩衝完整個FLV檔案到本地才可以任意選擇播放點,其優點就是在本機快取裡是找不到這個FLV檔案的。其優點就是FLV不會緩衝在用戶端,FLV的保密性好,其缺點就是消耗伺服器資源,串連始終是即時的。

一句話,HTTP方式是本地播放,RTMP方式是伺服器即時播放,因需而定。 
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.