Commit 6a6f3ac4 by Zeng Rixuan

UDP通讯例程

parent a462a62a
Build
/Debug/*
*.log
/release/*
/Release/*
/.vs/*
/x64/*
/x86/*
doc/rtf/
doc/latex/
doc/html/
cmake_minimum_required(VERSION 3.0.0)
project(helloword VERSION 0.1.0)
#reset output path
set ( EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/bin )
set ( LIBRARY_OUTPUT_PATH ${CMAKE_BINARY_DIR}/lib )
include(CTest)
enable_testing()
#set include file
include_directories (./inc)
#set source file
aux_source_directory ( . DIR_USR_SOURCE )
aux_source_directory ( ./src DIR_USR_SOURCE )
# find_library(WSOCK32_LIBRARY wsock32)
# find_library(WS2_32_LIBRARY ws2_32)
# add_executable(helloword main.cpp)
add_executable ( ${CMAKE_PROJECT_NAME} ${DIR_USR_SOURCE} )
if(WIN32)
target_link_libraries(${CMAKE_PROJECT_NAME} wsock32 ws2_32)
endif()
set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(CPack)
#include <winsock2.h>
#include <stdio.h>
#include <tchar.h>
#include <iostream>
#pragma comment(lib,"ws2_32.lib")
#define PORT 8050 //自己的端口
int _tmain(int argc, _TCHAR* argv[])
{
//UDP客户端
WSADATA wsaData={0};
int iResult = WSAStartup(0x0202,&wsaData);
SOCKET sock = socket(AF_INET,SOCK_DGRAM,0);//UDP协议类型
SOCKADDR_IN sa={AF_INET,htons(PORT)};
int n = bind(sock,(sockaddr*)&sa,sizeof(sa));//绑定端口,可以不填端口
SOCKADDR_IN to={AF_INET};
to.sin_port = htons(9000); //对方端口
to.sin_addr.S_un.S_addr = inet_addr("127.0.0.1"); //对方IP地址
int a = sendto(sock,"hello",5,0,(sockaddr*)&to,sizeof(to));
}
Build
/Debug/*
*.log
/release/*
/Release/*
/.vs/*
/x64/*
/x86/*
doc/rtf/
doc/latex/
doc/html/
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"compilerPath": "D:\\software\\TDM-GCC\\TDM-GCC\\bin\\gcc.exe",
"cStandard": "gnu11",
"cppStandard": "gnu++98",
"intelliSenseMode": "windows-gcc-x64",
"configurationProvider": "ms-vscode.cmake-tools"
}
],
"version": 4
}
\ No newline at end of file
cmake_minimum_required(VERSION 3.0.0)
project(helloword VERSION 0.1.0)
#reset output path
set ( EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/bin )
set ( LIBRARY_OUTPUT_PATH ${CMAKE_BINARY_DIR}/lib )
include(CTest)
enable_testing()
#set include file
include_directories (./inc)
#set source file
aux_source_directory ( . DIR_USR_SOURCE )
aux_source_directory ( ./src DIR_USR_SOURCE )
# find_library(WSOCK32_LIBRARY wsock32)
# find_library(WS2_32_LIBRARY ws2_32)
# add_executable(helloword main.cpp)
add_executable ( ${CMAKE_PROJECT_NAME} ${DIR_USR_SOURCE} )
if(WIN32)
target_link_libraries(${CMAKE_PROJECT_NAME} wsock32 ws2_32)
endif()
set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(CPack)
//#include "stdafx.h"
#include <stdio.h>
#include <tchar.h>
#include <winsock2.h>
#include <iostream>
#pragma comment(lib,"ws2_32.lib")
#define PORT 9000 //对外服务端口
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
WSADATA wsaData={0};
int iResutl = WSAStartup(0x0202,&wsaData);
SOCKET sock = socket(AF_INET,SOCK_DGRAM,0);
if(sock == INVALID_SOCKET)
{
cout <<"创建套接字失败!" <<endl;
}
sockaddr_in sa ={AF_INET};
sa.sin_port = htons(PORT);//开放给外部的端口
sa.sin_addr.s_addr = 0; //本机地址
int n = bind(sock,(sockaddr*)&sa,sizeof(sa));
if(SOCKET_ERROR == n)
{
cout << "端口绑定失败!"<<endl;
}
cout << "UDP服务器启动" <<endl;
char buff[1000]={0};
sockaddr_in form;
int len = sizeof(form);
int ret = 0;
while(ret =recvfrom(sock,buff,1000,0,(sockaddr*)&form,&len))
{
cout << "IP地址来自:" << inet_ntoa(form.sin_addr) <<"端口:"<<
htons(form.sin_port) <<endl;
cout << buff << endl;
memset(buff,0,1000);
}
return 0;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment