UNIX network programming chart

Source: Internet
Author: User
Tags socket error

Preface

When I first came into contact with the network field, I learned the principle of the protocol and studied protocol-related algorithms, as we all know, the study of protocol pure theory is boring and complex. I fell asleep when I looked at it. Due to the project's needs, I did not expect to go through more than half a month of hard work without any practical experience. I actually gave most of the Protocol theories to the research, it is also required to design testing algorithms based on the Protocol's working principles. Since the first project only required the design of protocol algorithms, I was blank in implementing protocol programming. By accident, I learned that network programming is a subject, I think it seems to be tailored for me, and I am determined to learn it, even whether it is useful or not for looking for a job in the future. So I immediately bought a classic tutorial on UNIX network programming. I never touched it again after I flipped through the first few pages in the first few days. Sorry, until now, I picked it up again. Until now there are several main reasons: 1. I think it will certainly be involved in future project development, so there is no need to spend time alone; 2. after I bought the book UNIX network programming, I wanted to use my spare time to study it. However, I usually don't have time to learn such complicated things, I had to configure the Source Code compiling environment before I had to study in depth. Besides, I am not that patient. If you don't talk much about it, go to the topic.

1. Environment Configuration

The examples in UNIX network programming are all run in Unix systems. In fact, there is no need to install a UNIX system during learning. The same is true for Linux systems, I installed Windows/Linux dual-systems.

I believe that all those who read this book have seen a sentence # include "UNP. H "is the header file. This is the header file specially written by the author for all examples in the teaching material. In fact, this is nothing strange, he only packages the header files and some functions used in socket programming. If you do not need UNP. h. You can add the required header files to your program. As a matter of fact, I personally understand that if you have a programming foundation, you do not need to use an encapsulated function (such as socket, accept, and listen). There are two reasons:

1. configuring the environment according to the author's requirements is a little troublesome. If you are not very clear about the entire program structure, you may feel dizzy. If you use Baidu to configure the environment for others, you have configured the configuration successfully, but you may not know the reason for doing so.

2. This is the most important thing. Because we are in the learning stage, we must learn the APIS provided by the system, instead of the APIS encapsulated once by others, if you get used to wrapped functions encapsulated in books, you may cause unnecessary errors and troubles when programming in other environments. In order to help me grasp the knowledge and memory, I insisted on not using UNP during the learning process. h and the package functions provided by the author, because of this, I can directly skip the environment configuration process I spent a lot of time studying and download the source code online.

 

The old principle is that theoretical learning is not as fast as simply looking at the program crash. Below are two procedures for obtaining the server time after modification.

Client (note: the port must be later than 1024. If it is a root account, select 1 ~ 1024 numbers are used as ports)

1 # include <string. h> 2 # include <sys/types. h> 3 # include <sys/socket. h> 4 # include <sys/time. h> 5 # include <time. h> 6 # include <fcntl. h> 7 # include <netinet/in. h> 8 # include <ARPA/inet. h> 9 # include <sys/errno. h> 10 # include <iostream> 11 # include <stdlib. h> 12 # include <stdio. h> // The above rows can be used # include "unp. H "replaces 13 using namespace STD; 14 15 const int maxline = 1024; 16 int main (INT argc, char ** argv) 17 {18 int so Ckfd, connectfd, N; 19 char recvline [maxline + 1]; 20 char sendline [maxline + 1]; 21 22 struct sockaddr_in servaddr; 23 if (argc! = 2) 24 {25 cout <"Usage: daytimecli <IPaddress>" <Endl; 26 exit (0); 27} 28 sockfd = socket (af_inet, sock_stream, 0); 29 If (sockfd <0) // in actual project development, this step must be determined to check whether the statement is successfully executed. The package function socket () in the Teaching Material () it is the judgment process that includes the creation and the following rows. 30 {31 cout <"socket error" <Endl; 32 exit (0); 33} 34 memset (& servaddr, 0, sizeof (servaddr )); // bzero (), which can be found in unp. see the definition 35 servaddr in H. sin_family = af_inet; 36 servaddr. sin_port = htons (1300); 37 If (inet_ton (af_inet, argv [1], & servaddr. sin_addr) <= 0) 38 {39 cout <"inet_ptons error" <Endl; 40 exit (0); 41} 42 connectfd = connect (sockfd, (sockaddr *) & servaddr, sizeof (servaddr); 43 If (connectfd <0) 44 {45 cout <"Connect er Ror: "<connectfd <Endl; 46 exit (0); 47} 48 While (n = read (sockfd, recvline, maxline)> 0) 49 {50 recvline [N] = 0; 51 snprintf (sendline, strlen ("client send string! \ N ")," client send string! \ N "); 52 write (sockfd, sendline, strlen (sendline); // send () 53 If (fputs (recvline, stdout) = EOF Winsock) // fputs (STR, file) Input STR to the file, which is equivalent to cout <recvline; 54 {55 cout <"fputs error" <Endl; 56 exit (0); 57} 58} 59 If (n <0) 60 {61 cout <"read error" <Endl; 62 Exit (0 ); 63} 64 exit (0); 65}

Server

 1 #include<string.h>   2 #include <sys/types.h>   3 #include <sys/socket.h>   4 #include <sys/time.h>   5 #include <time.h>   6 #include <fcntl.h>   7 #include<netinet/in.h>   8 #include<arpa/inet.h>   9 #include <sys/errno.h>  10 #include<iostream>  11 #include<stdlib.h>  12 #include<stdio.h>  13 #include<errno.h>  14 using namespace std;  15 16 const int MAXLINE = 2048;17 18 int main(int argc, char **argv)19 {20     int listenfd,connfd,bindfd;21     struct sockaddr_in servaddr;22     char buff[MAXLINE],recvbuff[MAXLINE];23     time_t ticks;24 25     listenfd = socket(AF_INET,SOCK_STREAM,0);26     if (listenfd < 0)27     {28         cout << "listen error : "<<strerror(errno) << endl;29         exit(0);30     }31     memset(&servaddr,0, sizeof(servaddr));32     servaddr.sin_family = AF_INET;33     servaddr.sin_port = htons(1300);34     servaddr.sin_addr.s_addr = htonl(INADDR_ANY);35 36     bindfd=bind(listenfd, (sockaddr*)&servaddr,sizeof(servaddr));37     if (bindfd < 0)38     {39         cout << "bind error : " << bindfd << endl;40     }41     listen(listenfd, 5);42     while(1) {43         connfd = accept(listenfd,(sockaddr*)NULL,NULL);44         ticks = time(NULL);45         snprintf(buff,sizeof(buff),"%.24s\r\n",ctime(&ticks));46         cout << "before write,buff : " << buff << endl;47         write(connfd,buff,strlen(buff));48         cout << "after write,buff : " << buff << endl;49         strcat(buff," test for socket!\n" );50         cout << "after test write,buff : " << buff << endl;51         write(connfd,buff,strlen(buff));52         cout << "before read,recvbuff : " << recvbuff << endl;53         int n=read(connfd,recvbuff,strlen(buff));54         cout << "after read,recvbuff : " << recvbuff << endl;55         cout << "read n = "<<n <<"length recvbuff = " << strlen(recvbuff) <<endl;56         close(connfd);57     }58 }

Ah, after half an hour, I couldn't find the screenshot capture tool in Linux, and I couldn't run the program on the result. If I had time, I 'd test it myself. This program can run successfully.

 

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.