Libev implementation of tiny socket server

Source: Internet
Author: User
Tags bind printf socket socket error

The main references are 3 places:

1. Libev Official Handbook: http://pod.tst.eu/http://cvs.schmorp.de/libev/ev.pod

2.lengzijian's Blog: http://blog.csdn.net/lengzijian/article/details/8315133

3. The company's own projects

----

/* * @author: Wusuopubupt * @date: 2014-09-04 * @desc: Tiny socket server implemented by Libev * to use th
 is, you should install Libev at first. * * Server:just Run the program * client:telnet localhost 8080 * * @refer: 1). http://pod.tst.eu/http://c Vs.schmorp.de/libev/ev.pod * 2). http://blog.csdn.net/lengzijian/article/details/8315133 * */#include <ev.h> #include <stdio.h> #include & lt;stdlib.h> #include <string.h> #include <netinet/in.h> #include <unistd.h> #define PORT 8080 #def

INE buffer_size 1024x768 #define Max_connections struct Ev_io *libevlist[max_connections] = {NULL};
void Socket_accept_callback (struct ev_loop *loop, struct ev_io *watcher, int revents);
void Socket_read_callback (struct ev_loop *loop, struct ev_io *watcher, int revents);						  /* Server Client Socket Socket |
	  |						  V-V bind connect |
	  |						  V V Listen Write |
	  |					V	  V Accept Read |
	  |
	  V V Read Close |
  	  V Write |

	V close */int main () {struct Ev_loop *loop = ev_default_loop (0);
	/* Socket start */int sd;
	struct sockaddr_in addr;

	int addr_len = sizeof (addr);
	struct Ev_io *socket_watcher = (struct ev_io*) malloc (sizeof (struct ev_io));

	struct Ev_timer *timeout_watcher = (struct ev_timer*) malloc (sizeof (struct ev_timer));
	Socket SD = socket (pf_inet, sock_stream, 0);
		if (SD < 0) {printf ("Socket error\n");
	return-1;
	} bzero (&addr, sizeof (addr));
	addr.sin_family = af_inet;
	Addr.sin_port = htons (port);

	ADDR.SIN_ADDR.S_ADDR = Inaddr_any;
		Bind if (Bind (SD, (struct sockaddr*) &addr, sizeof (addr))! = 0) {printf ("bind error\n");
	return-1;
		}//Listen if (Listen (SD, Somaxconn) < 0) {printf ("Listen error\n");
	return-1;
	}//Set SD reuseful int breuseaddr = 1; if (setsockopt (SD, Sol_socket, SO_REUSEADDR, (const char*) &breuseaddr, sizeof (BreuseaDDR))! = 0) {printf ("setsockopt Error in reuseaddr[%d]\n", SD);
	return-1;
	}/* Socket end */Ev_io_init (socket_watcher, Socket_accept_callback, SD, Ev_read);

	Ev_io_start (Loop, socket_watcher);
	while (1) {Ev_run (loop, 0);
} return 1; } void Socket_accept_callback (struct ev_loop *loop, struct ev_io *watcher, int revents) {printf ("I am:%d\n", Getpid ())

	;
	struct sockaddr_in client_addr;
	socklen_t Client_len = sizeof (CLIENT_ADDR);

	int client_sd;

	Ev_io Watcher for client struct ev_io *client_watcher = (struct ev_io*) malloc (sizeof (struct ev_io));
		if (Client_watcher = = NULL) {printf ("malloc error in accept_cb\n");
	Return
		} if (Ev_error & revents) {printf ("ERROR event in accept\n");
	Return }//Socket accept:get File Description client_sd = Accept (WATCHER-&GT;FD, (struct sockaddr*) &client_addr, &c
	Lient_len);
		if (CLIENT_SD < 0) {printf ("Accept error\n");
	Return }//Too much connections if (Client_sd > Max_connectIONS) {printf ("fd Too large[%d]\n", CLIENT_SD);
		Close (CLIENT_SD);
	Return
		} if (LIBEVLIST[CLIENT_SD]! = null) {printf ("Client_sd NOT null FD is [%d]\n", CLIENT_SD);
	Return

	} printf ("Client connected\n");
	Listen new client Ev_io_init (Client_watcher, Socket_read_callback, CLIENT_SD, Ev_read);

	Ev_io_start (Loop, client_watcher);
LIBEVLIST[CLIENT_SD] = Client_watcher;
	} void Socket_read_callback (struct ev_loop *loop, struct ev_io *watcher, int revents) {char buffer[buffer_size];

	ssize_t Read;
		if (Ev_error & revents) {printf ("ERROR event in read\n");
	Return }//Socket recv read = recv (watcher->fd, buffer, buffer_size, 0);
		Read stream to Buffer if (read < 0) {printf ("read error\n");
	Return

		} if (read = = 0) {printf ("Client disconnected.\n");
		if (libevlist[watcher->fd] = = NULL) {printf ("The FD already freed[%d]\n", WATCHER-&GT;FD);
			} else {close (WATCHER-&GT;FD); Ev_io_stop (Loop, LIBEVLIST[WATCHER-&GT;FD]);
			Free (LIBEVLIST[WATCHER-&GT;FD]);
		LIBEVLIST[WATCHER-&GT;FD] = NULL;
	} return;
	} else {printf ("Receive message:%s\n", buffer);
	}//Socket send to client send (WATCHER-&GT;FD, buffer, read, 0);
Bzero (buffer, read);
 }



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.