C Implementing the Windows socket

Source: Internet
Author: User

Service-Side code:?
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;      }

  

Client code
/** testsocketclient.c * * Created on:2012-8-16 * Author: The Moon and the stars*/#include<winsock2. H>#include<stdio.h>//define constants used in the program#defineServer_address "127.0.0.1"//server-side IP address#definePORT 5150//port number of the server#defineMsgsize 1024//size of the send and receive buffers#pragmaComment (lib, "Ws2_32.lib")intMain () {wsadata wsadata; //connect the section words usedSOCKET sclient; //Save address information for remote serversockaddr_in server; //send and receive buffers    CharSzmessage[msgsize]; //number of bytes successfully received    intret; //Initialize Windows Socket LibraryWSAStartup (0x0202, &wsadata); //Create a client-side section wordSclient = socket (af_inet, sock_stream, ipproto_tcp);//Af_inet Indicates the use of the TCP/IP protocol family;//sock_stream, ipproto_tcp Specify the use of the TCP protocol//indicates the address information (port number, IP address, and so on) of the remote servermemset (&server,0,sizeof(sockaddr_in));//set the server that saved the address to full 0 firstserver.sin_family = pf_inet;//the declaration address format is the TCP/IP address formatServer.sin_port = htons (port);//indicates the port number of the connection server, htons () for converts values between the host and network byte orderSERVER.SIN_ADDR.S_ADDR = inet_addr (server_address);//indicates the IP address of the connecting server//the Sin_addr field of the structure sockaddr_in is used to save the IP address, and the Sin_addr field is also a struct, sin_addr.s_addr is used to finally save the IP address //inet_addr () converts the "127.0.0.1" string of a shape into an IP address format//connect to the server you just indicated.Connect (Sclient, (structSOCKADDR *) &server,sizeof(sockaddr_in));//This connection can be used with sclient after connection.//server has saved address information for the remote server     while(TRUE) {printf ("Send:"); //input from keyboardGets (szmessage);//The gets () functionreads characters from stdin and loads them into szmessage//Send DataSend (Sclient, Szmessage, strlen (szmessage),0);//sclient indicates which connection to send, Szmessage indicates the save address of the data to be sent, strlen (szmessage) indicates the data length    }                         //releasing connections and making ends workclosesocket (sclient);        WSACleanup (); return 0; }

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

Java Mina and C + + ACE do the socket Long Connection test report:

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

C + + RTMP server does streaming media

Stream Media Research blog: Http://www.cnblogs.com/haibindev

http://www.rtmpd.com/

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

http: First download FLV to the local cache via IIS, and then play the FLV via the Netconnection local connection, which is to play the local video, not the video of the playback server. So this flv can be found in the local cache. The advantage is that the server downloaded this flv, the server is not consumed, save the server consumption. The disadvantage is that FLV is cached on the client side, and the privacy of the FLV is bad.

rtmp mode: Connect to the FMS/RED5 server via Netconnection, and play the server's FLV file in real-time, this way can choose the video play point (SEEK ()), The advantage is that the FLV file cannot be found in the local cache, as it is not necessary to cache a full FLV file locally to select the playback point. Its advantage is that FLV will not be cached on the client, the privacy of FLV good, its disadvantage is to consume server resources, the connection is always real-time.

In a word, the HTTP mode is local playback, rtmp mode is the server real-time playback, depending on the need.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.