使用SOCKET TCP

  刚刚接触SOCKET编程,网上看了一些资料,发现做些简单的应用还是不难。但是要深入了解SOCKET编程还需要系统的看一些书。一般在进程间通信TCP是一种不错的方式。 ---XXX

TCP链接是面向流的链接,因此数据在应用程序看来应该是没有边界的,之前对send和recv函数中发送的大小和返回值没有理解,今天做了一个实验有了进一步的了解。socket一些常用的函数介绍可以在这里找到http://blog.csdn.net/ithzhang/article/details/8448655

之前有个疑问是如果一方向另一方一次发送大量的数据,超过了TCP缓冲区的大小,那么接收方该如何处理。这里就要用到recv函数的返回值了,它指示了本次调用函数实际接收的字节数,如果这个值小于我们需要接收的数据,那么就需要再一次调用recv函数。

下面是我测试的代码,代码是在VS2013下编译的。

欢迎一起讨论,指正错误。

服务器端:

 #include <winsock2.h>
#include <iostream>
#include <numeric>
#include <vector>
#include <algorithm>
#pragma comment(lib,"ws2_32.lib") using namespace std; const int port = ; int main(int argc,char**argv)
{
WSADATA ws;
if (WSAStartup(MAKEWORD(, ), &ws) != )
{
cout << "init windows socket failed::" << GetLastError() << endl;
return -;
} SOCKET serverSocket; serverSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); sockaddr_in serverAddr;
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(port);
//serverAddr.sin_addr.s_addr = inet_addr("192.168.182.1");
serverAddr.sin_addr.s_addr = htonl(INADDR_ANY);
memset(&(serverAddr.sin_zero), , sizeof(serverAddr.sin_zero)); if (bind(serverSocket, (SOCKADDR*)&serverAddr, sizeof(serverAddr))!=)
{
cout << "bind server socket failed\n";
WSACleanup();
return -;
} if (listen(serverSocket,)!=)
{
cout << "listen failded\n";
return -;
} const int buf_size = * ; float *buf = new float[buf_size]; for_each(buf, buf + buf_size, [](float &v){v = ; });
cout << std::accumulate(buf, buf + buf_size, ) << endl;
while (true)
{
int add_size = sizeof(SOCKADDR); sockaddr_in client_addr;
SOCKET accept_sock = accept(serverSocket, (SOCKADDR*)&client_addr, &add_size);
if (accept_sock == -)
{
cout << "accept faild\n";
continue;
} int send_size = send(accept_sock, (char*)buf, buf_size*sizeof(float), );
cout << send_size << endl;
if (send_size==-)
{
cout << "send data faild\n";
continue;
} } delete[] buf;
return ;
}

客户端的:

 #include <iostream>
#include <numeric>
#include <winsock2.h>
#include <algorithm> #pragma comment(lib,"ws2_32.lib")
using namespace std;
const int port = ;
int main()
{
WSADATA ws; if (WSAStartup(MAKEWORD(, ), &ws) != )
{
cout << "load socket faild\n";
WSACleanup();
return -;
} SOCKET client_sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); sockaddr_in server_addr;
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(port);
server_addr.sin_addr.s_addr = inet_addr("192.168.182.1");
memset(&(server_addr.sin_zero), , sizeof(server_addr.sin_zero)); if (connect(client_sock, (SOCKADDR*)&server_addr, sizeof(SOCKADDR)) != )
{
cout << "connect to server failed \n";
WSACleanup();
return -;
}
const int buf_size = * ; float *buf = new float[buf_size] ;
for_each(buf, buf + buf_size, [](float &v){v = ; });
cout << std::accumulate(buf, buf + buf_size, ) << endl; int recved_size = ; do
{
int rsz = recv(client_sock, (char*)buf + recved_size, buf_size*sizeof(float)-recved_size, );
if (rsz == -)
{
cout << "recv data failed\n";
WSACleanup();
}
recved_size += rsz; } while (recved_size<buf_size*sizeof(float)); cout << std::accumulate(buf, buf + buf_size, ) << endl;
delete[] buf; closesocket(client_sock);
}
上一篇:设计点滴&css效果点滴


下一篇:MTK6572横屏的调试过程