Linux Network Programming--Chat room client program

Source: Internet
Author: User
Tags assert prefetch

Chat Room client program

#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 ("Co NnectiOn 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, ' + ', 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_mor E |            Splice_f_move); RET = SpliCE (pipefd[0], NULL, SOCKFD, NULL, 32768, Splice_f_more |        Splice_f_move);    }} close (SOCKFD); return 0;}

Test 1turn on the server to listen: Nc-l 6789Run the client program:./a.out 127.0.0.1 6789
[Email protected] ~]$ nc-l 6789Hello worldhi boy hi gils[[email protected] test]$./a.out 127.0.0.1 6789Hello Worldhi Bo Y Hi gils
Test 2
[[email protected] test]$./a.out 202.108.22.5 80get/http/1.0http/1.1, Oct okdate:tue 08:42:21 ent-type:text/htmlcontent-length:14613last-modified:wed, 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:0xa c1b0f5500017ba2bduserid:0accept-ranges:bytes<! DOCTYPE html><!--STATUS ok-->
......

Client Connection Baidu, implementing HTTP testProgram Harvesting1. Application of poll function in IO multiplexing2. Efficient replication of splice functions3, the application of NC command in the network




Linux Network Programming--Chat room client program

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.