获取服务器时间
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <netdb.h> #define HOSTNAMELEN 40 #define BUFLEN 1024 #define PORT 13 int main(int argc, char *argv[]) { int rc; int sockfd; char buf[BUFLEN+1]; char *pc; struct sockaddr_in sa; struct hostent *hen; if (argc < 2) { fprintf(stderr, "missing host name\n"); exit(1); } hen = gethostbyname(argv[1]); if (!hen) { perror("could not resolve host name"); exit(1); } memset(&sa, 0, sizeof(sa)); sa.sin_family = AF_INET; sa.sin_port = htons(PORT); memcpy(&sa.sin_addr.s_addr, hen->h_addr_list[0], hen->h_length); sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd < 0) { perror("socket()"); exit(1); } rc = connect(sockfd, (struct sockaddr*)&sa, sizeof(sa)); if (rc < 0) { perror("connect()"); exit(1); } pc = buf; while (rc = read(sockfd, pc, BUFLEN - (pc-buf))) { pc += rc; } close(sockfd); *pc = ‘\0‘; printf("time: %s\n", buf); return 0; }
tcp服务器端
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #define PORT 8888 #define BACKLOG 1 int main() { int listenfd, connfd; struct sockaddr_in servaddr; struct sockaddr_in cliaddr; int sin_size; //socket if ((listenfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { perror("socket()"); exit(1); } //set socket can be reuse int opt = SO_REUSEADDR; setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)); //bind memset(&servaddr, 0, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr = htonl(INADDR_ANY); servaddr.sin_port = htons(PORT); if (bind(listenfd, (struct sockaddr*)&servaddr, sizeof(servaddr)) == -1) { perror("bind()"); exit(1); } //listen if (listen(listenfd, BACKLOG) == -1) { perror("listen()"); exit(1); } //accept sin_size = sizeof(cliaddr); if ((connfd = accept(listenfd, (struct sockaddr*)&cliaddr, &sin_size)) = = -1) { perror("accept()"); exit(1); } //print client ip printf("get a connection from %s\n", inet_ntoa(cliaddr.sin_addr)); //send char *msg = "welcome to server"; send(connfd, msg, strlen(msg), 0); //close close(connfd); close(listenfd); return 0; }
tcp客户端
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <netdb.h> #define PORT 8888 #define MAXDATASIZE 100 int main(int argc, char *argv[]) { int fd, numbytes; char buf[MAXDATASIZE]; struct hostent *he; struct sockaddr_in servaddr; if (argc != 2) { printf("usage: %s <ip>\n", argv[0]); exit(1); } if ((he = gethostbyname(argv[1])) == NULL) { perror("gethostbyname()"); exit(1); } if ((fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { perror("socket()"); exit(1); } bzero(&servaddr, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_port = htons(PORT); servaddr.sin_addr = *((struct in_addr *)he->h_addr); if (connect(fd, (struct sockaddr*)&servaddr, sizeof(servaddr)) == -1) { perror("connect()"); exit(1); } if ((numbytes = recv(fd, buf, MAXDATASIZE, 0)) == -1) { perror("recv()"); exit(1); } buf[numbytes] = 0; printf("server message: %s\n", buf); close(fd); return 0; }
udp服务器端
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #define PORT 8888 #define LINESIZE 100 int main() { int sockfd; struct sockaddr_in servaddr; struct sockaddr_in cliaddr; int sin_size; int num; char msg[LINESIZE]; if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) { perror("socket()"); exit(1); } memset(&servaddr, 0, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr = htonl(INADDR_ANY); servaddr.sin_port = htons(PORT); if (bind(sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr)) == -1) { perror("bind()"); exit(1); } sin_size = sizeof(cliaddr); while (1) { num = recvfrom(sockfd, msg, LINESIZE, 0, (struct sockaddr*)&cliaddr, &sin_size); if (num < 0) { perror("recvfrom()"); exit(1); } msg[num] = 0; printf("you got a message (%s) from %s\n", msg, inet_ntoa(cliaddr.sin_addr)); char *msg1 = "welcome to server."; sendto(sockfd, msg1, strlen(msg1), 0, (struct sockaddr*)&cliaddr, sin_size); if (!strcmp(msg, "quit")) break; } close(sockfd); return 0; }
udp客户端
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <netdb.h> #define PORT 8888 #define LINESIZE 100 int main(int argc, char *argv[]) { int fd, numbytes; char buf[LINESIZE]; struct hostent *he; struct sockaddr_in servaddr, reply; if (argc != 3) { printf("usage: %s <ip> <message>\n", argv[0]); exit(1); } if ((he = gethostbyname(argv[1])) == NULL) { perror("gethostbyname()"); exit(1); } if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) { perror("socket()"); exit(1); } bzero(&servaddr, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_port = htons(PORT); servaddr.sin_addr = *((struct in_addr*)he->h_addr); sendto(fd, argv[2], strlen(argv[2]), 0, (struct sockaddr*)&servaddr, sizeof(servaddr)); while (1) { int len; if ((numbytes = recvfrom(fd, buf, LINESIZE, 0, (struct sockaddr*)&reply, &len)) == -1) { perror("recvfrom()"); exit(1); } if (len != sizeof(reply) || memcmp((const void*)&servaddr, (const void*)& reply, len) != 0) { printf("receive message from other server.\n"); continue; } buf[numbytes] = 0; printf("server message: %s\n", buf); break; } close(fd); return 0; }
时间: 2024-10-24 19:00:49