udp编程实例1
//udp_server.c
#include <signal.h>
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <arpa/inet.h>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
int socket_fd;
void sig_fun(int signo)
{
if(signo == SIGINT){
printf("signo = %d\n",signo);
close(socket_fd);
exit(1);
}
}
void out_addr(struct sockaddr_in *clientaddr)
{
int port = ntohs(clientaddr->sin_port);
char ip[16];
if(inet_ntop(AF_INET, &clientaddr->sin_addr.s_addr, ip ,16) == NULL){
perror("out_addr:");
}
printf("cliet:%s(%d)\n",ip,port);
}
int main(int argc, char *argv[])
{
if(argc < 2){
printf("参数太少");
exit(1);
}
if(signal(SIGINT,sig_fun) == SIG_ERR){
perror("signal:");
exit(1);
}
/*1. 创建socket*/
if((socket_fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0){
perror("socket:");
exit(1);
}
int opt = 1;
if((setsockopt(socket_fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt))) < 0){//设置套接字选项,SO_REUSEADDR表示端口可以再次绑定
perror("setsockopt:");
exit(1);
}
/*2. 绑定IP地址和端口号*/
struct sockaddr_in serviceaddr;
memset(&serviceaddr, 0, sizeof(serviceaddr));
serviceaddr.sin_family = AF_INET;
serviceaddr.sin_port = htons(atoi(argv[1]));
serviceaddr.sin_addr.s_addr = INADDR_ANY;//一台服务器上可能有多块网卡(多个IP地址)
//这个宏是响应本机所有网卡(IP地址)上连接的客户端请求
if(bind(socket_fd, (struct sockaddr *)&serviceaddr, sizeof(serviceaddr)) < 0){
perror("bind:");
exit(1);
}
/*3. 读写*/
//int recvfrom(int sockfd.void * buf,int len, unsigned int flags,struct sockaddr *from,int *fromlen);
char buffer[512];
memset(buffer, 0, sizeof(buffer));
struct sockaddr_in clientaddr;
int len = sizeof(clientaddr);
if(recvfrom(socket_fd, buffer, sizeof(buffer), 0, (struct sockaddr *)&clientaddr, &len) < 0){
perror("recvfrom:");
}
printf("%s\n",buffer);
out_addr(&clientaddr);
//int sendto(int sockfd,const void*msg,int len,unsigned int flags,const struct sockaddr *to,int tolen);
time_t curtime;
time(&curtime);
char *pts = ctime(&curtime);
int length = strlen(pts);
if(sendto(socket_fd, pts, length + 1, 0, (struct sockaddr *)&clientaddr, len) < 0){
printf("sendto failed!\n");
}
return 0;
}
//udp_client.c
#include <signal.h>
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <arpa/inet.h>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
int socket_fd;
int main(int argc, char *argv[])
{
if(argc < 3){
printf("参数太少\n");
exit(1);
}
if((socket_fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0 ){
perror("socket:");
exit(1);
}
//int sendto(int sockfd,const void*msg,int len,unsigned int flags,const struct sockaddr *to,int tolen);
struct sockaddr_in serviceaddr;
memset(&serviceaddr, 0, sizeof(serviceaddr));
serviceaddr.sin_family = AF_INET;
serviceaddr.sin_port = htons(atoi(argv[2]));
if(inet_pton(AF_INET, argv[1], &serviceaddr.sin_addr.s_addr) < 0){
perror("inet_pton:");
exit(1);
}
/*
*udp中调用connect不会建立连接(三次握手),只会在内核中记录服务器的信息(IP地址和端口)
*调用connect的好处:客户端只会接受服务器端发来的信息,不会接受到处服务器以外的其他地方发过来的数据报文
*if(connect(socket_fd, (struct sockaddr *)&serviceaddr, sizeof(serviceaddr)) < 0){
* perror("connect");
* exit(1);
*}
*/
char buffer[512] = "hello world";
int len = strlen(buffer);
if(sendto(socket_fd, buffer, len, 0, (struct sockaddr *)&serviceaddr, sizeof(serviceaddr)) < 0){
printf("sendto failed\n");
}
printf("sendto completed\n");
/*调用connect函数可以调用send函数
*send(socket_fd, buffer, sizeof(buffer), 0);
*/
//int recvfrom(int sockfd.void * buf,int len, unsigned int flags,struct sockaddr *from,int *fromlen);
memset(buffer, 0, len);
if(recv(socket_fd, buffer, len, 0) < 0){
printf("recvfrom failed!\n");
}
printf("read=%s\n",buffer);
return 0;
}