服务器端
#include <windows.h>
#include <iostream.h>
#include <winsock.h>
#pragma comment(lib,"ws2_32")#define NO_FLAGS_SET 0#define PORT (u_short) 44965
#define MAXBUFLEN 256INT main(VOID)
{WSADATA Data;//它存储 被WSAStartup函数调用后返回的 Windows Sockets数据SOCKADDR_IN serverSockAddr;//服务器地址,他区分地址和端口号SOCKADDR_IN clientSockAddr;//客户端地址SOCKET serverSocket;//服务器套接字SOCKET clientSocket;//客户端套接字int addrLen=sizeof(SOCKADDR_IN);//获得地址长度int status; //获取WSAStartup返回的整数来判断当前各种状态int numrcv; //获取接受数据过程是否有异常。char buffer[MAXBUFLEN];//缓冲区/* initialize the Windows Socket DLL */status=WSAStartup(MAKEWORD(1, 1), &Data);//调用库函数并返回结果
/*初始化Winsock DLLif (status != 0)cerr << "ERROR: WSAStartup unsuccessful" << endl;//失败/* zero the sockaddr_in structure *///memset(&serverSockAddr, 0,sizeof(serverSockAddr));/* specify the port portion of the address */serverSockAddr.sin_port=htons(PORT);//将端口从整形转化成网络字节序并赋值给服务器端//口/* specify the address family as Internet */serverSockAddr.sin_family=AF_INET;//表明协议类型是TCP/ip协议serverSockAddr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");/* specify that the address does not matter */
/*INADDR_ANY 的具体含义是,绑定到0.0.0.0。此时,对所有的地址都将是有效的
serverSockAddr.sin_addr.s_addr=htonl(INADDR_ANY);/* create a socket */serverSocket=socket(AF_INET, SOCK_DGRAM, 0);//创建服务器套接字if (serverSocket == INVALID_SOCKET)cerr << "ERROR: socket unsuccessful" << endl;int len = sizeof(serverSockAddr); //获取服务器地址长度/* associate the socket with the address */bind(serverSocket, (LPSOCKADDR) &serverSockAddr,len);//将套接字与IP地址绑定/*if (status == SOCKET_ERROR)cerr << "ERROR: bind unsuccessful" << endl; //绑定失败/* allow the socket to take connections *//*status=listen(serverSocket, 1);if (status == SOCKET_ERROR)cerr << "ERROR: listen unsuccessful" << endl; //监听失败/* accept the connection request when one is received *//*clientSocket=accept(serverSocket,(LPSOCKADDR) &clientSockAddr,&addrLen);*///cout << "Got the connection..." << endl;while(1){
numrcv=recvfrom(serverSocket, buffer, MAXBUFLEN, 0,(SOCKADDR*)&clientSockAddr,&len);
//接收从客户端发送过来的数据并返回结果。if ((numrcv == 0) || (numrcv == SOCKET_ERROR)){cout << "Connection terminated." << endl;status=closesocket(clientSocket);if (status == SOCKET_ERROR)cerr << "ERROR: closesocket unsuccessful"<< endl;status=WSACleanup();if (status == SOCKET_ERROR)cerr << "ERROR: WSACleanup unsuccessful" << endl;return(1); }cout << buffer << endl;} /* while */
}
客户端如下:
#include <windows.h>
#include <iostream.h>
#include <winsock.h>
#pragma comment(lib,"ws2_32")#define NO_FLAGS_SET 0#define PORT (u_short) 44965
#define DEST_IP_ADDR "10.86.2.1" //Server addressINT main(VOID)
{WSADATA Data;//它存储 被WSAStartup函数调用后返回的 Windows Sockets数据SOCKADDR_IN destSockAddr;//目的套接字地址,他区分地址和端口号SOCKET client;//客户端套接字int status;//获取WSAStartup返回的整数来判断当前各种状态int numsnt; //获取发送数据过程是否有异常。char *toSendtxt="Test String";/* initialize the Windows Socket DLL */status=WSAStartup(MAKEWORD(1, 1), &Data);//调用外部库函数并返回结果/*if (status != 0)cerr << "ERROR: WSAStartup unsuccessful"//调用失败<< endl;/* convert IP address into in_addr form *///destAddr=inet_addr(DEST_IP_ADDR);/* copy destAddr into sockaddr_in structure *//*memcpy(&destSockAddr.sin_addr,&destAddr, sizeof(destAddr));/* specify the port portion of the address */destSockAddr.sin_port=htons(PORT); //将端口从整形转化成网络字节序并赋值给目的套接//字端/* specify the address family as Internet */destSockAddr.sin_family=AF_INET;//表明协议类型是TCP/IP协议destSockAddr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");//设置服务器端的IP地址int len = sizeof(SOCKADDR_IN);/* create a socket */client=socket(AF_INET, SOCK_DGRAM, 0);//创建一个客户端套接字/*if (destSocket == INVALID_SOCKET){cerr << "ERROR: socket unsuccessful" << endl;//创建失败status=WSACleanup();if (status == SOCKET_ERROR)cerr << "ERROR: WSACleanup unsuccessful" << endl;return(1);}/*cout << "Trying to connect to IP Address: "<< DEST_IP_ADDR << endl;/* connect to the server */while(1){cout << "Sending..." << endl; //控制台输出Sending...numsnt=sendto(client, toSendtxt, strlen(toSendtxt) + 1, NO_FLAGS_SET,(const SOCKADDR*)&destSockAddr,len);//发送数据并获取结果,各参数上面有说明if (numsnt != (int)strlen(toSendtxt) + 1){cout << "Connection terminated" << endl;//连接中断status=closesocket(client); //关闭套接字if (status == SOCKET_ERROR)cerr << "ERROR: closesocket unsuccessful"//关闭失败<< endl;status=WSACleanup(); //终止调用外部库if (status == SOCKET_ERROR)cerr << "ERROR: WSACleanup unsuccessful"//终止失败<< endl;return(1);}/* Wait before sending the message again */Sleep(4800); //线程睡眠4.8秒 } /* while */
}
小程序制作过程中遇到的问题
做C++socket UDP编程,server端接收的数据不完整,中间参杂十六进制的CDCDCDCD,不知道为何?
解决:
虽然发送的buffer全是正确的,发送数据的长度也是正确的。接数据的长度不正确,接收的数据不完整,应该是原来数组没有初始化,到时接收数据不完整时,接收数组中就出现了CDCDCDCD。之后我将数组进行初始化赋值一下,问题得到解决。