转载声明:文章来源https://blog.csdn.net/weixin_44618297/article/details/130741682
一、双向通信(不区分客户端和服务器)
1、发送
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#define BUF_SIZE 128
#define SENDPROT 30300
#define SENDADDR "192.168.62.199"
#define RECVPROT 30301
#define RECVADDR "192.168.62.224"
// #define SENDPROT 6666
// #define SENDADDR "192.168.3.186"
// #define RECVPROT 8888
// #define RECVADDR "192.168.3.187"
void print_data(const char *string, unsigned char*data, int size)
{
int i;
printf("---------------------------------------------\n");
printf("%s:\n", string);
for(i=0; i<size; i++)
{
printf("%02x ", data[i]);
if((i%16) == 15)
printf("\n");
}
if(size%16)
printf("\n");
printf("---------------------------------------------\n");
}
int main(int argc, char *argv[])
{
//建立套接字
int sock_fd = socket(AF_INET, SOCK_DGRAM, 0); //IPV4,数据报套接字类型,不指定协议
struct sockaddr_in sendaddr;
sendaddr.sin_family = AF_INET; //协议类型IPV4
sendaddr.sin_port = htons(SENDPROT); //端口号-网络字节序
sendaddr.sin_addr.s_addr = inet_addr(SENDADDR); //IP地址-字符串转IP
//发送数据
char buf[BUF_SIZE] = { 0 };
struct sockaddr_in recvaddr;
recvaddr.sin_family = AF_INET; //协议类型IPV4
recvaddr.sin_port = htons(RECVPROT); //端口号-网络字节序
recvaddr.sin_addr.s_addr = inet_addr(RECVADDR);
socklen_t recvaddrlen = sizeof(recvaddr);
//绑定地址
bind(sock_fd, (const struct sockaddr *)&recvaddr, sizeof(recvaddr));
while (1)
{
memset(buf,0, BUF_SIZE);
memset(buf, 0x01, BUF_SIZE);
printf("udp client sendto:[%s - %d]\n",SENDADDR, SENDPROT);
int rv = sendto(sock_fd, buf, strlen(buf), 0, (const struct sockaddr*)&sendaddr, sizeof(sendaddr));//给服务器发送数据
if(rv < 0)
{
printf("sendto error!\n");
}
bzero(buf, BUF_SIZE);
bzero(&recvaddr, recvaddrlen);
rv = recvfrom(sock_fd, buf, BUF_SIZE, 0, (struct sockaddr *)&recvaddr, &recvaddrlen);
if(rv < 0)
{
printf("recv error!");
}
// printf("udp client recvfrom:[%s - %hu]\n",inet_ntoa(recvaddr.sin_addr), recvaddr.sin_port);
printf("udp client recvfrom:[%s - %d]\n",inet_ntoa(recvaddr.sin_addr), ntohs(recvaddr.sin_port));
print_data("recv data", (uint8_t*)buf, sizeof(buf)); //打印数据
break;
//sleep(1);
}
//关闭套接字
close(sock_fd);
return 0;
}
2、接收
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#define BUF_SIZE 128
#define SENDPROT 30300
#define SENDADDR "192.168.62.199"
#define RECVPROT 30301
#define RECVADDR "192.168.62.224"
// #define SENDPROT 8888
// #define SENDADDR "192.168.3.187"
// #define RECVPROT 6666
// #define RECVADDR "192.168.3.186"
void print_data(const char *string, unsigned char*data, int size)
{
int i;
printf("---------------------------------------------\n");
printf("%s:\n", string);
for(i=0; i<size; i++)
{
printf("%02x ", data[i]);
if((i%16) == 15)
printf("\n");
}
if(size%16)
printf("\n");
printf("---------------------------------------------\n");
}
int main(int argc, char *argv[])
{
//建立套接字
int sock_fd = socket(AF_INET, SOCK_DGRAM, 0); //IPV4,数据报套接字类型,不指定协议
struct sockaddr_in sendaddr;
sendaddr.sin_family = AF_INET; //协议类型IPV4
sendaddr.sin_port = htons(SENDPROT); //端口号-网络字节序
sendaddr.sin_addr.s_addr = inet_addr(SENDADDR); //IP地址-字符串转IP
//发送数据
char buf[BUF_SIZE] = { 0 };
struct sockaddr_in recvaddr;
recvaddr.sin_family = AF_INET; //协议类型IPV4
recvaddr.sin_port = htons(RECVPROT); //端口号-网络字节序
recvaddr.sin_addr.s_addr = inet_addr(RECVADDR);
socklen_t recvaddrlen = sizeof(recvaddr);
//绑定地址
bind(sock_fd, (const struct sockaddr *)&recvaddr, sizeof(recvaddr));
while (1)
{
bzero(buf, BUF_SIZE);
bzero(&recvaddr, recvaddrlen);
int rv = recvfrom(sock_fd, buf, BUF_SIZE, 0, (struct sockaddr *)&recvaddr, &recvaddrlen);
if(rv < 0)
{
printf("recv error!");
}
// printf("udp server recvfrom:[%s - %hu]\n",inet_ntoa(recvaddr.sin_addr), recvaddr.sin_port);
printf("udp server recvfrom:[%s - %d]\n",inet_ntoa(recvaddr.sin_addr), ntohs(recvaddr.sin_port));
print_data("recv data", (uint8_t*)buf, sizeof(buf)); //打印数据
// memset(buf,0, BUF_SIZE);
// memset(buf, 0x01, BUF_SIZE);
printf("udp server sendto:[%s - %d]\n",SENDADDR, SENDPROT);
rv = sendto(sock_fd, buf, strlen(buf), 0, (const struct sockaddr*)&sendaddr, sizeof(sendaddr));//给服务器发送数据
if(rv < 0)
{
printf("sendto error!\n");
}
break;
//sleep(1);
}
//关闭套接字
close(sock_fd);
return 0;
}
二、双向通信(客户端和服务端互相发送接收)
1、服务端
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <math.h>
#include <fcntl.h>
#include <sys/types.h>
#include <vector>
#include <iostream>
#include <tcp.h>
#include <sys/time.h>
#include <sys/socket.h> //提供socket函数及数据结构
#include <sys/types.h>
#include <unistd.h>
#include <pthread.h>
#include <atomic>
#include <thread>
#include <stdbool.h>
#include <string.h>
#include <errno.h>
#include <arpa/inet.h>
using namespace std;
#define BUF_SIZE 128
#define PORT 5555
void print_data(const char *string, unsigned char*data, int size)
{
int i;
printf("---------------------------------------------\n");
printf("%s:\n", string);
for(i=0; i<size; i++)
{
printf("%02x ", data[i]);
if((i%16) == 15)
printf("\n");
}
if(size%16)
printf("\n");
printf("---------------------------------------------\n");
}
int main(int argc, char **argv)
{
//建立套接字
int sock_fd = socket(AF_INET, SOCK_DGRAM, 0); //IPV4,数据报套接字类型,不指定协议
struct sockaddr_in srvaddr;
srvaddr.sin_family = AF_INET; //协议类型IPV4
srvaddr.sin_port = htons(PORT); //端口号-网络字节序
srvaddr.sin_addr.s_addr = htonl(INADDR_ANY); //IP地址:表示绑定本机IP
//srvaddr.sin_addr.s_addr = inet_addr(ADDR); //IP地址-字符串转IP
//绑定地址
bind(sock_fd, (const struct sockaddr *)&srvaddr, sizeof(srvaddr));
//接收数据
char buf[BUF_SIZE] = { 0 };
struct sockaddr_in cliaddr;
socklen_t cliaddrlen = sizeof(cliaddr);
while (1)
{
bzero(buf, BUF_SIZE);
bzero(&cliaddr, cliaddrlen);
int rv = recvfrom(sock_fd, buf, BUF_SIZE, 0, (struct sockaddr *)&cliaddr, &cliaddrlen);
if(rv < 0)
{
printf("recvfrom data error!");
}
else
{
printf("udp server recvfrom:[%s - %hu]\n",inet_ntoa(cliaddr.sin_addr), cliaddr.sin_port);
print_data("recv data:", (uint8_t*)buf, sizeof(buf)); //打印数据
}
rv = sendto(sock_fd, buf, strlen(buf), 0, (const struct sockaddr*)&cliaddr, sizeof(cliaddr));
if(rv < 0)
{
printf("send data error!");
}
else
{
printf("udp server sendto:[%s - %hu]\n",inet_ntoa(cliaddr.sin_addr), cliaddr.sin_port);
}
}
//关闭套接字
close(sock_fd);
return 0;
}
2、客户端
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#define BUF_SIZE 128
#define PROT 5555
#define ADDR "192.168.3.186"
void print_data(const char *string, unsigned char*data, int size)
{
int i;
printf("---------------------------------------------\n");
printf("%s:\n", string);
for(i=0; i<size; i++)
{
printf("%02x ", data[i]);
if((i%16) == 15)
printf("\n");
}
if(size%16)
printf("\n");
printf("---------------------------------------------\n");
}
int main(int argc, char *argv[])
{
//建立套接字
int sock_fd = socket(AF_INET, SOCK_DGRAM, 0); //IPV4,数据报套接字类型,不指定协议
//声明将连接地址
struct sockaddr_in srvaddr;
srvaddr.sin_family = AF_INET; //协议类型IPV4
srvaddr.sin_port = htons(PROT); //端口号-网络字节序
srvaddr.sin_addr.s_addr = inet_addr(ADDR); //IP地址-字符串转IP
//srvaddr.sin_addr.s_addr = htons(INADDR_ANY);
//发送数据
char buf[BUF_SIZE] = { 0 };
struct sockaddr_in cliaddr;
socklen_t cliaddrlen = sizeof(cliaddr);
while (1)
{
memset(buf,0, BUF_SIZE);
memset(buf, 0x01, BUF_SIZE);
printf("udp client sendto:[%s - %d]\n",ADDR, PROT);
int rv = sendto(sock_fd, buf, strlen(buf), 0, (const struct sockaddr*)&srvaddr, sizeof(srvaddr));//给服务器发送数据
if(rv < 0)
{
printf("sendto error!\n");
}
bzero(buf, BUF_SIZE);
bzero(&cliaddr, cliaddrlen);
rv = recvfrom(sock_fd, buf, BUF_SIZE, 0, (struct sockaddr *)&cliaddr, &cliaddrlen);
if(rv < 0)
{
printf("recv error!");
}
printf("udp client recvfrom:[%s - %hu]\n",inet_ntoa(cliaddr.sin_addr), cliaddr.sin_port);
print_data("recv data:", (uint8_t*)buf, sizeof(buf)); //打印数据
sleep(1);
}
//关闭套接字
close(sock_fd);
return 0;
}
三、单向通信(客户端只能发送,服务端只能接收)
1、服务端
只接收一个字符串
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#define BUF_SIZE 88
#define PORT 55555
int main(int argc, char *argv[])
{
//建立套接字
int sock_fd = socket(AF_INET, SOCK_DGRAM, 0); //IPV4,数据报套接字类型,不指定协议
//说明本服务器地址
/* const struct sockaddr_in srvaddr = {
.sin_family = AF_INET, //协议类型IPV4
.sin_port = htons(PORT), //端口号-网络字节序
.sin_addr.s_addr = htonl(INADDR_ANY) //IP地址-任意地址
};*/
struct sockaddr_in srvaddr;
srvaddr.sin_family = AF_INET; //协议类型IPV4
srvaddr.sin_port = htons(PORT); //端口号-网络字节序
srvaddr.sin_addr.s_addr = htonl(INADDR_ANY); //IP地址-任意地址
//绑定地址
bind(sock_fd, (const struct sockaddr *)&srvaddr, sizeof(srvaddr));
//接收数据
char buf[BUF_SIZE] = { 0 };
struct sockaddr_in cliaddr;
socklen_t cliaddrlen = sizeof(cliaddr);
while (1)
{
bzero(buf, BUF_SIZE);
bzero(&cliaddr, cliaddrlen);
recvfrom(sock_fd, buf, BUF_SIZE, 0, (struct sockaddr *)&cliaddr, &cliaddrlen);
printf("udp server from [%s - %hu]:%s\n",inet_ntoa(cliaddr.sin_addr),cliaddr.sin_port,buf);
}
//关闭套接字
close(sock_fd);
return 0;
}
2、客户端
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#define BUF_SIZE 88
#define PROT 55555
#define ADDR "192.168.111.213"
int main(int argc, char *argv[])
{
//建立套接字
int sock_fd = socket(AF_INET, SOCK_DGRAM, 0); //IPV4,数据报套接字类型,不指定协议
//声明将连接地址
/* const struct sockaddr_in srvaddr = {
.sin_family = AF_INET, //协议类型IPV4
.sin_port = htons(PROT), //端口号-网络字节序
.sin_addr.s_addr = inet_addr(ADDR) //IP地址-字符串转IP
};*/
//声明将连接地址
struct sockaddr_in srvaddr;
srvaddr.sin_family = AF_INET; //协议类型IPV4
srvaddr.sin_port = htons(PROT); //端口号-网络字节序
srvaddr.sin_addr.s_addr = inet_addr(ADDR); //IP地址-字符串转IP
//发送数据
char buf[BUF_SIZE] = { 0 };
int count = 0;
while (1)
{
memset(buf,0, BUF_SIZE);
sprintf(buf, "%d",count);//每次发一个字符串
printf("udp client send:[%s - %d]:%s\n",ADDR,PROT,buf);
sendto(sock_fd, buf, strlen(buf), 0, (const struct sockaddr*)&srvaddr, sizeof(srvaddr));
count++;
sleep(1);
}
//关闭套接字
close(sock_fd);
return 0;
}
帖子还没人回复快来抢沙发