#pragma comment (lib,"ws2_32.lib")
#include <Winsock2.h>
#include <stdio.h>
static int base_port = 60123;
void main()
{
//版本协商
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD( 1, 1 );
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) {
/* Tell the user that we could not find a usable */
/* WinSock DLL. */
return;
}
/* Confirm that the WinSock DLL supports 2.2.*/
/* Note that if the DLL supports versions greater */
/* than 2.2 in addition to 2.2, it will still return */
/* 2.2 in wVersion since that is the version we */
/* requested. */
if ( LOBYTE( wsaData.wVersion ) != 1 ||
HIBYTE( wsaData.wVersion ) != 1 ) {
/* Tell the user that we could not find a usable */
/* WinSock DLL. */
WSACleanup( );
return;
}
/* The WinSock DLL is acceptable. Proceed. */
//创建服务器套接字
SOCKET Svr = socket(AF_INET,SOCK_DGRAM,0);
SOCKADDR_IN addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(base_port + 1);
addr.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
bind(Svr,(sockaddr*)&addr,sizeof(sockaddr));
//创建地址
SOCKADDR_IN addrSvr;
addrSvr.sin_family = AF_INET;
addrSvr.sin_port = htons(base_port);
addrSvr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
char recvBuf[128];
char sendBuf[128];
int len = sizeof(sockaddr);
while(true)
{
memset(sendBuf, 0x0, 128);
gets(sendBuf);
//发送数据
sendto(Svr,sendBuf,strlen(sendBuf)+1,0,(sockaddr*)&addrSvr,len);
//接收数据
recvfrom(Svr,recvBuf,128,0,(sockaddr*)&addrSvr,&len);
char* ipSvr = inet_ntoa(addrSvr.sin_addr);
printf("%s said: %s\n",ipSvr,recvBuf);
}
closesocket(Svr);
WSACleanup();
}
------------
#pragma comment (lib,"ws2_32.lib")
#include <Winsock2.h>
#include <stdio.h>
static int base_port = 60123;
SOCKET svr;
struct _addr_client_st_
{
int flag;
SOCKADDR_IN addr;
};
struct _addr_client_st_ addrClientArray[9] = {0};
DWORD WINAPI TaskLoopThread(PVOID pvParam)
{
DWORD dwResult = 0;
//版本协商
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD( 1, 1 );
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) {
/* Tell the user that we could not find a usable */
/* WinSock DLL. */
return dwResult;
}
/* Confirm that the WinSock DLL supports 2.2.*/
/* Note that if the DLL supports versions greater */
/* than 2.2 in addition to 2.2, it will still return */
/* 2.2 in wVersion since that is the version we */
/* requested. */
if ( LOBYTE( wsaData.wVersion ) != 1 ||
HIBYTE( wsaData.wVersion ) != 1) {
/* Tell the user that we could not find a usable */
/* WinSock DLL. */
WSACleanup( );
return dwResult;
}
/* The WinSock DLL is acceptable. Proceed. */
//创建数据报套接字
svr = socket(AF_INET,SOCK_DGRAM,0);
//创建本地地址信息
SOCKADDR_IN addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(base_port);
addr.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
int len = sizeof(sockaddr);
bind(svr,(sockaddr*)&addr,len);
//创建客户端地址对象
SOCKADDR_IN addrClient;
char recvBuf[128];
char tempBuf[256];
while(true)
{
//接收数据
recvfrom(svr,recvBuf,128,0,(sockaddr*)&addrClient,&len);
char* ipClient = inet_ntoa(addrClient.sin_addr);
int client_port = ntohs(addrClient.sin_port);
sprintf(tempBuf,"%s(%d) said: %s\n",ipClient,client_port, recvBuf);
printf("%s",tempBuf);
if (addrClientArray[client_port - base_port - 1].flag == 0) {
memcpy(&(addrClientArray[client_port - base_port - 1].addr), &addrClient, len);
}
}
closesocket(svr);
WSACleanup();
return dwResult;
}
void main()
{
CreateThread(
NULL, // 指定安全的结构指针,一般可以NULL,bInheritHandle成员指定句柄是否可以继承
0, // 指定线程堆栈空间的大小
TaskLoopThread, // 指定线程入口点函数地址
NULL, // 传递给线程函数的参数
0, // 一般为0,立即执行线程函数
0); // 指定线程ID,一般就0
char sendBuf[128] = {0};
int port = 0;
int len = sizeof(sockaddr);
while(1)
{
printf("port:");
gets(sendBuf);
port = atoi(sendBuf);
memset(sendBuf, 0x0, 128);
printf("msg:");
gets(sendBuf);
sendto(svr,sendBuf,strlen(sendBuf)+1,0,(sockaddr*)&(addrClientArray[port].addr),len);
}
return;
}
2187

被折叠的 条评论
为什么被折叠?



