大多數的事倍功半源於自以為是事半功倍的一念之差。查了兩天UDP傳輸代碼,終於從一個博客的一行小字發現了問題的所在。之前為了方便直接運用Linux下寫好的客戶端代碼稍作修改之後去跟linux的服務器對接,造成有的機器收的到數據包有的機器卻收不到數據包,或是有時收的到數據包有時收不到數據包的狀況。可見不同操作系統之間的移植哪怕是同種語言也並不一定是表面看的那麼簡單,再者生搬硬套的做法往往得不償失。下面貼出在網上找到的關於正確使用UDP廣播的代碼,由於多播和廣播相差無幾,是故以後會把項目的多播代碼貼上,以補全題目內容。
相比TCP,UDP的優點是傳輸速度快且能對整個網絡廣播數據。但我以前在使用UDP整個網絡廣播數據的用法上,一直沒能好好使用。這段時間,正好需要使用UDP的這個功能,因此經過摸索,得出了一種使用方法如下:(不一定是最好的,但能實現我的功能)
Python實現UDP通信 http://www.linuxidc.com/Linux/2014-02/97159.htm
Unix-domain-socket詳解UDP http://www.linuxidc.com/Linux/2014-02/96981.htm
Linux網絡編程之使用UDP傳輸文件 http://www.linuxidc.com/Linux/2013-06/85843.htm
Java 使用 TCP 和 UDP 傳輸文件 http://www.linuxidc.com/Linux/2013-06/85756.htm
Java 獲取可用 UDP 端口號的方法 http://www.linuxidc.com/Linux/2013-04/82086.htm
Linux程序設計之套接字:UDP http://www.linuxidc.com/Linux/2013-03/81306.htm
// 服務器端
// Server.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <WinSock2.h>
#include <stdio.h>
#include <iostream>
using namespace std;
#pragma comment(lib, "ws2_32.lib")
const int MAX_BUF_LEN = 255;
int _tmain(int argc, _TCHAR* argv[])
{
WORD wVersionRequested;
WSADATA wsaData;
int err;
// 啟動socket api
wVersionRequested = MAKEWORD( 2, 2 );
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 )
{
return -1;
}
if ( LOBYTE( wsaData.wVersion ) != 2 ||
HIBYTE( wsaData.wVersion ) != 2 )
{
WSACleanup( );
return -1;
}
// 創建socket
SOCKET connect_socket;
connect_socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if(INVALID_SOCKET == connect_socket)
{
err = WSAGetLastError();
printf("/"socket/" error! error code is %d/n", err);
return -1;
}
SOCKADDR_IN sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(3779);
sin.sin_addr.s_addr = INADDR_BROADCAST;
bool bOpt = true;
//設置該套接字為廣播類型
setsockopt(connect_socket, SOL_SOCKET, SO_BROADCAST, (char*)&bOpt, sizeof(bOpt));
int nAddrLen = sizeof(SOCKADDR);
char buff[MAX_BUF_LEN] = "";
int nLoop = 0;
while(1)
{
nLoop++;
sprintf(buff, "%8d", nLoop);
// 發送數據
int nSendSize = sendto(connect_socket, buff, strlen(buff), 0, (SOCKADDR*)&sin, nAddrLen);
if(SOCKET_ERROR == nSendSize)
{
err = WSAGetLastError();
printf("/"sendto/" error!, error code is %d/n", err);
return -1;
}
printf("Send: %s/n", buff);
Sleep(500);
}
return 0;
}
// 客戶端
// Client.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <WinSock2.h>
#include <stdio.h>
#pragma comment(lib, "ws2_32.lib")
const int MAX_BUF_LEN = 255;
int _tmain(int argc, _TCHAR* argv[])
{
WORD wVersionRequested;
WSADATA wsaData;
int err;
// 啟動socket api
wVersionRequested = MAKEWORD( 2, 2 );
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 )
{
return -1;
}
if ( LOBYTE( wsaData.wVersion ) != 2 || HIBYTE( wsaData.wVersion ) != 2 )
{
WSACleanup( );
return -1;
}
// 創建socket
SOCKET connect_socket;
connect_socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if(INVALID_SOCKET == connect_socket)
{
err = WSAGetLastError();
printf("/"socket/" error! error code is %d/n", err);
return -1;
}
// 用來綁定套接字
SOCKADDR_IN sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(3779);
sin.sin_addr.s_addr = 0;
// 用來從網絡上的廣播地址接收數據
SOCKADDR_IN sin_from;
sin_from.sin_family = AF_INET;
sin_from.sin_port = htons(3779);
sin_from.sin_addr.s_addr = INADDR_BROADCAST;
//設置該套接字為廣播類型,
bool bOpt = true;
setsockopt(connect_socket, SOL_SOCKET, SO_BROADCAST, (char*)&bOpt, sizeof(bOpt));
// 綁定套接字
err = bind(connect_socket, (SOCKADDR*)&sin, sizeof(SOCKADDR));
if(SOCKET_ERROR == err)
{
err = WSAGetLastError();
printf("/"bind/" error! error code is %d/n", err);
return -1;
}
int nAddrLen = sizeof(SOCKADDR);
char buff[MAX_BUF_LEN] = "";
int nLoop = 0;
while(1)
{
// 接收數據
int nSendSize = recvfrom(connect_socket, buff, MAX_BUF_LEN, 0, (SOCKADDR*)&sin_from, &nAddrLen);
if(SOCKET_ERROR == nSendSize)
{
err = WSAGetLastError();
printf("/"recvfrom/" error! error code is %d/n", err);
return -1;
}
buff[nSendSize] = '/0';
printf("Recv: %s/n", buff);
}
return 0;
}