Socket Primer-Client-side ejection program

Source: Internet
Author: User
Tags socket error htons

Result output:
------------------------------------------------------
Client:
[Email protected]:~/public/c$./postbackcli.out 127.0.0.1
Connect OK
Aaa
Aaa
bbbbbbbbbbbbbbbbbbbbbbbbbbbbb
bbbbbbbbbbbbbbbbbbbbbbbbbbbbb
C
C
#
Close OK
[Email protected]:~/public/c$
------------------------------------------------------
Server-side:
[Email protected]:~/public/c$./postbacksrv.out
======waiting for client ' s request======
from:127.0.0.1,port:45050
---------Gorgeous split-line---------
Postbackcli.c

The main function in the detailed comments can see the blogger-Get the server Time instance-blog, with more detailed comments//part of the details of the code is more hasty, Bo Master new introduction, do not spray #include<stdio.h> #include <stdlib.h > #include <string.h> #include <errno.h> #include <sys/types.h> #include <sys/socket.h> #     include<netinet/in.h> #define MAXLINE 4096 void Str_cli (FILE *fp,int SOCKFD); int main (int argc, char** argv) {     int SOCKFD, N,rec_len;     Char recvline[4096], sendline[4096];     Char Buf[maxline];         struct sockaddr_in servaddr;     if (argc! = 2) {printf ("Usage:./client \ n");     Exit (0); } if ((SOCKFD = socket (af_inet, sock_stream, 0)) < 0) {printf ("Create Socket Error:%s (errno:%d) \ n", Strer     Ror (errno), errno);     Exit (0);     } memset (&servaddr, 0, sizeof (SERVADDR));     servaddr.sin_family = af_inet;     Servaddr.sin_port = htons (8000);     if (Inet_pton (Af_inet, argv[1], &servaddr.sin_addr) <= 0) {printf ("Inet_pton error for%s\n", argv[1]);     Exit (0); } if (Connect (sockfd, struct sockaddr*) &servaddr, sizeof (SERVADDR)) < 0) {printf ("Connect Error:%s (errno:%d) \ n", Strerror (errno), errno);     Exit (0);    } printf ("Connect ok\n");    STR_CLI (STDIN,SOCKFD);     Close (SOCKFD);    printf ("Close ok\n"); Exit (0); }//Callback handler void Str_cli (FILE *fp,int sockfd) {Char sendline[maxline],recvline[maxline];//defines two buffers while (Fgets (Sendlin E,MAXLINE,FP) = NULL)//from the standard input input to the sendline {if (sendline[0] = = ' # ') break;//if the input # exits the loop and ends the callback program write (sockfd,s Endline,strlen (Sendline));//writes the data in the sendline buffer to "socket" read (sockfd,recvline,maxline);//Read data from socket to Recvline fputs (recv Line,stdout);//read from the Recvline buffer to the standard output bzero (recvline,sizeof (Recvline));//clear the Accept buffer, each time is the latest data "should be aware of"}}

Postbacksrv.c

#include <stdio.h> #include <stdlib.h> #include <string.h> #include <errno.h> #include <sys/ types.h> #include <sys/socket.h> #include <netinet/in.h> #define DEFAULT_PORT 8000//default port #define MAXLINE     4096 void Str_echo (int sockfd); int main (int argc, char** argv) {int socket_fd, CONNECT_FD;     struct sockaddr_in servaddr,cliaddr;     Char Buff[maxline],temp[maxline];    int n;     uint32_t Len; Initialize the socket if (SOCKET_FD = socket (af_inet, sock_stream, 0)) = =-1) {printf ("Create Socket Error:%s (errno:     %d) \ n ", Strerror (errno), errno);     Exit (0);    }//Initialize memset (&servaddr, 0, sizeof (SERVADDR));     servaddr.sin_family = af_inet;     SERVADDR.SIN_ADDR.S_ADDR = htonl (Inaddr_any),//IP address is set to Inaddr_any, the system automatically gets the IP address of the machine. Servaddr.sin_port = htons (default_port);//Set the port to Default_port//bind the local address to the created socket on the IF (bind (socket_fd, struct sock addr*) &servaddr, sizeof (servaddr)) = = 1) {printf ("Bind socket Error:%s (ERRNO:%d) \ n ", Strerror (errno), errno);     Exit (0); }//Start listening for client connections if (listen (socket_fd) = =-1) {printf ("Listen socket Error:%s (errno:%d) \ n", Strerror (Errn     o), errno);     Exit (0);     } printf ("======waiting for client ' s request======\n");    while (1) {len=sizeof (CLIADDR);     Blocking until there is a client connection connect_fd = Accept (socket_fd, (struct sockaddr *) &cliaddr,&len);    Send the response data to the client if (!fork ())//Enter the child process, the child process service terminates when the parent process will send a SIGCHLD signal, the parent process is not processed, the child process into a zombie state, must clean up the zombie state, where the UNIX signal is involved in Li.               {//the child process closes the listener socket Close (SOCKET_FD) within it;        Str_echo (CONNECT_FD); Output Customer Agreement information printf ("\nfrom:%s,port:%d\n", (char *) inet_ntop (af_inet,&cliaddr.sin_addr,temp,sizeof (temp)), Ntohs     (Cliaddr.sin_port));      Close (CONNECT_FD);       Exit (0);     } close (CONNECT_FD); } close (SOCKET_FD);    } void Str_echo (int sockfd) {ssize_t n;    Char Buf[maxline];    while ((N=read (sockfd,buf,maxline)) >0) write (sockfd,buf,n); if (n<0 &AMP;&Amp ERRNO==EINTR) perror ("str_echo:red error");}

  

Socket Primer-Client-side ejection 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.