09.网络编程之二

来源:互联网 发布:黑蚂蚁成人的网络电视 编辑:程序博客网 时间:2024/06/13 17:08

【代码1】header.h

#include <stdio.h>#include <stdlib.h>#include <string.h>#include <errno.h>#include <sys/types.h>          /* See NOTES */#include <sys/socket.h>#include <netinet/in.h>#include <netinet/ip.h> /* superset of previous */#include <unistd.h>#include <arpa/inet.h>#define err_exit(function)\do {\fprintf(stderr, "line %d in %s at %s--->%s : %s\n", __LINE__ - 1, __FUNCTION__, __FILE__, function, strerror(errno)); \exit(EXIT_FAILURE);\} while(0)

【代码2】test_get_dir_content.c

#include <stdio.h>#include <sys/stat.h>#include <fcntl.h>#include <stdlib.h>#include <string.h>#include <unistd.h>#include <sys/types.h>#include <errno.h>int main(int argc, char *argv[]){system("ls > dir.txt");int fd = -1;if (0 > (fd = open("dir.txt", O_RDONLY))) {fprintf(stderr, "open : %s\n", strerror(errno));exit(1);}int ret;char buf[10];char output[10];while((ret = read(fd, buf, sizeof(buf) - 1))) {/* the last byte for '\0' */memset(output, 0, sizeof(output));memcpy(output, buf, ret);printf("%s", output);}exit(0);}

【代码3】test_getfilename.c

#include <stdio.h>#include <stdlib.h>#include <string.h>#include <errno.h>int main(void){char buf[128];char *p = NULL;fgets(buf, sizeof(buf), stdin);if (NULL == (p = strchr(buf, ':'))) {fprintf(stderr, "strchr : %s\n", strerror(errno));exit(1);}printf("addr of buf is %p, addr of first ':' is %p\n", buf, p);char filename[256];strncpy(filename, p + 1, strlen(buf) + 1 - (p - buf + 1));filename[strlen(filename)-1] = '\0';printf("the file name is %s, its length is %d\n", filename, strlen(filename));exit(0);}

【代码4】test_tcp_client.c

#include "header.h"int main(int argc, char *argv[]){int connfd = -1;if (0 > (connfd = socket(AF_INET, SOCK_STREAM, 0)))err_exit("socket");struct sockaddr_in server;bzero(&server, sizeof(server));server.sin_family = AF_INET;server.sin_port = htons(8888);server.sin_addr.s_addr = inet_addr("192.168.80.128");if ( 0 > connect(connfd, (struct sockaddr *)&server, sizeof(server)))err_exit("connect");printf("connect success!\n");char buf[128];int ret;while(1) {ret = recv(connfd, buf, sizeof(buf), 0);if (ret < 0)err_exit("recv");else if (ret == 0) {printf("server quit!\n");break;}printf("%s", buf);fgets(buf, sizeof(buf), stdin);send(connfd, buf, strlen(buf) + 1, 0);}close(connfd);exit(0);}

【代码5】test_tcp_server.c

#include "header.h"int main(int argc, char *argv[]){int listenfd = -1;if (0 > (listenfd = socket(AF_INET, SOCK_STREAM, 0)))err_exit("socket");struct sockaddr_in server, client;bzero(&server, sizeof(server));server.sin_family = AF_INET;server.sin_port = htons(8888);/* server.sin_addr.s_addr = inet_addr("192.168.80.128"); *//* inet_aton("192.168.80.128", &server.sin_addr); */server.sin_addr.s_addr = htonl(INADDR_ANY);/* server.sin_addr.s_addr = inet_addr("0");  */if (0 > bind(listenfd, (struct sockaddr *)&server, sizeof(server)))err_exit("bind");listen(listenfd, 1024);printf("listen...\n");bzero(&client, sizeof(client));socklen_t len = sizeof(client);  // must be done!int connfd = -1;if ( 0 > (connfd = accept(listenfd, (struct sockaddr *)&client, &len)))err_exit("accept");printf("client ip is %s, port number is %d\n", inet_ntoa(client.sin_addr), ntohs(client.sin_port));char buf[128];int ret;while(1) {fgets(buf, sizeof(buf), stdin);send(connfd, buf, strlen(buf) + 1, 0);ret = recv(connfd, buf, sizeof(buf), 0);if (ret < 0)err_exit("recv");else if (ret == 0) {printf("server quit!\n");break;}printf("client : %s", buf);}close(listenfd);close(connfd);exit(0);}


原创粉丝点击