Network programming learning under Linux--Getting Started instance ZZ

Source: Internet
Author: User
Tags socket error htons

Http://www.cppblog.com/cuijixin/archive/2008/03/14/44480.html is not also the use of C how to achieve network programming feel mysterious Mo, we are here to rip its mysterious veil, hehe.

Together:

Eh, do not worry, we first introduce some of the main implementation process of network programs , mainly to facilitate better understanding of the following program examples OH:

1) The system starts the server execution. The server completes some initialization operations and then goes to sleep, waiting for the client to request.
2) on a machine in the network, the user executes the client program
3) client to establish a connection to the server process
4) After the connection is established, the client sends a request to the server over the network to request a service.
5) After the server receives a request from the client, it processes the content according to the client's request, and then returns the processing result.
6) The server disconnects from the client, continues to sleep, and waits for requests from other clients.

Now introduce an instance

Function: Implement a simple server-client programming, the client sends the connection request after receiving the connection success information.
The code consists of two parts, the server section (SERVICE.C) and the client part (CLIENT.C)



1 /*service.c*/2#include <stdio.h>3#include <stdlib.h>4#include <errno.h>5#include <string.h>6#include <sys/types.h>7#include <netinet/inch.h>8#include <sys/socket.h>9#include <sys/wait.h>Ten #defineMyPort 3490/* Open port number */ One #defineBACKLOG 5/* Specifies the maximum number of unacceptable client requests that the socket can accept */ A Main () - { - intsockfd,new_fd; the structsockaddr_in srvaddr; - structsockaddr_in cliaddr; - intsin_size; - /*Create socket Descriptor*/ + if(Sockfd=socket (Af_inet,sock_stream,0))==-1) - { +Perror ("Socket Error"); AExit1); at } -Bzero (&AMP;SRVADDR,sizeof(SRVADDR)); - /*populate an Internet socket address structure with your own IP address and port information*/ -srvaddr.sin_family=af_inet;  -srvaddr.sin_port=htons (myport); - /*function bind sets the address and socket of the server together*/ in if(Bind (SOCKFD, (structSOCKADDR *) &srvaddr,sizeof(structSOCKADDR)) ==-1) - { toPerror ("bind error"); +Exit1); - } the /*The listen function tells the kernel that the socket can accept requests from the client*/ * if(Listen (Sockfd,backlog) ==-1) $ {Panax NotoginsengPerror ("Listen error"); -Exit1); the } + /*processing the client's request, calling the function accept to get a connection to a client*/ A  for(;;) the { +Sin_size=sizeof(structsockaddr_in); -if(New_fd=accept (SOCKFD, (structSOCKADDR *) (&cliaddr,&sin_size)) ==-1) $       { $Perror ("Accept Error"); -Continue; -       } theprintf"server:got connection from%s \ n", Inet_ntoa (CLIADDR.SIN_ADDR)); - /*Write data to customers*/Wuyiif(Write (NEW_FD,"hello,network!\n", -)==-1) thePerror ("Write error!"); - Close (NEW_FD); Wu } - Close (SOCKFD); About}


The following is the client:
1 /*client.c*/2#include <stdio.h>3#include <stdlib.h>4#include <errno.h>5#include <string.h>6#include <netdb.h>7#include <sys/types.h>8#include <netinet/inch.h>9#include <sys/socket.h>Ten #definePORT 3490 One #defineMaxdatasize 5000 A intMainintargcChar**argv) - { - intsockfd,nbytes; the Charbuf[1024x768]; - structHostent *he; - structsockaddr_in srvaddr; - if(argc!=2) + { -Perror ("usage:client hostname\n"); +Exit1); A } at /*function gethostbyname the IP address corresponding to the specified domain address*/ - if((He=gethostbyname (argv[1]))==NULL) - { -Perror ("gethostbyname"); -Exit1); - } in /*create socket, return socket descriptor*/ - if(Sockfd=socket (Af_inet,sock_stream,0))==-1) to { +Perror ("Create socket Error"); -Exit1); the } *Bzero (&AMP;SRVADDR,sizeof(SRVADDR)); $ /*populate an Internet socket address structure with the IP address and port number of the obtained remote server process*/Panax Notoginsengsrvaddr.sin_family=af_inet; -srvaddr.sin_port=htons (PORT); theSrvaddr.sin_addr=* ((structIN_ADDR *) he->h_addr); + /*establish an Internet connection to this remote server with connect*/ A if(Connect (SOCKFD,structSOCKADDR *) &srvaddr,sizeof(structSOCKADDR)) ==-1) the { +Perror ("Connect Error"); -Exit1); $ } $ /*call the Read function to read the message from the server write .*/ - if((Nbytes=read (sockfd,buf,maxdatasize)) ==-1) - { thePerror ("Read Error"); -Exit1);Wuyi } thebuf[nbytes]=' /'; -printf"read:%s", buf); Wu Close (SOCKFD); -}







Now let's compile and execute to see the effect OH

Compile:
#gcc-O service service.c
#gcc-O Client client.c
Modify Execute Permissions
#chmod 770 Service
#chmod 770 Client
Perform
Now execute server Side OH
#./service
Open a separate command-line window to connect to the server's 3490 port
#./client localhost
Oh, see the effect of it
Server-side:

Server:got connection from 127.0.0.1

Client

read:hello,network!

Network programming learning under Linux--Getting Started instance ZZ

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.