【C/C++套接字编程】套接字的基本概念与基础语法_Mr_Fmnwon的博客-CSDN博客
目录
前言
UDP_server.cpp
UDP_client.cpp
总结
本篇着重于基于socket编程,实现基本的UDP通信。
//UDP_server.cpp
#include
#include
#include
#include
#pragma warning(disable : 4996)
#pragma comment(lib, "ws2_32.lib")
#define USER_ERROR -1
using namespace std;
void space2_(char *str);
void print_time();
void print_time(in_addr addr);
int main()
{
char recvData[200];
char sendData[200];
WSADATA wsaData;
// 打开网络库/启动网络库,启动了这个库,这个库里的函数/功能才能使用
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
{
printf("Failed to load Winsock.\\n");
return USER_ERROR;
}
SOCKET socket_of_server;
//part1 创建socket
socket_of_server = socket(AF_INET, SOCK_DGRAM, 0);
//part1 end
// 确保socker创建成功
if (socket_of_server == INVALID_SOCKET)
{
printf("socket() Failed:%d\\n", WSAGetLastError());
return USER_ERROR;
}
//part2 填写套接字信息
struct sockaddr_in server_in;
server_in.sin_family = AF_INET;
server_in.sin_port = htons(20000);
server_in.sin_addr.S_un.S_addr =inet_addr("10.129.46.156");
//part2 end
//part3 将服务器的socket和服务器的ip地址和端口号绑定
if (bind(socket_of_server, (struct sockaddr *)&server_in, sizeof(server_in)) == SOCKET_ERROR)
{
printf("blind() Failed:%d\\n", WSAGetLastError());
return USER_ERROR;
}
struct sockaddr_in dest_addr;
int len = sizeof(struct sockaddr_in);
printf("——————————聊天室已经启动——————————\\n");
while (1)
{
memset(recvData,'\\0',sizeof(recvData));
memset(sendData,'\\0',sizeof(sendData));
recvfrom(socket_of_server,recvData,200,0,(struct sockaddr *)&dest_addr,&len);
print_time(dest_addr.sin_addr);
printf("收到消息:%s\\n",recvData);
printf("输入要传输的信息:");
gets(sendData);
printf("\\033[1A"); //先回到上一行
printf("\\033[K"); //清除该行
print_time();
printf("发送消息:%s\\n",sendData);
sendto(socket_of_server,sendData,strlen(sendData),0,(struct sockaddr *)&dest_addr,len);
}
closesocket(socket_of_server);
WSACleanup();
return 0;
}
void space2_(char *str)
{
for(unsigned int i=0; i
//UDP_client.cpp
#include
#include
#include
#include
#include
#include
#pragma comment(lib, "ws2_32.lib")
#pragma warning(disable : 4996)
#define USER_ERROR -1
void print_time_from(in_addr addr);
void print_time_to(in_addr addr);
void print_time();
void print_time(in_addr addr);
int main()
{
// time_t now;
// char* curr_time = time(&now);
char recvData[200];
char sendData[200];
//启动套接字编程,不启动无法运行相关API
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
{
printf("Failed to load Winsock.\\n");
return USER_ERROR;
}
//part1 创建套接字
SOCKET socket_client = socket(AF_INET, SOCK_DGRAM, 0);
//end part1
if (socket_client == INVALID_SOCKET)
{
printf(" Failed socket() \\n");
return 0;
}
//part2 设置套接字信息 这里是需要
struct sockaddr_in client_in;
client_in.sin_port =htons(20001);
client_in.sin_addr.S_un.S_addr = inet_addr("10.128.18.146");
client_in.sin_family =AF_INET;
//part2 end
//part3 将服务器的socket和服务器的ip地址和端口号绑定
if (bind(socket_client, (struct sockaddr *)&client_in, sizeof(client_in)) == SOCKET_ERROR)
{
printf("blind() Failed:%d\\n", WSAGetLastError());
return USER_ERROR;
}
struct sockaddr_in dest_addr;
dest_addr.sin_family = AF_INET;
dest_addr.sin_port = htons(20000);
dest_addr.sin_addr.s_addr = inet_addr("10.129.46.156");
int len = sizeof(dest_addr);
printf("——————————聊天室已经启动——————————\\n");
while(1)
{
memset(recvData,'\\0',sizeof(recvData));
memset(sendData,'\\0',sizeof(sendData));
printf("输入要传输的信息:");
gets(sendData);
printf("\\033[1A"); //先回到上一行
printf("\\033[K"); //清除该行
print_time_to(dest_addr.sin_addr);
printf("发送消息:%s\\n",sendData);
sendto(socket_client,sendData,strlen(sendData),0,(struct sockaddr *)&dest_addr,sizeof(dest_addr));
recvfrom(socket_client,recvData,200,0,(struct sockaddr *)&dest_addr,&len);
print_time_from(dest_addr.sin_addr);
printf("收到消息:%s\\n",recvData);
}
closesocket(socket_client);
WSACleanup();
return 0;
}
void print_time(){
time_t cur_time;
time(&cur_time);
char *now=ctime(&cur_time);
now[24]='\\0';
printf("[%s]",now);
}
void print_time(in_addr addr){
time_t cur_time;
time(&cur_time);
char *now=ctime(&cur_time);
now[24]='\\0';
printf("[%s](From %s):",now,inet_ntoa(addr));
}
void print_time_from(in_addr addr) {
time_t cur_time;
time(&cur_time);
char* now = ctime(&cur_time);
now[24] = '\\0';
printf("[%s](From %s):", now, inet_ntoa(addr));
}
void print_time_to(in_addr addr) {
time_t cur_time;
time(&cur_time);
char* now = ctime(&cur_time);
now[24] = '\\0';
printf("[%s](To %s):", now, inet_ntoa(addr));
}
对于UDP协议的实现过程,还需要加深理解