Linux网络编程--聊天室客户端程序
来源:互联网 发布:如何年入百万 知乎 编辑:程序博客网 时间:2024/04/26 03:05
聊天室客户端程序
#define _GNU_SOURCE 1#include <sys/types.h>#include <sys/socket.h>#include <netinet/in.h>#include <arpa/inet.h>#include <assert.h>#include <stdio.h>#include <unistd.h>#include <string.h>#include <stdlib.h>#include <poll.h>#include <fcntl.h>#define BUFFER_SIZE 64int main( int argc, char* argv[] ){ if( argc <= 2 ) { printf( "usage: %s ip_address port_number\n", basename( argv[0] ) ); return 1; } const char* ip = argv[1]; int port = atoi( argv[2] ); struct sockaddr_in server_address; bzero( &server_address, sizeof( server_address ) ); server_address.sin_family = AF_INET; inet_pton( AF_INET, ip, &server_address.sin_addr ); server_address.sin_port = htons( port ); int sockfd = socket( PF_INET, SOCK_STREAM, 0 ); assert( sockfd >= 0 ); if ( connect( sockfd, ( struct sockaddr* )&server_address, sizeof( server_address ) ) < 0 ) { printf( "connection failed\n" ); close( sockfd ); return 1; } struct pollfd fds[2]; fds[0].fd = 0; fds[0].events = POLLIN; fds[0].revents = 0; fds[1].fd = sockfd; fds[1].events = POLLIN | POLLRDHUP; fds[1].revents = 0; char read_buf[BUFFER_SIZE]; int pipefd[2]; int ret = pipe( pipefd ); assert( ret != -1 ); while( 1 ) { ret = poll( fds, 2, -1 ); if( ret < 0 ) { printf( "poll failure\n" ); break; } if( fds[1].revents & POLLRDHUP ) { printf( "server close the connection\n" ); break; } else if( fds[1].revents & POLLIN ) { memset( read_buf, '\0', BUFFER_SIZE ); recv( fds[1].fd, read_buf, BUFFER_SIZE-1, 0 ); printf( "%s\n", read_buf ); } if( fds[0].revents & POLLIN ) { ret = splice( 0, NULL, pipefd[1], NULL, 32768, SPLICE_F_MORE | SPLICE_F_MOVE ); ret = splice( pipefd[0], NULL, sockfd, NULL, 32768, SPLICE_F_MORE | SPLICE_F_MOVE ); } } close( sockfd ); return 0;}
测试1
开启服务端侦听:nc -l 6789
运行客户端程序:./a.out 127.0.0.1 6789
[fes@fes ~]$ nc -l 6789Hello WorldHi Boy hi gils[fes@fes test]$ ./a.out 127.0.0.1 6789Hello WorldHi Boy hi gils
测试2
[fes@fes test]$ ./a.out 202.108.22.5 80GET / HTTP/1.0HTTP/1.1 200 OKDate: Tue, 28 Oct 2014 08:42:21 GMTContent-Type: text/htmlContent-Length: 14613Last-Modified: Wed, 03 Sep 2014 02:48:32 GMTConnection: CloseVary: Accept-EncodingSet-Cookie: BAIDUID=BB394409C2863298A47B3D23F81817CC:FG=1; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.comSet-Cookie: BAIDUPSID=BB394409C2863298A47B3D23F81817CC; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.comSet-Cookie: BDSVRTM=0; path=/P3P: CP=" OTI DSP COR IVA OUR IND COM "Server: BWS/1.1Pragma: no-cacheCache-control: no-cacheBDPAGETYPE: 1BDQID: 0xac1b0f5500017ba2BDUSERID: 0Accept-Ranges: bytes<!DOCTYPE html><!--STATUS OK--><html><head><meta http-equiv="content-type" content="text/html;charset=utf-8"><meta http-equiv="X-UA-Compatible" content="IE=Edge"><link rel="dns-prefetch" href="//s1.bdstatic.com"/><link rel="dns-prefetch" href="//t1.baidu.com"/><link rel="dns-prefetch" href="//t2.baidu.com"/><link rel="dns-prefetch" href="//t3.baidu.com"/><link rel="dns-prefetch" href="//t10.baidu.com"/><link rel="dns-prefetch" href="//t11.baidu.com"/><link rel="dns-prefetch" href="//t12.baidu.com"/><link rel="dns-prefetch" href="//b1.bdstatic.com"/>
......
客户端连接百度,实现HTTP测试
程序收获
1、poll函数在IO复用中的应用2、splice函数高效复制
3、nc命令在网络中的应用
0 0
- Linux网络编程--聊天室客户端程序
- 基于Linux C的聊天室客户端(四)网络编程
- java网络编程之聊天室客户端(二)
- 网络编程之即时通信程序(聊天室)------(三)客户端登录
- 网络编程之即时通信程序(聊天室)------(四)客户端搭建
- Linux C 网络编程——多线程的聊天室实现(客户端)
- 网络编程-UDP协议---简单聊天室程序
- 网络编程_多线程实现聊天室程序
- SOCKET网络编程:Linux下实现聊天室
- linux网络编程十六:I/O复用的应用-poll简单实现聊天室程序
- Linux网络编程--使用epoll,共享内存技术实现高性能的聊天室程序
- Linux网络编程--使用epoll,共享内存技术实现高性能的聊天室程序
- 聊天室程序客户端
- [Windows网络编程课设]南工程聊天室客户端源码
- Linux网络编程一步一步学-HTTPS客户端程序示例
- Linux网络编程一步一步学-HTTPS客户端程序示例
- Linux网络编程一步一步学-HTTPS客户端程序示例
- 【Linux网络编程】基于UDP的客户端/服务器程序
- UITableView_UITableViewCel
- zookeeper 使用(一)
- CCProgressTimer进度条
- Web.config配置文件详解
- linux-3.4.2内核移植
- Linux网络编程--聊天室客户端程序
- RedHat Enterprise Linux6.4 使用CentOS yum源
- innobackupex存储备份历史
- HDU 1029 Ignatius and the Princess IV
- Jboss EJB实现实例
- js解析json数据
- TableViewController_01
- intent intent-filter
- oracle树形结构由子节点递归得到父节点