Linux Server, client

Source: Internet
Author: User
Tags socket error htons

#include <stdio.h>//standard input and output header file#include <sys/socket.h>//function declarations and structure definitions related to sockets#include <netinet/inch.h>//some struct declarations, macro definitions#include <arpa/inet.h>//some function declarations#include <errno.h>//Viewing error codes/*these are the necessary header files, several of which are required for almost every network program, such as <sys/socket.h>,<netinet/in.h>, etc.*/intMainintargcChar*argv[]) {printf ("=======================================\n"); printf ("= My First server program =\n"); printf ("=======================================\n"); intsockfd,new_fd;structsockaddr_in server_addr;structsockaddr_in client_addr;intSin_size,portnumber;Charhello[]="Hello, the network! This is my first server.\n"; if(argc!=2) {fprintf (stderr,"usage:%s portnumber \a\n", argv[0]); Exit (1);//Program error ended }if((Portnumber=atoi (argv[1])) <0) {fprintf (stderr,"usage:%s portnumber \a\n", argv[0]); Exit (1); }   //atoi () converts a character type to an integral type//set up the socket descriptor (intnet protocol cluster, stream socket, using default protocol) if(Sockfd=socket (Af_inet,sock_stream,0))==-1) {fprintf (stderr,"Socket error:%s \a\n", Strerror (errno)); Exit (1); }//populate the server's socket address structure SOCKADDRBzero (&AMP;SERVER_ADDR,sizeof(structsockaddr_in)); Server_addr.sin_family=af_inet; server_addr.sin_addr.s_addr=htonl (Inaddr_any); Server_addr.sin_port=htons (portnumber);//start bundling sockfd descriptors if(Bind (SOCKFD, (structSOCKADDR *) (&AMP;SERVER_ADDR),sizeof(structSOCKADDR)) ==-1) {fprintf (stderr,"Bind error:%s \a\n", Strerror (errno)); Exit (1); } //Monitoring SOCKFD descriptorsif(Listen (SOCKFD,5)==-1){   //fprintf (stderr, "Listen error:%s \a\n", Strerror (errno));Exit1); }   while(1) {//block the server until the client program establishes a connectionSin_size=sizeof(structsockaddr_in); if(New_fd=accept (SOCKFD, (structsockaddr*) (&AMP;CLIENT_ADDR), &sin_size)) ==-1){     //fprintf (stderr, "Accept error:%s \a\n", Strerror (errno));Exit1); } fprintf (stderr,"Server get connection from%s: \ n", Inet_ntoa (CLIENT_ADDR.SIN_ADDR)); if(Write (New_fd,hello,strlen (hello)) ==-1){       //fprintf (stderr, "Write error:%s \a\n", Strerror (errno));Exit1); }     //end of communication, continue to loop executionClose (NEW_FD);/*when the server finishes processing the client request, close the connection socket*/ }//turn off the listener socket descriptorClose (SOCKFD); exit (0); }
#include <stdio.h>#include<sys/socket.h>#include<netinet/inch.h>#include<arpa/inet.h>#include<errno.h>#include<netdb.h>/*including structure hostent (host environment), several functions of obtaining host information*/intMainintargcChar*argv[]) {printf ("=======================================\n"); printf ("= My First client program! =\n"); printf ("=======================================\n"); intsockfd,new_fd;structsockaddr_in server_addr;structHostent *host;intNbytes,portnumber;Charbuffer[1024x768]; if(argc!=3) {fprintf (stderr,"usage:%s hostname portnumber \a\n", argv[0]); Exit (1); }//Get server address if((Host=gethostbyname (argv[1]))==NULL) {fprintf (stderr,"HostName erro!"); Exit (1); } if((Portnumber=atoi (argv[2])) <0) {fprintf (stderr,"usage:%s hostname portnumber \a\n", argv[0]); Exit (1); } //establishing a client SOCKFD descriptor if(Sockfd=socket (Af_inet,sock_stream,0))==-1) {fprintf (stderr,"Socket error:%s \a\n", Strerror (errno)); Exit (1); }//before calling the function connect, you need to specify the socket address of the server process//filling the SOCKADDR structureBzero (&AMP;SERVER_ADDR,sizeof(structsockaddr_in)); Server_addr.sin_family=af_inet; Server_addr.sin_port=htons (portnumber);//server_addr.sin_addr= * (struct in_addr *) host->h_addr);//initiating a connection request establish a connection with the remote server if(Connect (SOCKFD,structSOCKADDR *) (&AMP;SERVER_ADDR),sizeof(structSOCKADDR)) ==-1) {fprintf (stderr,"Bind error:%s \a\n", Strerror (errno)); Exit (1); } //perform read-write data operations to display data on standard output if(Nbytes=read (Sockfd,buffer,1024x768))==-1) {fprintf (stderr,"Read Error:%s \ n", Strerror (errno)); Exit (1); } Buffer[nbytes]='\\'; printf ("Received:%s \ n", buffer);//End Communication//Close ConnectionClose (SOCKFD); exit (0); }

Linux Server, client

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.