UDP
收UDP
#include <sys/types.h>
#include <sys/socket.h>
#include<pthread.h>
#include <netinet/in.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
if (argc != 2)
{
printf("Usage: %s port\n", argv[0]);
exit(1);
}
printf("Welcome! This is a UDP server, I can only received message from client and reply with same message\n");
struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(atoi(argv[1]));
addr.sin_addr.s_addr = htonl(INADDR_ANY);
int sock;
if ( (sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
{
perror("socket");
exit(1);
}
if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0)
{
perror("bind");
exit(1);
}
char buff[512];
struct sockaddr_in clientAddr;
int n;
int len = sizeof(clientAddr);
while (1)
{
n = recvfrom(sock, buff, 511, 0, (struct sockaddr*)&clientAddr, &len);
if (n>0)
{
buff[n] = 0;
printf("%s %u says: %s\n", inet_ntoa(clientAddr.sin_addr), ntohs(clientAddr.sin_port), buff);
n = sendto(sock, buff, n, 0, (struct sockaddr *)&clientAddr, sizeof(clientAddr));
if (n < 0)
{
perror("sendto");
break;
}
}
else
{
perror("recv");
break;
}
}
return 0;
}
发UDP
#include <sys/types.h>
#include <sys/socket.h>
#include<pthread.h>
#include <netinet/in.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
if (argc != 3)
{
printf("Usage: %s ip port", argv[0]);
exit(1);
}
printf("This is a UDP client\n");
struct sockaddr_in addr;
int sock;
if ( (sock=socket(AF_INET, SOCK_DGRAM, 0)) <0)
{
perror("socket");
exit(1);
}
addr.sin_family = AF_INET;
addr.sin_port = htons(atoi(argv[2]));
addr.sin_addr.s_addr = inet_addr(argv[1]);
if (addr.sin_addr.s_addr == INADDR_NONE)
{
printf("Incorrect ip address!");
close(sock);
exit(1);
}
char buff[512];
int len = sizeof(addr);
while (1)
{
gets(buff);
int n;
n = sendto(sock, buff, strlen(buff), 0, (struct sockaddr *)&addr, sizeof(addr));
if (n < 0)
{
perror("sendto");
close(sock);
break;
}
n = recvfrom(sock, buff, 512, 0, (struct sockaddr *)&addr, &len);
if (n>0)
{
buff[n] = 0;
printf("received:");
puts(buff);
}
else if (n==0)
{
printf("server closed\n");
close(sock);
break;
}
else if (n == -1)
{
perror("recvfrom");
close(sock);
break;
}
}
return 0;
}
组播
收组播流
#include <stdio.h>
#include <sys/types.h>用法:./testudp 192.168.200.105 238.123.46.66 8001
发组播流
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define PORT 10086
#define SIZE 128
int main(void)
{
int ret = -1;
int sockfd = -1;
int i = 0;
char buf[SIZE];
struct sockaddr_in peer;
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (-1 == sockfd)
{
perror("socket");
goto err0;
}
memset(&peer, 0, sizeof(peer));
peer.sin_family = AF_INET;
peer.sin_port = htons(PORT);
inet_pton(AF_INET, "224.0.0.88", &peer.sin_addr);
printf("send data to UDP Server %s: %d\n", inet_ntoa(peer.sin_addr), ntohs(peer.sin_port));
//向多播组发送消息
while(1)
{
sprintf(buf, "hello world %d", i);
ret = sendto(sockfd, buf, strlen(buf), 0, (void*)&peer, sizeof(peer));
printf("ret: %d\n", ret);
sleep(1);
i++;
}
return 0;
err0:
return -1;
}
因篇幅问题不能全部显示,请点此查看更多更全内容
Copyright © 2019- oldu.cn 版权所有 浙ICP备2024123271号-1
违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com
本站由北京市万商天勤律师事务所王兴未律师提供法律服务