UDP通信小程序
程序员文章站
2022-07-07 15:16:16
...
udpserver.cpp
#include<Winsock2.h>
#include<stdio.h>
#include<stdlib.h>
#include <WS2tcpip.h>
#pragma comment(lib, "Ws2_32.lib")
using namespace std;
//服务器端口号为5050
#define DEFAULT_PORT 5055
//接收数据缓冲区长度
#define BUFFER_LENGTH 1024
void main()
{
int iPort = DEFAULT_PORT;
WSADATA wsaData;
SOCKET sSocket;
//客户地址长度
int iLen;
//发送的数据长度
int iSend;
//接收的数据长度
int iRecv;
//要发送给客户的信息
char send_buf[] = "Hello! I am a server.";
//接收数据的缓冲区
char recv_buf[BUFFER_LENGTH];
//本地地址和客户地址
struct sockaddr_in ser,cli;
printf("-------------------------------\n");
printf("Server waiting\n");
printf("-------------------------------\n");
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
{
printf("Failed to load Winsock.\n ");
return;
}
//产生服务器端套接口
sSocket = socket(AF_INET, SOCK_DGRAM, 0);
if (sSocket == INVALID_SOCKET)
{
printf("socket( ) Failed: %d\n", WSAGetLastError());
return;
}
//以下建立服务器端地址
ser.sin_family = AF_INET;
//htons( )函数把一个双字节主机字节顺序的数转换为网络字节顺序的数
//ser.sin_port = htons(iPort);
ser.sin_port = htons(5055);
//htonl( )函数把一个主机字节顺序的数转换为网络字节顺序的数
ser.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(sSocket, (LPSOCKADDR)&ser, sizeof(ser)) == SOCKET_ERROR)
{
printf("bind( ) Failed: %d\n", WSAGetLastError());
return;
}
iLen = sizeof(cli);
//初始化接收缓冲区
memset(recv_buf, 0, sizeof(recv_buf));
//进入一个无限循环,进行数据接收和发送
while (1)
{
//从客户端接收数据
iRecv = recvfrom(sSocket, recv_buf, BUFFER_LENGTH, 0,(SOCKADDR*)&cli, &iLen);
if (iRecv == SOCKET_ERROR)
{
printf("recvfrom( ) Failed.:%d\n", WSAGetLastError());
break;
}
else if (iRecv == 0)
break;
else
{
char ip[20];
struct in_addr s;
s = cli.sin_addr;
inet_ntop(AF_INET, (void*)&s, ip, 16);
//输出接收到的数据
printf("recvfrom( ):%s\n", recv_buf);
printf("Accepted client IP:[%s],port:[%d]\n", ip, ntohs(cli.sin_port));
//输出客户IP地址和端口号
//printf("Accepted client IP:[%s],port:[%d]\n",inet_ntoa(cli.sin_addr),ntohs(cli.sin_port));
}
if (strcmp(recv_buf, "bye") == 0)
{
closesocket(sSocket);
break;
}
printf("Please input send data:");
char c = getchar();
int i = 0;
while (c != '\n')
{
send_buf[i] = c;
i++;
c = getchar();
}
send_buf[i] = '\0';
//给客户发送信息
iSend = sendto(sSocket, send_buf, sizeof(send_buf), 0,(SOCKADDR*)&cli, sizeof(cli));
if (iSend == SOCKET_ERROR)
{
printf("sendto( ) Failed.:%d\n", WSAGetLastError());
printf("-------------------------------\n");
break;
}
else if (iSend == 0)
break;
else
{
printf("sendto( ) succeeded!\n");
printf("-------------------------------\n");
}
}
printf("通信结束!");
//closesocket(sSocket);
WSACleanup();
system("pause");
}
udpClient.cpp
#include<Winsock2.h>
#include<stdio.h>
#include <WS2tcpip.h>
#pragma comment(lib, "Ws2_32.lib")
#pragma warning(disable:4996)
using namespace std;
//服务器端口号为5050
#define DEFAULT_PORT 5055
//缓冲区长度
#define DATA_BUFFER 1024
void main(int argc, char *argv[])
{
WSADATA wsaData;
SOCKET sClient;
int iPort = 5050;
//服务器地址长度
int iLen;
//接收数据的缓冲
int iSend;
int iRecv;
//要发送给服务器的信息
char send_buf[] = "Hello! I am a client.";
//接收数据的缓冲区
char recv_buf[DATA_BUFFER];
//服务器端地址
struct sockaddr_in ser;
//处理命令行中输入的参数
/*if (argc < 2)
{
//提示在命令行中输入服务器IP地址
printf("Usage:client [server IP address]\n");
return;
}*/
//接收数据的缓冲区初始化
memset(recv_buf, 0, sizeof(recv_buf));
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
{
printf("Failed to load Winsock.\n");
return;
}
//建立服务器端地址
ser.sin_family = AF_INET;
//ser.sin_port = htons(iPort);
ser.sin_port = htons(5055);
ser.sin_addr.s_addr = inet_addr("127.0.0.1");
//建立客户端数据报套接口
sClient = socket(AF_INET, SOCK_DGRAM, 0);
if (sClient == INVALID_SOCKET)
{
printf("socket( )Failed:%d\n", WSAGetLastError());
return;
}
iLen = sizeof(ser);
while (1)
{
//向服务器发送数据
iSend = sendto(sClient, send_buf, sizeof(send_buf), 0, (struct sockaddr*)&ser, iLen);
if (iSend == SOCKET_ERROR)
{
printf("sendto( )Failed:%d\n", WSAGetLastError());
return;
}
else if (iSend == 0)
return;
else
printf("snedto( ) succeeded.\n");
//从服务器接收数据
iRecv = recvfrom(sClient, recv_buf, sizeof(recv_buf), 0, (struct sockaddr*)&ser, &iLen);
if (iRecv == SOCKET_ERROR)
{
printf("recvfrom( )Failed.:%d\n", WSAGetLastError());
return;
}
else if (iRecv == 0)
return;
else
{
//显示从服务器收到的信息
printf("recvfrom( ):%s\n", recv_buf);
printf("-------------------------------\n");
}
if (strcmp(recv_buf, "bye") == 0)
{
closesocket(sClient);
break;
}
//给服务器发送数据
printf("Please input send data:");
char c = getchar();
int i = 0;
while (c != '\n')
{
send_buf[i] = c;
i++;
c = getchar();
}
send_buf[i] = '\0';
printf("The send data is:%s", send_buf);
}
printf("通信结束!");
//closesocket(sClient);
WSACleanup();
system("pause");
}
上一篇: 实现UDP通信的例子程序【linux】(zzzc)
下一篇: C# Socket 通信