Linux socket programming Get server time

Source: Internet
Author: User
Tags exit socket socket error port number linux

The client sends the request to the server, and the server receives the request to do the processing and returns the processing results back to the client. The following TCP protocol is used to implement the connection between the server and the client.

1. Client

The Protocol (UDP or TCP) is agreed between the parties, and the socket is created according to the transport Protocol;

The IP address and port number of the server;

connecting to the server;

Gets the data that the server passes back.

 #include <string.h> #include <sys/types.h> #include <sys/socket.h> #include <sys/ time.h> #include <time.h> #include <fcntl.h> #include <netinet/in.h> #include <a rpa/inet.h> #include <sys/errno.h> #include <iostream> #include <stdlib.h> #include    
<stdio.h> using namespace std;    
const int maxline=1024;    
    int main (int argc,char** argv) {int sockfd,n;    
    Char recvline[maxline+1];    
    struct sockaddr_in servaddr;    
        if (argc!=2) {cout<< "usage:a.out<ipaddress" <<endl;    
    Exit (0);    
    } sockfd=socket (af_inet,sock_stream,0);    
        if (sockfd<0) {cout<< "socket error" <<endl;    
    Exit (0);    
    } memset (&servaddr,0, sizeof (SERVADDR));    
    Servaddr.sin_family=af_inet; Servaddr.sin_port=htons (8080);//convert unsigned short integer value to netThe byte order, in which the high-order byte of the value is deposited into the low byte in memory 0x1234 becomes 0x3412 if (Inet_pton (AF_INET,ARGV[1],&SERVADDR.SIN_ADDR) <=0)//IP address in the    
        Convert dotted decimal and integer {cout<< "inet_ptons error" <<endl;    
    Exit (0); } if (Connect (SOCKFD, (sockaddr*) &servaddr,sizeof (servaddr)) <0) {cout<< "Connect er    
        Ror "<<endl;    
    Exit (0);    
        while ((N=read (sockfd,recvline,maxline)) >0) {recvline[n]=0;    
            if (fputs (recvline,stdout) ==eof) {cout<< "fputs error" <<endl;    
        Exit (0);    
      } if (n<0) {cout<< "read error" <<endl;    
    Exit (0);    
} exit (0); }

Related Article

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.