linux socket源代码 可用 网上我没…
2013-07-10 22:07
381 查看
//server.c - a server, datagram sockets #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <errno.h> #include <string.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> // the port users will be connecting to #define MYPORT 9989 #define MAXBUFLEN 500 int main(int argc, char *argv[]) { int sockfd; // my address information struct sockaddr_in my_addr; // connector’s address information struct sockaddr_in their_addr; int addr_len, numbytes; char buf[MAXBUFLEN]; if((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) { perror("Server-socket() sockfd error lol!"); exit(1); } else printf("Server-socket() sockfd is OK...\n"); // host byte order my_addr.sin_family = AF_INET; // short, network byte order my_addr.sin_port = htons(MYPORT); // automatically fill with my IP my_addr.sin_addr.s_addr = INADDR_ANY; // zero the rest of the struct memset(&(my_addr.sin_zero), '\0', 8); if(bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1) { perror("Server-bind() error lol!"); exit(1); } else printf("Server-bind() is OK...\n"); addr_len = sizeof(struct sockaddr); while (1) { if((numbytes = recvfrom(sockfd, buf, MAXBUFLEN-1, 0, (struct sockaddr *)&their_addr, (socklen_t *)&addr_len)) == -1) { perror("Server-recvfrom() error lol!"); //If something wrong, just exit lol... exit(1); } else { printf("Server-Waiting and listening...\n"); printf("Server-recvfrom() is OK...\n"); } printf("Server-Got packet from %s\n", inet_ntoa(their_addr.sin_addr)); printf("Server-Packet is %d bytes long\n", numbytes); buf[numbytes] = '\0'; printf("Server-Packet contains \"%s\"\n", buf); } if(close(sockfd) != 0) printf("Server-sockfd closing failed!\n"); else printf("Server-sockfd successfully closed!\n"); return 0; } | //client.c - a client, datagram #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <errno.h> #include <string.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <netdb.h> // the port users will be connecting to #define MYPORT 9989 int main(int argc, char *argv[]) { int sockfd; // connector’s address information struct sockaddr_in their_addr; struct hostent *he; int numbytes; if (argc != 3) { fprintf(stderr, "Client-Usage: %s <hostname> <message>\n", argv[0]); exit(1); } // get the host info if ((he = gethostbyname(argv[1])) == NULL) { perror("Client-gethostbyname() error lol!"); exit(1); } else printf("Client-gethostname() is OK...\n"); if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) { perror("Client-socket() error lol!"); exit(1); } else printf("Client-socket() sockfd is OK...\n"); // host byte order their_addr.sin_family = AF_INET; // short, network byte order printf("Using port: 4950\n"); their_addr.sin_port = htons(MYPORT); their_addr.sin_addr = *((struct in_addr *) he->h_addr); // zero the rest of the struct memset(&(their_addr.sin_zero), '\0', 8); if ((numbytes = sendto(sockfd, argv[2], strlen(argv[2]), 0, (struct sockaddr *) &their_addr, sizeof(struct sockaddr))) == -1) { perror("Client-sendto() error lol!"); exit(1); } else printf("Client-sendto() is OK...\n"); printf("sent %d bytes to %s\n", numbytes, inet_ntoa(their_addr.sin_addr)); if (close(sockfd) != 0) printf("Client-sockfd closing is failed!\n"); else printf("Client-sockfd successfully closed!\n"); return 0; } |
相关文章推荐
- Linux系统&nbsp;阻塞socket遇到网络故障
- Linux Socket编程(转载)
- Linux&nbsp;Socket编程(不限Linux)
- Linux&nbsp;网络栈剖析--从&nbsp;socket&nbsp;到设…
- linux socket编程TCP&UDP
- 红帽&nbsp;LINUX&nbsp;6自学笔记第三章(1)&nbsp;…
- linux powerpc i2c驱动 之 i2c设备…
- Linux&nbsp;磁盘的分割、格式化、…
- Linux命令之rz - 批量上…
- Linux&nbsp;vmstat命令实战详解
- Linux安装Memcached服务及php&nbsp…
- linux sed命令详解
- Linux&nbsp;下安装jdk+tomca…
- 转:详述Linux后台运行命令&nbsp;&amp;
- 2011-04-12 18:19 Linux…
- 【转】Linux C语言头文件搜索路径
- linux qq 的安装与卸载(转…
- Socket&Linux方面的SOCKET资料
- kali linux的http
- 去掉linux&nbsp;mint和ubuntu下的&amp;…