http://www.cs.tau.ac.il/~eddiea/samples/
server.c
http://www.cs.tau.ac.il/~eddiea/samples/Non-Blocking/tcp-nonblocking-server.c.html
client.c
http://www.cs.tau.ac.il/~eddiea/samples/Non-Blocking/tcp-nonblocking-client.c.html
阅读(160) | 评论(0) | 转发(0) |
<script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"16"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];</script>
server.c
http://www.cs.tau.ac.il/~eddiea/samples/Non-Blocking/tcp-nonblocking-server.c.html
点击(此处)折叠或打开
- // gcc server.c -o server
- // indent -npro -kr -i8 -ts8 -sob -l280 -ss -ncs -cp1 *
-
- /***************************************************************************/
- /* */
- /* Server program which wait for the client to connect and reads the data */
- /* using non-blocking socket. */
- /* The reading of non-blocking sockets is done in a loop until data */
- /* arrives to the sockfd. */
- /* */
- /* based on Beej's program - look in the simple TCP server for further doc.*/
- /* */
- /* */
- /***************************************************************************/
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <errno.h>
- #include <string.h>
- #include <sys/types.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
- #include <sys/socket.h>
- #include <sys/wait.h>
- #include <fcntl.h> /* Added for the nonblocking socket */
-
- #define MYPORT 3456 /* the port users will be connecting to */
- #define BACKLOG 10 /* how many pending connections queue will hold */
-
- int main()
- {
- int sockfd, new_fd; /* listen on sock_fd, new connection on new_fd */
- struct sockaddr_in my_addr; /* my address information */
- struct sockaddr_in their_addr; /* connector's address information */
- int sin_size;
- char string_read[255];
- int n, i;
- int last_fd; /* Thelast sockfd that is connected */
-
- if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
- perror("socket");
- exit(1);
- }
-
- last_fd = sockfd;
-
- my_addr.sin_family = AF_INET; /* host byte order */
- my_addr.sin_port = htons(MYPORT); /* short, network byte order */
- my_addr.sin_addr.s_addr = INADDR_ANY; /* auto-fill with my IP */
- bzero(&(my_addr.sin_zero), 8); /* zero the rest of the struct */
-
- if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1) {
- perror("bind");
- exit(1);
- }
-
- if (listen(sockfd, BACKLOG) == -1) {
- perror("listen");
- exit(1);
- }
-
- if ((new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size)) == -1) {
- perror("accept");
- }
- fcntl(last_fd, F_SETFL, O_NONBLOCK); /* Change the socket into non-blocking state */
- fcntl(new_fd, F_SETFL, O_NONBLOCK); /* Change the socket into non-blocking state */
-
- while (1) {
- for (i = sockfd; i <= last_fd; i++) {
- printf("Round number %d\n", i);
- if (i = sockfd) {
- sin_size = sizeof(struct sockaddr_in);
- if ((new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size)) == -1) {
- perror("accept");
- }
- printf("server: got connection from %s\n", inet_ntoa(their_addr.sin_addr));
- fcntl(new_fd, F_SETFL, O_NONBLOCK);
- last_fd = new_fd;
- } else {
- n = recv(new_fd, string_read, sizeof(string_read), 0);
- if (n < 1) {
- perror("recv - non blocking \n");
- printf("Round %d, and the data read size is: n=%d \n", i, n);
- } else {
- string_read[n] = '\0';
- printf("The string is: %s \n", string_read);
- if (send(new_fd, "Hello, world!\n", 14, 0) == -1)
- perror("send");
- }
- }
- }
- }
- return 0;
- }
http://www.cs.tau.ac.il/~eddiea/samples/Non-Blocking/tcp-nonblocking-client.c.html
点击(此处)折叠或打开
- // gcc client.c -o client
- // indent -npro -kr -i8 -ts8 -sob -l280 -ss -ncs -cp1 *
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <errno.h>
- #include <string.h>
- #include <netdb.h>
- #include <sys/types.h>
- #include <netinet/in.h>
- #include <sys/socket.h>
- #include <unistd.h>
-
- #define PORT 3456 /* the port client will be connecting to */
-
- #define MAXDATASIZE 100 /* max number of bytes we can get at once */
-
- int main(int argc, char *argv[])
- {
- int sockfd, numbytes;
- char buf[MAXDATASIZE];
- struct hostent *he;
- struct sockaddr_in their_addr; /* connector's address information */
-
- if (argc != 2) {
- fprintf(stderr, "usage: client hostname\n");
- exit(1);
- }
-
- if ((he = gethostbyname(argv[1])) == NULL) { /* get the host info */
- herror("gethostbyname");
- exit(1);
- }
-
- if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
- perror("socket");
- exit(1);
- }
-
- their_addr.sin_family = AF_INET; /* host byte order */
- their_addr.sin_port = htons(PORT); /* short, network byte order */
- their_addr.sin_addr = *((struct in_addr *)he->h_addr);
- bzero(&(their_addr.sin_zero), 8); /* zero the rest of the struct */
-
- if (connect(sockfd, (struct sockaddr *)&their_addr, sizeof(struct sockaddr)) == -1) {
- perror("connect");
- exit(1);
- }
- /* sleep(5); */
- while (1) {
- if (send(sockfd, "Hello, world!\n", 14, 0) == -1)
- perror("send");
- printf("In loop \n");
- sleep(2);
- }
- close(sockfd);
-
- return 0;
- }
相关热门文章
给主人留下些什么吧!~~
评论热议