Linux socket programming-simplest server and client programs

Source: Internet
Author: User

I reviewed the Linux socket programming, wrote the simplest server and client programs, and passed the test. The server-side programs adopt the loop and busy waiting mechanism, which will be changed to multithreading and thread pool mechanism later.

Server programs

#include <cstdlib>#include <iostream>#include <cstring>#include <cstdio>#include <cerrno>#include <sys/types.h>#include <sys/socket.h>#include <netinet/in.h>#include <arpa/inet.h>#include <string>using namespace std;#define BACKLOG 10int main(int argc, char * argv[]){int connfd, sockfd;struct sockaddr_in servaddr;struct sockaddr_in tempaddr;struct sockaddr_in cliaddr;socklen_t clilen;char ip_str[INET_ADDRSTRLEN];int ret_val;socklen_t templen;sockfd = socket(AF_INET, SOCK_STREAM, 0);if(sockfd == -1){perror("socket");exit(1);}bzero(&servaddr, sizeof(servaddr));servaddr.sin_family = AF_INET;servaddr.sin_addr.s_addr = htonl(INADDR_ANY);servaddr.sin_port = 0;ret_val = bind(sockfd,(struct sockaddr *)&servaddr, sizeof(servaddr));if(ret_val == -1){perror("bind");exit(1);}cout<< "the sockfd is "<< sockfd << endl;ret_val = listen(sockfd,BACKLOG);if(ret_val == -1){perror("listen");exit(1);}templen = sizeof(struct sockaddr);ret_val =  getsockname(sockfd, (struct sockaddr *)&tempaddr, &templen);if(ret_val == -1){perror("getsockname");exit(1);}cout<<"Server is listening on port "<< ntohs(tempaddr.sin_port)<<endl;for(;;){clilen = sizeof(cliaddr);connfd = accept(sockfd,(struct sockaddr *) &cliaddr,&clilen);if(connfd == -1){perror("accept");continue;}cout<< " Sever: client "<< inet_ntoa(cliaddr.sin_addr) << " connect"<<endl;close(connfd);}return 0;}

Client Program

#include <cstdlib>#include <cstdio>#include <cstring>#include <sys/types.h>#include <sys/socket.h>#include <netinet/in.h>#include <arpa/inet.h>#include <unistd.h>#include <iostream>using namespace std;int main(int argc, char * argv[]){int sockfd;int conn_ret;struct sockaddr_in servaddr;if(argc != 3){cout<< "Usage : client <address> <port> " <<endl;return 0;}sockfd = socket(AF_INET,SOCK_STREAM,0);if (sockfd == -1){perror("sock");exit(1);}bzero(&servaddr,sizeof(servaddr));servaddr.sin_family = AF_INET;        servaddr.sin_port = htons(atoi(argv[2]));inet_pton(AF_INET,argv[1],&servaddr.sin_addr);conn_ret = connect(sockfd, (struct sockaddr *)&servaddr,sizeof(servaddr));if(conn_ret == -1){perror("connect");}close(sockfd);return 0;}

 

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.