Linux C + + TCP socket Communication instance

Source: Internet
Author: User
Tags socket error htons

Environment: Linux language: C + + Communication Mode: TCP

  The following uses the TCP protocol to write a simple server, the client, where the server side has been listening to the local port number No. 6666. If a connection request is received, the request is received and a message is received from the client, and the client establishes a connection with the server and sends a message.

Server.cpp
1#include <stdio.h>2#include <stdlib.h>3#include <string.h>4#include <errno.h>5#include <sys/types.h>6#include <sys/socket.h>7#include <netinet/inch.h>8#include <unistd.h>9 Ten #defineMAXLINE 4096 One  A intMainintargcChar**argv) { -     intlistenfd, CONNFD; -     structsockaddr_in servaddr; the     Charbuff[4096]; -     intN; -  -     if(LISTENFD = socket (af_inet, Sock_stream,0)) == -1 ){ +printf"Create socket Error:%s (errno:%d) \ n", Strerror (errno), errno); -         return 0; +     } A  atmemset (&AMP;SERVADDR,0,sizeof(SERVADDR)); -servaddr.sin_family =af_inet; -SERVADDR.SIN_ADDR.S_ADDR =htonl (inaddr_any); -Servaddr.sin_port = htons (6666); -  -     if(Bind (LISTENFD, (structsockaddr*) &servaddr,sizeof(SERVADDR)) == -1){ inprintf"bind socket Error:%s (errno:%d) \ n", Strerror (errno), errno); -         return 0; to     } +  -     if(Listen (LISTENFD,Ten) == -1){ theprintf"Listen socket Error:%s (errno:%d) \ n", Strerror (errno), errno); *         return 0; $     }Panax Notoginseng  -printf"======waiting for client ' s request======\n"); the      while(1){ +         if(CONNFD = Accept (LISTENFD, (structsockaddr*) (null)) = =-1){ Aprintf"Accept Socket Error:%s (errno:%d)", Strerror (errno), errno); the             Continue; +         } -n = recv (CONNFD, Buff, MAXLINE,0); $Buff[n] =' /'; $printf"recv msg from client:%s\n", buff); - Close (CONNFD); -     } the Close (LISTENFD); -     return 0;Wuyi}
client.cpp
1#include <stdio.h>2#include <stdlib.h>3#include <string.h>4#include <errno.h>5#include <sys/types.h>6#include <sys/socket.h>7#include <netinet/inch.h>8#include <arpa/inet.h>9#include <unistd.h>Ten #defineMAXLINE 4096 One  A intMainintargcChar**argv) { -     intsockfd, N; -     Charrecvline[4096], sendline[4096]; the     structsockaddr_in servaddr; -  -     if(ARGC! =2){ -printf"usage:./client <ipaddress>\n"); +         return 0; -     } +  A     if(SOCKFD = socket (af_inet, Sock_stream,0)) <0){ atprintf"Create socket Error:%s (errno:%d) \ n", Strerror (errno), errno); -         return 0; -     } -  -memset (&AMP;SERVADDR,0,sizeof(SERVADDR)); -servaddr.sin_family =af_inet; inServaddr.sin_port = htons (6666); -     if(Inet_pton (Af_inet, argv[1], &servaddr.sin_addr) <=0){ toprintf"Inet_pton error for%s\n", argv[1]); +         return 0; -     } the  *     if(Connect (SOCKFD,structsockaddr*) &servaddr,sizeof(SERVADDR)) <0){ $printf"Connect Error:%s (errno:%d) \ n", Strerror (errno), errno);Panax Notoginseng         return 0; -     } the  +printf"send msg to server: \ n"); AFgets (Sendline,4096, stdin); the     if(Send (SOCKFD, Sendline, strlen (Sendline),0) <0){ +printf"Send msg Error:%s (errno:%d) \ n", Strerror (errno), errno); -         return 0; $     } $ Close (SOCKFD); -     return 0; -}
Makefile
1 All:server Client2 SERVER:SERVER.O3g++-G-o server server.o4 CLIENT:CLIENT.O5g++-G-o client client.o6 Server.o:server.cpp7g++-G-C Server.cpp8 Client.o:client.cpp9g++-G-C client.cppTen Clean:all OneRM All

after the make command is executed, the server and client two executables are generated. Open two terminal Windows respectively, one executes the./server command, one executes the./client 127.0.0.1 command, which indicates that the 6666 port of the machine is attached, executing the./server command to execute first. After executing the./client 127.0.0.1 command, you will be prompted to send the contents of the server, after entering "Hello", client clients are finished, you can see that terminal window of the server output "recv msg from Client:hello ”。 Continue with the./client 127.0.0.1 command, then enter "haha", and the terminal of the server continues to output "recv msg from Client:haha". Results.

TCP Protocol Communication interaction process:

Reference Documents:

"Backstage Development: core technology and Application Practice" chapter sixth, p206

Linux C + + TCP socket Communication instance

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.