码迷,mamicode.com
首页 > 其他好文 > 详细

一种简单的UDP服务器框架

时间:2014-10-19 11:37:34      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:udp linux

代码组织:

bubuko.com,布布扣

进入lib,

bubuko.com,布布扣

socket.h

/**
  @file		socket.h
  @brief	Socket API header file

  UDP socket utility functions, it provides simple functions that helps
  to build UDP client/server.

  @author wangzhicheng
 */
#ifndef SOCKET_H
#define SOCKET_H

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <resolv.h>
#include <fcntl.h>

int	UDPServerInit(int port, int *serverfd);
int	UDPClientInit(int *clientfd);
int	UDPRecv(int sockfd, void *buf, size_t size, char *from_ip, int *port);
int	UDPSend(int sockfd, const void *buf, size_t size, const char *to_ip, const int *port);
int UDPClose(int sockfd);

#endif

socket.c

#include "socket.h"
/*
 * @brief		initialize UDP server
 * @port		port number for socket
 * @serverfd	server socket fd
 * return server socked fd for success, on error return error code
 * */
int	UDPServerInit(int port, int *serverfd) {
	struct sockaddr_in server;
	// create socket , same as client
	*serverfd = socket(AF_INET, SOCK_DGRAM, 0);
	if(*serverfd < 0) return -1;
	/// initialize structure dest
	memset((void*)&server, '\0', sizeof(server));
	server.sin_family = AF_INET;
	server.sin_port = htons(port);
	server.sin_addr.s_addr = INADDR_ANY;
	// Assign a port number to socket
	bind(*serverfd, (struct sockaddr*)&server, sizeof(server));

	return *serverfd;
}
/*
 * @brief	initialize UDP client
 * @clientfd	client socket fd
 * return client socked fd for success, on error return error code
 */
int	UDPClientInit(int *clientfd) {
	*clientfd = socket(AF_INET, SOCK_DGRAM, 0);

	return *clientfd;
}
/*
 * @brief		UDPRecv from UDP socket
 * @clientfd	socket fd
 * @buf	     	receving buffer
 * @size		buffer size
 * @from_ip		the client ip address of requested from UDP
 * return	    the length of receive data
 */
int	UDPRecv(int sockfd, void *buf, size_t size, char *from_ip, int *port) {
	struct sockaddr_in client;
	size_t addrlen = 0;
	int rc;
	memset(&client, '\0', sizeof(client));
	addrlen = sizeof(client);
	rc = recvfrom(sockfd, buf, size, 0, (struct sockaddr *)&client, &addrlen);
	strcpy(from_ip, (const char *)inet_ntoa(client.sin_addr));
	*port = htons(client.sin_port);

	return rc;
	
}
/*
 * @brief		UDPSend from UDP socket
 * @clientfd	socket fd
 * @buf	     	sending buffer
 * @size		buffer size
 * @to_ip		the ip address of target server
 * return	    the length of sending data
 */
int	UDPSend(int sockfd, const void *buf, size_t size, const char *to_ip, const int *port) {
	struct sockaddr_in server;
	memset(&server, '\0', sizeof(server));
	server.sin_family = AF_INET;
	server.sin_port = htons(*port);
	inet_aton(to_ip, &server.sin_addr);

	return sendto(sockfd, buf, size, 0, (struct sockaddr *)&server, sizeof(server));
}
/*
 * close the socket
 * */
int UDPClose(int sockfd) {
	return close(sockfd);
}

进入server

bubuko.com,布布扣

server.c

/*************************************************************************
    > File Name: server.c
    > Author: ma6174
    > Mail: ma6174@163.com 
    > Created Time: Sat 04 Oct 2014 09:46:30 PM WST
 ************************************************************************/

#include "socket.h"

int UDPPORT = 4001;
#define SIZE 256

int main() {
	int serverfd = -1;
	char buf[SIZE];
	char from_ip[SIZE];
	int port, num;
	if(UDPServerInit(UDPPORT, &serverfd) < 0) {
		perror("can init UDP server...!\n");
		exit(EXIT_FAILURE);
	}
	while(1) {
		num = UDPRecv(serverfd, buf, SIZE, from_ip, &port);
		if(num < 0) {
			perror("receving error...!\n");
			exit(EXIT_FAILURE);
		}
		buf[num] = '\0';
		printf("You got a message %s from client ..\n Its ip is %s, port is %d\n", buf, from_ip, port);
		if(!strcmp(buf, "bye")) {
			break;
		}
//		sleep(1);
	}
	UDPClose(serverfd);

	return 0;
}

Makefile

CC=gcc
LIBRARY=../lib
CFLAGS=-I$(LIBRARY)
CXXFLAGS=
OBJS1=server.o  socket.o 

all:	server


server: $(OBJS1)
	$(CC) -o   $@ $(OBJS1)  

socket.o: $(LIBRARY)/socket.c
	$(CC) -c $(LIBRARY)/socket.c

clean:
	rm *.o server  > /dev/null 2>&1

进入client

bubuko.com,布布扣

client.c

/*************************************************************************
    > File Name: client.c
    > Author: ma6174
    > Mail: ma6174@163.com 
    > Created Time: Sat 04 Oct 2014 09:46:30 PM WST
 ************************************************************************/

#include "socket.h"

#define SIZE 256
int UDPPORT = 4001;
const char *serveraddr = "127.0.0.1";

int main() {
	int clientfd = -1;
	char buf[SIZE];
	size_t  num;
	UDPClientInit(&clientfd);
	if(clientfd < 0) {
		perror("UDP client init failed...!\n");
		exit(EXIT_FAILURE);
	}
	while(1) {
		puts("sending message...:");
		scanf("%s", buf);
		UDPSend(clientfd, buf, strlen(buf), serveraddr, &UDPPORT);
//		sleep(1);
	}
	return 0;
}
Makefile

CC=gcc
LIBRARY=../lib
CFLAGS=-I$(LIBRARY)
CXXFLAGS=
OBJS1=client.o  socket.o 

all:	client


client: $(OBJS1)
	$(CC) -o   $@ $(OBJS1) 

socket.o: $(LIBRARY)/socket.c
	$(CC) -c $(LIBRARY)/socket.c

clean:
	rm *.o client  > /dev/null 2>&1

测试:

bubuko.com,布布扣


一种简单的UDP服务器框架

标签:udp linux

原文地址:http://blog.csdn.net/wangzhicheng1983/article/details/40261087

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!