#include <stdio.h> #include <string.h> #include <stdlib.h> #include <sys/types.h> /* See NOTES */ #include <sys/time.h> #include <unistd.h> #include <sys/select.h> #include <sys/socket.h> #include <errno.h> #include <strings.h> #include <netinet/in.h> #include <arpa/inet.h> #define N 64 typedef struct sockaddr SA; int main(int argc, char *argv[]) { int listenfd, connfd, maxfd, i; struct sockaddr_in servaddr, peeraddr; socklen_t len; char buf[N] = {0}; fd_set rdfs, bakrdfs; ssize_t n; if (argc < 3) { fprintf(stdout, "usage:%s <ip> <port>\n", argv[0]); exit(0); } bzero(&servaddr, sizeof(servaddr)); if ((listenfd = socket(PF_INET, SOCK_STREAM, 0)) == -1) { perror("socket"); exit(-1); } servaddr.sin_family = PF_INET; servaddr.sin_port = htons(atoi(argv[2])); servaddr.sin_addr.s_addr = inet_addr(argv[1]); if (bind(listenfd, (SA *)&servaddr, sizeof(servaddr)) == -1) { perror("bind"); exit(-1); } listen(listenfd, 5); maxfd = listenfd; FD_ZERO(&bakrdfs); FD_SET(listenfd, &bakrdfs); len = sizeof(peeraddr); while (1) { rdfs = bakrdfs; if (select(maxfd+1, &rdfs, NULL, NULL, NULL) == -1) { perror("select"); exit(-1); } for (i = 0; i <= maxfd; i++) { if (FD_ISSET(i, &rdfs)) { if (i == listenfd) { if ((connfd = accept(i, (SA *)&peeraddr, &len)) == -1) { perror("accept"); exit(-1); } fprintf(stdout, "welcome %s %d\n", inet_ntoa(peeraddr.sin_addr), ntohs(peeraddr.sin_port)); FD_SET(connfd, &bakrdfs); maxfd = (maxfd > connfd) ? maxfd : connfd; } else { bzero(buf, sizeof(buf)); if ((n = recv(i, buf, N, 0)) == 0) { close(i); FD_CLR(i, &bakrdfs); } else { printf("n=%d %s\n", n, buf); send(i, buf, N, 0); } } } } } exit(0); }
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <sys/types.h> /* See NOTES */ #include <sys/time.h> #include <unistd.h> #include <sys/select.h> #include <sys/socket.h> #include <errno.h> #include <strings.h> #include <netinet/in.h> #include <arpa/inet.h> #define N 64 typedef struct sockaddr SA; int main(int argc, char *argv[]) { int listenfd, connfd, maxfd, i; struct sockaddr_in servaddr, peeraddr; socklen_t len; char buf[N] = {0}; fd_set rdfs; if (argc < 3) { fprintf(stdout, "usage:%s <ip> <port>\n", argv[0]); exit(0); } bzero(&servaddr, sizeof(servaddr)); if ((listenfd = socket(PF_INET, SOCK_STREAM, 0)) == -1) { perror("socket"); exit(-1); } servaddr.sin_family = PF_INET; servaddr.sin_port = htons(atoi(argv[2])); servaddr.sin_addr.s_addr = inet_addr(argv[1]); if (bind(listenfd, (SA *)&servaddr, sizeof(servaddr)) == -1) { perror("bind"); exit(-1); } listen(listenfd, 5); maxfd = listenfd; FD_ZERO(&rdfs); while (1) { FD_SET(0, &rdfs); FD_SET(listenfd, &rdfs); if (select(maxfd+1, &rdfs, NULL, NULL, NULL) == -1) { perror("select"); exit(-1); } for (i = 0; i <= maxfd; i++) { if (FD_ISSET(i, &rdfs)) { if (i == 0) { fgets(buf, N, stdin); printf("*************\n"); printf("%s", buf); } else if (i == listenfd) { len = sizeof(peeraddr); if ((connfd = accept(listenfd, (SA *)&peeraddr, &len)) == -1) { perror("accept"); exit(-1); } fprintf(stdout, "welcome %s %d\n", inet_ntoa(peeraddr.sin_addr), ntohs(peeraddr.sin_port)); close(connfd); } } } } exit(0); }
#include <stdio.h> #include <unistd.h> #include <string.h> #include <stdlib.h> #include <sys/types.h> #include <sys/socket.h> #include <errno.h> #include <strings.h> #include <netinet/in.h> #include <arpa/inet.h> typedef struct sockaddr SA; #define N 64 int main(int argc, char *argv[]) { int sockfd; ssize_t n; struct sockaddr_in servaddr; char buf[N] = {0}; if (argc < 3) { fprintf(stdout, "usage:%s ip port\n", argv[0]); exit(0); } if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1) { perror("socket"); exit(-1); } bzero(&servaddr, sizeof(servaddr)); servaddr.sin_family = PF_INET; servaddr.sin_port = htons(atoi(argv[2]));// "9000"---9000 servaddr.sin_addr.s_addr = inet_addr(argv[1]); if (connect(sockfd, (SA *)&servaddr, sizeof(servaddr)) == -1) { perror("connect"); exit(-1); } printf(">"); while (fgets(buf, N, stdin) != NULL)//abc\n { buf[strlen(buf)-1] = 0;//abc\0 send(sockfd, buf, N, 0); bzero(buf, sizeof(buf)); n = recv(sockfd, buf, N, 0); printf("n=%d buf=%s\n", n, buf); printf(">"); } close(sockfd); exit(0); }
#include <stdio.h> #include <signal.h> #include <unistd.h> #include <stdlib.h> #include <sys/types.h> #include <sys/socket.h> #include <errno.h> #include <strings.h> #include <netinet/in.h> #include <arpa/inet.h> typedef struct sockaddr SA; voidf(int sig) {printf("*\n");} int main(int argc, char *argv[]) { int listenfd, connfd; struct sockaddr_in myaddr, peeraddr; socklen_t len; if (argc < 3) { fprintf(stdout, "usage:%s ip port\n", argv[0]); exit(0); } if ((listenfd = socket(PF_INET, SOCK_STREAM, 0)) == -1) { perror("socket"); exit(-1); } bzero(&myaddr, sizeof(myaddr)); myaddr.sin_family = PF_INET; myaddr.sin_port = htons(atoi(argv[2]));//htons(9000) myaddr.sin_addr.s_addr = inet_addr(argv[1]); int on = 1; if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) == -1) { perror("setsockopt"); exit(-1); } if (bind (listenfd, (SA *)&myaddr, sizeof(myaddr)) == -1) { perror("bind"); exit(-1); } if (listen(listenfd, 5) == -1) { perror("listen"); exit(-1); } bzero(&peeraddr, sizeof(peeraddr)); len = sizeof(peeraddr); struct timeval t={5, 0}; if (setsockopt(listenfd, SOL_SOCKET, SO_RCVTIMEO, &t, sizeof(t)) == -1) { perror("setsockopt"); exit(-1); } while (1) { if ((connfd = accept(listenfd, (SA *)&peeraddr, &len)) == -1) { printf("%d\n", errno); exit(-1); } printf("welcome %s:%d\n", inet_ntoa(peeraddr.sin_addr), ntohs(peeraddr.sin_port)); close(connfd); } exit(0); }
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <sys/types.h> /* See NOTES */ #include <sys/time.h> #include <unistd.h> #include <sys/select.h> #include <sys/socket.h> #include <errno.h> #include <strings.h> #include <netinet/in.h> #include <arpa/inet.h> #define N 64 typedef struct sockaddr SA; int main(int argc, char *argv[]) { int listenfd, connfd, maxfd, i; struct sockaddr_in servaddr, peeraddr; socklen_t len; char buf[N] = {0}; fd_set rdfs; if (argc < 3) { fprintf(stdout, "usage:%s <ip> <port>\n", argv[0]); exit(0); } bzero(&servaddr, sizeof(servaddr)); if ((listenfd = socket(PF_INET, SOCK_STREAM, 0)) == -1) { perror("socket"); exit(-1); } servaddr.sin_family = PF_INET; servaddr.sin_port = htons(atoi(argv[2])); servaddr.sin_addr.s_addr = inet_addr(argv[1]); if (bind(listenfd, (SA *)&servaddr, sizeof(servaddr)) == -1) { perror("bind"); exit(-1); } listen(listenfd, 5); maxfd = listenfd; int n; FD_ZERO(&rdfs); while (1) { struct timeval t = {5, 0}; FD_SET(0, &rdfs); FD_SET(listenfd, &rdfs); if ((n = select(maxfd+1, &rdfs, NULL, NULL, &t)) == -1) { perror("select"); exit(-1); } printf("n=%d\n", n); for (i = 0; i <= maxfd; i++) { if (FD_ISSET(i, &rdfs)) { if (i == 0) { fgets(buf, N, stdin); printf("*************\n"); printf("%s", buf); } else if (i == listenfd) { len = sizeof(peeraddr); if ((connfd = accept(listenfd, (SA *)&peeraddr, &len)) == -1) { perror("accept"); exit(-1); } fprintf(stdout, "welcome %s %d\n", inet_ntoa(peeraddr.sin_addr), ntohs(peeraddr.sin_port)); close(connfd); } } } } exit(0); }
#include <stdio.h> #include <signal.h> #include <unistd.h> #include <stdlib.h> #include <sys/types.h> #include <sys/socket.h> #include <errno.h> #include <strings.h> #include <netinet/in.h> #include <arpa/inet.h> typedef struct sockaddr SA; void f(int sig) { printf("signo=%d\n", sig); alarm(5); } int main(int argc, char *argv[]) { int listenfd, connfd; struct sockaddr_in myaddr, peeraddr; socklen_t len; if (argc < 3) { fprintf(stdout, "usage:%s ip port\n", argv[0]); exit(0); } if ((listenfd = socket(PF_INET, SOCK_STREAM, 0)) == -1) { perror("socket"); exit(-1); } bzero(&myaddr, sizeof(myaddr)); myaddr.sin_family = PF_INET; myaddr.sin_port = htons(atoi(argv[2]));//htons(9000) myaddr.sin_addr.s_addr = inet_addr(argv[1]); if (bind (listenfd, (SA *)&myaddr, sizeof(myaddr)) == -1) { perror("bind"); exit(-1); } if (listen(listenfd, 5) == -1) { perror("listen"); exit(-1); } bzero(&peeraddr, sizeof(peeraddr)); len = sizeof(peeraddr); // signal(SIGALRM, f); struct sigaction act; sigaction(SIGALRM, NULL, &act); act.sa_handler = f; sigaction(SIGALRM, &act, NULL); printf("**\n"); while (1) { alarm(5); if ((connfd = accept(listenfd, (SA *)&peeraddr, &len)) == -1) { printf("%d\n", errno); exit(-1); } printf("welcome %s:%d\n", inet_ntoa(peeraddr.sin_addr), ntohs(peeraddr.sin_port)); close(connfd); } exit(0); }
#include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <sys/types.h> #include <sys/socket.h> #include <errno.h> #include <strings.h> #include <string.h> #include <netinet/in.h> #include <arpa/inet.h> typedef struct sockaddr SA; #define N 64 int main(int argc, char *argv[]) { int sockfd; struct sockaddr_in myaddr, peeraddr; socklen_t len; char buf[N] = {0}; if (argc < 3) { fprintf(stdout, "usage:%s ip port\n", argv[0]); exit(0); } if ((sockfd = socket(PF_INET, SOCK_DGRAM, 0)) == -1) { perror("socket"); exit(-1); } bzero(&myaddr, sizeof(myaddr)); myaddr.sin_family = PF_INET; myaddr.sin_port = htons(atoi(argv[2]));//6000 myaddr.sin_addr.s_addr = inet_addr(argv[1]);//0.0.0.0 192.168.1.255 if (bind (sockfd, (SA *)&myaddr, sizeof(myaddr)) == -1) { perror("bind"); exit(-1); } len = sizeof(peeraddr); bzero(&peeraddr, sizeof(peeraddr)); while (1) { bzero(buf, sizeof(buf)); if (-1 == recvfrom(sockfd, buf, N, 0, (SA *)&peeraddr, &len)) { printf("errno=%d %s\n", errno, strerror(errno)); exit(-1); } printf("from %s:%d %s\n", inet_ntoa(peeraddr.sin_addr), ntohs(peeraddr.sin_port), buf); } exit(0); }
#include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <sys/types.h> #include <sys/socket.h> #include <errno.h> #include <strings.h> #include <string.h> #include <netinet/in.h> #include <arpa/inet.h> typedef struct sockaddr SA; #define N 64 int main(int argc, char *argv[]) { int sockfd; struct sockaddr_in servaddr; char buf[N] = {0}; if (argc < 3) { fprintf(stdout, "usage:%s ip port\n", argv[0]); exit(0); } if ((sockfd = socket(PF_INET, SOCK_DGRAM, 0)) == -1) { perror("socket"); exit(-1); } int on = 1; if (setsockopt(sockfd, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on)) == -1) { perror("setsockopt"); exit(-1); } bzero(&servaddr, sizeof(servaddr)); servaddr.sin_family = PF_INET; servaddr.sin_port = htons(atoi(argv[2]));// 6000 servaddr.sin_addr.s_addr = inet_addr(argv[1]);//192.168.1.255 strcpy(buf, "this is a broadcast package"); while (1) { sendto(sockfd, buf, N, 0, (SA *)&servaddr, sizeof(servaddr)); sleep(1); } close(sockfd); exit(0); }
#include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <sys/types.h> #include <sys/socket.h> #include <errno.h> #include <strings.h> #include <string.h> #include <netinet/in.h> #include <arpa/inet.h> typedef struct sockaddr SA; #define N 64 int main(int argc, char *argv[]) { int sockfd; struct sockaddr_in servaddr; char buf[N] = {0}; if (argc < 3) { fprintf(stdout, "usage:%s ip port\n", argv[0]); exit(0); } if ((sockfd = socket(PF_INET, SOCK_DGRAM, 0)) == -1) { perror("socket"); exit(-1); } bzero(&servaddr, sizeof(servaddr)); servaddr.sin_family = PF_INET; servaddr.sin_port = htons(atoi(argv[2]));// 6000 servaddr.sin_addr.s_addr = inet_addr(argv[1]);//224.10.10.1 strcpy(buf, "this is a multicast package"); while (1) { sendto(sockfd, buf, N, 0, (SA *)&servaddr, sizeof(servaddr)); sleep(1); } close(sockfd); exit(0); }
#include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <sys/types.h> #include <sys/socket.h> #include <errno.h> #include <strings.h> #include <string.h> #include <netinet/in.h> #include <arpa/inet.h> typedef struct sockaddr SA; #define N 64 int main(int argc, char *argv[]) { int sockfd; struct sockaddr_in myaddr, peeraddr; socklen_t len; char buf[N] = {0}; struct ip_mreq mreq; if (argc < 3) { fprintf(stdout, "usage:%s ip port\n", argv[0]); exit(0); } if ((sockfd = socket(PF_INET, SOCK_DGRAM, 0)) == -1) { perror("socket"); exit(-1); } bzero(&mreq, sizeof(mreq)); mreq.imr_multiaddr.s_addr = inet_addr("224.10.10.1"); mreq.imr_interface.s_addr = htonl(INADDR_ANY); if (setsockopt(sockfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) == -1) { perror("setsockopt"); exit(-1); } bzero(&myaddr, sizeof(myaddr)); myaddr.sin_family = PF_INET; myaddr.sin_port = htons(atoi(argv[2]));//6000 myaddr.sin_addr.s_addr = inet_addr(argv[1]);//0.0.0.0 224.10.10.1 if (bind (sockfd, (SA *)&myaddr, sizeof(myaddr)) == -1) { perror("bind"); exit(-1); } len = sizeof(peeraddr); bzero(&peeraddr, sizeof(peeraddr)); while (1) { bzero(buf, sizeof(buf)); if (-1 == recvfrom(sockfd, buf, N, 0, (SA *)&peeraddr, &len)) { printf("errno=%d %s\n", errno, strerror(errno)); exit(-1); } printf("from %s:%d %s\n", inet_ntoa(peeraddr.sin_addr), ntohs(peeraddr.sin_port), buf); } exit(0); }
#include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <sys/types.h> #include <sys/socket.h> #include <errno.h> #include <strings.h> #include <sys/un.h> typedef struct sockaddr SA; #define N 64 int main(int argc, char *argv[]) { int listenfd, connfd; struct sockaddr_un myaddr, peeraddr; socklen_t len; char buf[N] = {0}; ssize_t n; if ((listenfd = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) { perror("socket"); exit(-1); } bzero(&myaddr, sizeof(myaddr)); myaddr.sun_family = PF_UNIX; strcpy(myaddr.sun_path, "serversocket"); unlink("serversocket"); if (bind (listenfd, (SA *)&myaddr, sizeof(myaddr)) == -1) { perror("bind"); exit(-1); } if (listen(listenfd, 5) == -1) { perror("listen"); exit(-1); } bzero(&peeraddr, sizeof(peeraddr)); len = sizeof(peeraddr); while (1) { if ((connfd = accept(listenfd, (SA *)&peeraddr, &len)) == -1) { perror("accept"); exit(-1); } printf("welcome %s\n", peeraddr.sun_path); while (1) { bzero(buf, sizeof(buf)); if ((n = recv(connfd, buf, N, 0)) == 0) break; send(connfd, buf, N, 0); } close(connfd); } exit(0); }
#include <stdio.h> #include <unistd.h> #include <string.h> #include <stdlib.h> #include <sys/types.h> #include <sys/socket.h> #include <errno.h> #include <strings.h> #include <sys/un.h> typedef struct sockaddr SA; #define N 64 int main(int argc, char *argv[]) { int sockfd; ssize_t n; struct sockaddr_un servaddr,myaddr; char buf[N] = {0}; if ((sockfd = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) { perror("socket"); exit(-1); } #if 1 bzero(&myaddr, sizeof(myaddr)); myaddr.sun_family = PF_UNIX; strcpy(myaddr.sun_path, "clientsocket"); unlink("clientsocket"); if (bind (sockfd, (SA *)&myaddr, sizeof(myaddr)) == -1) { perror("bind"); exit(-1); } #endif bzero(&servaddr, sizeof(servaddr)); servaddr.sun_family = PF_UNIX; strcpy(servaddr.sun_path, "serversocket"); if (connect(sockfd, (SA *)&servaddr, sizeof(servaddr)) == -1) { perror("connect"); exit(-1); } printf(">"); while (fgets(buf, N, stdin) != NULL)//abc\n { buf[strlen(buf)-1] = 0;//abc\0 send(sockfd, buf, N, 0); bzero(buf, sizeof(buf)); n = recv(sockfd, buf, N, 0); printf("n=%d buf=%s\n", n, buf); printf(">"); } close(sockfd); exit(0); }
#include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <sys/types.h> #include <sys/socket.h> #include <errno.h> #include <strings.h> #include <string.h> #include <sys/un.h> typedef struct sockaddr SA; #define N 64 int main(int argc, char *argv[]) { int sockfd; struct sockaddr_un servaddr, myaddr; socklen_t len; char buf[N] = {0}; if ((sockfd = socket(PF_UNIX, SOCK_DGRAM, 0)) == -1) { perror("socket"); exit(-1); } bzero(&myaddr, sizeof(myaddr)); myaddr.sun_family = PF_UNIX; strcpy(myaddr.sun_path, "clientsocket"); unlink("clientsocket"); if (bind (sockfd, (SA *)&myaddr, sizeof(myaddr)) == -1) { perror("bind"); exit(-1); } bzero(&servaddr, sizeof(servaddr)); servaddr.sun_family = PF_UNIX; strcpy(servaddr.sun_path, "serversocket"); printf(">"); while (fgets(buf, N, stdin) != NULL) { buf[strlen(buf)-1] = 0; sendto(sockfd, buf, N, 0, (SA *)&servaddr, sizeof(servaddr)); bzero(buf, sizeof(buf)); recvfrom(sockfd, buf, N, 0, NULL, NULL); printf("%s\n", buf); printf(">"); } close(sockfd); exit(0); }
#include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <sys/types.h> #include <sys/socket.h> #include <errno.h> #include <strings.h> #include <string.h> #include <sys/un.h> typedef struct sockaddr SA; #define N 64 int main(int argc, char *argv[]) { int sockfd; struct sockaddr_un myaddr, peeraddr; socklen_t len; char buf[N] = {0}; if ((sockfd = socket(PF_UNIX, SOCK_DGRAM, 0)) == -1) { perror("socket"); exit(-1); } bzero(&myaddr, sizeof(myaddr)); myaddr.sun_family = PF_UNIX; strcpy(myaddr.sun_path, "serversocket"); unlink("serversocket"); if (bind (sockfd, (SA *)&myaddr, sizeof(myaddr)) == -1) { perror("bind"); exit(-1); } len = sizeof(peeraddr); bzero(&peeraddr, sizeof(peeraddr)); while (1) { bzero(buf, sizeof(buf)); if (-1 == recvfrom(sockfd, buf, N, 0, (SA *)&peeraddr, &len)) { printf("errno=%d %s\n", errno, strerror(errno)); exit(-1); } printf("from %s: %s\n", peeraddr.sun_path, buf); sendto(sockfd, buf, N, 0, (SA *)&peeraddr, sizeof(peeraddr)); } exit(0); }