标签: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
c实现windows socket,布布扣,bubuko.com
标签:style blog class c code java
原文地址:http://www.cnblogs.com/as3lib/p/3744872.html