socket——tcp-nonblocking-server-client
来源:互联网 发布:js encodeuri gbk 编辑:程序博客网 时间:2024/05/21 23:20
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
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;
- }
0
上一篇:linux getline 忽略行尾换行符—strip trailing newline
下一篇:pthread_create—pthread_cancel—pthread_join
相关热门文章
- test123
- 编写安全代码——小心有符号数...
- 使用openssl api进行加密解密...
- 一段自己打印自己的c程序...
- sql relay的c++接口
- linux dhcp peizhi roc
- 关于Unix文件的软链接
- 求教这个命令什么意思,我是新...
- sed -e "/grep/d" 是什么意思...
- 谁能够帮我解决LINUX 2.6 10...
给主人留下些什么吧!~~
评论热议
0 0
- socket——tcp-nonblocking-server-client
- TCP Socket Server Client
- TCP Server《——》TCP Client
- epoll ET mode with tcp nonblocking socket
- Simple TCP Server Client Socket C
- Socket编程—Client、Server通信
- Python: 使用select函数编写nonblocking TCP/IP socket程序
- C socket TCP/UDP client/server example codes
- Qt TCP之Server/Client/Socket信号、槽等总结
- 网络套接字socket(tcp、server、client)
- TCP(socket vc) client
- socket client(tcp)
- tcp python socket client
- java基础学习笔记——网络TCP/Client&Server
- TCP的Server和Client通信—Java实现
- (OK) Linux epoll模型—socket epoll server client chat
- (OK) Linux epoll模型—socket epoll server client chat
- java Tcp Client server
- 操作系统的作业调度算法
- Ehcache(二) xml配置详解
- 操作系统的内存分配
- top -bcn 1
- linux getline 忽略行尾换行符—strip trailing newline
- socket——tcp-nonblocking-server-client
- pthread_create—pthread_cancel—pthread_join
- Evaluating Network Simulation Tools—CORE—NS3—MANET
- POJ 3461
- How to Connect GNS3 to VirtualBox—CORE—NS3—MANET
- How To Install Docker on CentOS 7
- 《Python核心编程》第十章:错误和异常
- ubable to compile class for jsp
- Docker—从入门到实践—Welcome to the Docker user guide
原创粉丝点击
热门IT博客
热门问题
老师的惩罚
人脸识别
我在镇武司摸鱼那些年
重生之率土为王
我在大康的咸鱼生活
盘龙之生命进化
天生仙种
凡人之先天五行
春回大明朝
姑娘不必设防,我是瞎子
柠檬绿茶图片
孕妇可以喝柠檬红茶吗
柠檬红茶好处
早上一杯蜂蜜柠檬水
什么时候喝蜂蜜柠檬水好
柠檬茶的做法
柠檬茶功效
柠檬百香果蜂蜜茶
百香果柠檬茶
蜂蜜柠檬茶
自制柠檬茶
柠檬茶图片
柠檬茶怎么做
柠檬百香果蜂蜜茶做法
蜂蜜柠檬茶做法
柠檬百香果蜂蜜茶功效
柠檬茶的危害
孕妇可以喝柠檬茶吗
菊花柠檬茶
扶她柠檬茶
速溶柠檬茶
柠檬普洱茶功效与作用
怎样做柠檬茶
蜂蜜柠檬茶的做法
港式柠檬茶的做法
柠檬茶用什么茶
维他柠檬茶图片
柠檬茶怎样做
自制柠檬茶的做法
柠檬茶减肥吗
手摇柠檬茶
柠檬茶的做法大全
柠檬茶怎么做才好喝
如何做柠檬茶
檸檬茶
维他柠檬茶价格
如何泡柠檬茶
柠檬茶价格
自制柠檬蜂蜜茶
柠檬茶饮料
玫瑰柠檬茶的功效