一个简单的udp客户端服务端代码:
公共文件部分:global.c
#include
#include
#include
#include
#include
#include
#include
int
Socket(int family, int type, int protocol)
{
int n;
if ( (n = socket(family, type, protocol)) < 0)
{
perror("socket error");
exit(1);
}
return(n);
}
void
Bind(int fd, const struct sockaddr *sa, socklen_t salen)
{
if (bind(fd, sa, salen) < 0)
{
perror("bind error");
exit(1);
}
}
void
Sendto(int fd, const void *ptr, size_t nbytes, int flags,
const struct sockaddr *sa, socklen_t salen)
{
if (sendto(fd, ptr, nbytes, flags, sa, salen) != (ssize_t)nbytes)
{
perror("sendto error");
exit(1);
}
}
ssize_t
Recvfrom(int fd, void *ptr, size_t nbytes, int flags,
struct sockaddr *sa, socklen_t *salenptr)
{
ssize_t n;
if ( (n = recvfrom(fd, ptr, nbytes, flags, sa, salenptr)) < 0)
{
perror("recvfrom error");
exit(1);
}
return(n);
}
客户端:udpcli.c
#include "global.c"
int main(int argc, char* argv[])
{
int sockfd;
struct sockaddr_in conn_addr;
struct sockaddr_in serv_addr;
char buf[1024] = "hello,world";
int n = 0;
char serv_ip[INET_ADDRSTRLEN];
socklen_t serv_addr_len;
sockfd = Socket(AF_INET, SOCK_DGRAM, 0);
conn_addr.sin_family = AF_INET;
conn_addr.sin_port = htons(8345);
if (inet_pton(AF_INET, "127.0.0.1", &conn_addr.sin_addr) == -1)
{
perror("inet_pton error\n");
close(sockfd);
exit(0);
}
serv_addr_len = sizeof(serv_addr);
Sendto(sockfd, buf, sizeof(buf), 0, (struct sockaddr*)&conn_addr, sizeof(conn_addr));
memset(buf, 0, sizeof(buf));
n = recvfrom(sockfd, buf, sizeof(buf), 0, (struct sockaddr*)&serv_addr, &serv_addr_len);
if (n > 0)
{
if (inet_ntop(AF_INET, &serv_addr.sin_addr, serv_ip, sizeof(serv_ip)) == NULL)
{
perror("inet_ntop error\n");
close(sockfd);
exit(-1);
}
printf("recv data from %s : %d %s\n", serv_ip, ntohs(serv_addr.sin_port), buf);
}
close(sockfd);
exit(0);
}
udp服务端:udpserv.c
#include "global.c"
int main(int argc, char *argv[])
{
int sockfd = 0;
struct sockaddr_in serv_addr;
struct sockaddr_in cli_addr;
socklen_t cli_addr_len;
char serv_ip[INET_ADDRSTRLEN];
char buf[1024];
sockfd = Socket(AF_INET, SOCK_DGRAM, 0);
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(8345);
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
if (inet_ntop(AF_INET, &serv_addr.sin_addr, serv_ip, sizeof(serv_ip)) == NULL) {
printf("inet_ntop error\n");
close(sockfd);
exit(0);
}
printf("bind in %s : %d\n", serv_ip, ntohs(serv_addr.sin_port));
Bind(sockfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr));
cli_addr_len = sizeof(cli_addr);
Recvfrom(sockfd, buf, sizeof(buf), 0, (struct sockaddr*)&cli_addr, &cli_addr_len);
printf("recv data from %s : %d %s\n", inet_ntoa(cli_addr.sin_addr), ntohs(cli_addr.sin_port), buf);
ssize_t nn = sendto(sockfd, buf, sizeof(buf), 0, (struct sockaddr*)&cli_addr, sizeof(cli_addr));
if (nn == -1)
printf("sendto error\n");
printf("sendto %ld bytes\n", nn);
close(sockfd);
return 0;
}
makefile文件:Makefile
CC=gcc
CFLAGS=-Wall -std=c99 -pedantic
EXEC=server client
all: server client
client: udpcli.o
$(CC) $(CFLAGS) udpcli.o -o client
udpcli.o: udpcli.c global.o
$(CC) $(CFLAGS) -c udpcli.c -o udpcli.o
server: udpserv.o
$(CC) $(CFLAGS) udpserv.o -o server
udpserv.o: udpserv.c global.o
$(CC) $(CFLAGS) -c udpserv.c -o udpserv.o
global.o: global.c
$(CC) $(CFLAGS) -c global.c -o global.o
clean:
rm -f $(EXEC) *.o *~