Linux C + + server programming, collecting a test example

Source: Internet
Author: User
Tags htons

//Server#include <stdio.h>#include<unistd.h>#include<stdlib.h>#include<string.h>#include<malloc.h>#include<pthread.h>#include<semaphore.h>#include<sys/types.h>#include<sys/socket.h>#include<sys/time.h>#include<sys/ioctl.h>#include<netinet/inch.h>#include<errno.h>#include<string.h>#include"time.h"  #defineServer_port 10000#defineMax_buf_size 16#defineSUCCESS 0#defineFAILURE-1intRecvfromserver (intICLIENTSOCKFD); intMainintargcChar**argv) {    intISERVERSOCKFD; intICLIENTSOCKFD; structsockaddr_in stserveraddr; structsockaddr_in stclientaddr; intIserverlen; intIclientlen; intIopt =1; intIreturn;         pid_t pid; // Timetime_t timer; structTM *Tblock; Timer=Time (NULL); Tblock= LocalTime (&timer); printf ("Servertime is%s", Asctime (Tblock)); printf ("The server.\n"); printf ("I am waiting the client to rsync time \ n"); //Create server SocketISERVERSOCKFD = socket (af_inet, Sock_stream,0); if(ISERVERSOCKFD <0) {printf ("socket create failed in commmanageserverrecvthread.\n"); returnFAILURE; } stserveraddr.sin_family=af_inet; StServerAddr.sin_addr.s_addr=htonl (Inaddr_any); Stserveraddr.sin_port=htons (Server_port); Iserverlen=sizeof(STSERVERADDR); //bind socketIreturn = Bind (ISERVERSOCKFD, (structSOCKADDR *) &stserveraddr, Iserverlen); if(Ireturn <0) {printf ("socket bind failed in commmanageserverrecvthread.\n"); } setsockopt (Iserversockfd,sol_socket,so_reuseaddr,&iopt,sizeof(iopt)); //Listen socketIreturn = Listen (ISERVERSOCKFD,Ten); //Accept request from client     while(1) {ICLIENTSOCKFD= Accept (ISERVERSOCKFD, (structsockaddr*) &stclientaddr, (unsigned *) &Iclientlen); if(ISERVERSOCKFD <0) {printf ("Accept failed.\n"); } PID=Fork (); Switch(PID) { Case-1: {printf ("Fork failed.\n");  Break; }         Case 0: {Write (Iclientsockfd,asctime (tblock), -); printf ("Child Service ok!! \ n");           Close (ICLIENTSOCKFD);  Break; }        default: {close (ICLIENTSOCKFD);  Break;     }}} close (ICLIENTSOCKFD);     Close (ISERVERSOCKFD); returnSUCCESS;} intRecvfromserver (intICLIENTSOCKFD) {    CharAcbuf[max_buf_size]; intIreadnum; memset (Acbuf,0,sizeof(ACBUF)); Ireadnum= Recv (ICLIENTSOCKFD, Acbuf,0xFFFF,0); if(Ireadnum <0) {printf ("Read failed.\n"); returnFAILURE; } printf ("Receive Form client:\n"); printf ("%s\n", ACBUF); returnSUCCESS;} //Client#include <stdio.h>#include<unistd.h>#include<stdlib.h>#include<string.h>#include<malloc.h>#include<pthread.h>#include<semaphore.h>#include<sys/types.h>#include<sys/socket.h>#include<sys/time.h>#include<sys/ioctl.h>#include<netinet/inch.h>#include<errno.h>#include<string.h>#defineServer_port 10000#defineServer_ip "192.168.0.112"#defineWrite_buf_size 1024#defineSUCCESS 0#defineFAILURE-1CharAcbuf[write_buf_size];intMy_read (intFdvoid*buffer,intlength) {                 intBytes_left; intBytes_read; Char*ptr; Bytes_left=length; PTR=buffer;  while(bytes_left>0) {Bytes_read=read (fd,ptr,bytes_left); if(bytes_read<0)                  {                          if(errno==eintr) Bytes_read=0; Else                                       return-1; }                          Else               if(bytes_read==0)                           Break; Bytes_left-=Bytes_read; PTR+=Bytes_read; }               return(length-bytes_left);} intMainintargcChar**argv) {    intISOCKFD; intIreadnum; structsockaddr_in stserveraddr; structsockaddr_in stclientaddr; intIserverlen; intIclientlen; intIreturn; printf ("The client.\n"); //Create server SocketISOCKFD = socket (af_inet, Sock_stream,0); if(ISOCKFD <0) {printf ("socket create failed in commmanageserverrecvthread.\n"); returnFAILURE; } stserveraddr.sin_family=af_inet; Inet_pton (Af_inet, Server_ip,&stserveraddr.sin_addr); Stserveraddr.sin_port=htons (Server_port); Iserverlen=sizeof(STSERVERADDR);  while(1)    {            //printf ("Try to connect to the server ... \ n");Ireturn = Connect (ISOCKFD, (Const structSOCKADDR *) &stserveraddr, Iserverlen); if(Ireturn >=0)        {             Break; } Sleep (3); } printf ("Connect success.\n"); memset (Acbuf,0,sizeof(ACBUF)); My_read (ISOCKFD, Acbuf, -); printf ("%s\n", ACBUF);     Close (ISOCKFD); returnSUCCESS;}
View Code

Linux C + + server programming, collecting a test example

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.