Linux C + + TCP socket transfer file or picture 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 the message is received from the client, and the client establishes a connection to the server side. After the connection is established, read the contents of the file (/root/workspace/socket-picture/ bizhi.jpg), sent to the server side, the server side of the new New1.jpg file, the received file content is saved to new1.jpg, new1.jpg in the current directory;

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]; -FILE *FP; -     intN; -  +     if(LISTENFD = socket (af_inet, Sock_stream,0)) == -1 ){ -printf"Create socket Error:%s (errno:%d) \ n", Strerror (errno), errno); +         return 0; A     } atprintf"----init socket----\ n"); -  -memset (&AMP;SERVADDR,0,sizeof(SERVADDR)); -servaddr.sin_family =af_inet; -SERVADDR.SIN_ADDR.S_ADDR =htonl (inaddr_any); -Servaddr.sin_port = htons (6666); in     //to set up ports for reuse -     intcontain; toSetSockOpt (Listenfd,sol_socket, SO_REUSEADDR, &contain,sizeof(int)); +  -     if(Bind (LISTENFD, (structsockaddr*) &servaddr,sizeof(SERVADDR)) == -1){ theprintf"bind socket Error:%s (errno:%d) \ n", Strerror (errno), errno); *         return 0; $     }Panax Notoginsengprintf"----bind sucess----\ n"); -  the     if(Listen (LISTENFD,Ten) == -1){ +printf"Listen socket Error:%s (errno:%d) \ n", Strerror (errno), errno); A         return 0; the     } +     if(fp = fopen ("new1.jpg","AB") ) ==NULL) -     { $printf"file.\n"); $ Close (LISTENFD); -Exit1); -     } the  -printf"======waiting for client ' s request======\n");Wuyi      while(1){ the         structsockaddr_in client_addr; -Socklen_t size=sizeof(CLIENT_ADDR); Wu         if(CONNFD = Accept (LISTENFD, (structsockaddr*) &client_addr, &size)) = =-1){ -printf"Accept Socket Error:%s (errno:%d)", Strerror (errno), errno); About             Continue; $         } -          while(1){ -n =Read (CONNFD, buff, MAXLINE); -             if(n = =0) A                  Break; +Fwrite (Buff,1, N, FP); the         } -Buff[n] =' /'; $printf"recv msg from client:%s\n", buff); the Close (CONNFD); the fclose (FP); the     } the Close (LISTENFD); -     return 0; in}

Client.cpp

1#include <stdio.h>2#include <errno.h>3#include <string.h>4#include <netdb.h>5#include <sys/types.h>6#include <netinet/inch.h>7#include <sys/socket.h>8#include <stdlib.h>9#include <unistd.h>Ten#include <arpa/inet.h> One#include <netdb.h> A #defineMAXLINE 4096 -  - intMainintargcChar**argv) { the     intsockfd, Len; -     CharBuffer[maxline]; -     structsockaddr_in servaddr; -FILE *Fq; +  -     if(ARGC! =2){ +printf"usage:./client <ipaddress>\n"); A         return 0; at     } -  -     if(SOCKFD = socket (af_inet, Sock_stream,0)) <0){ -printf"Create socket Error:%s (errno:%d) \ n", Strerror (errno), errno); -         return 0; -     } in  -memset (&AMP;SERVADDR,0,sizeof(SERVADDR)); toservaddr.sin_family =af_inet; +Servaddr.sin_port = htons (6666); -     if(Inet_pton (Af_inet, argv[1], &servaddr.sin_addr) <=0){ theprintf"Inet_pton error for%s\n", argv[1]); *         return 0; $     }Panax Notoginseng  -     if(Connect (SOCKFD,structsockaddr*) &servaddr,sizeof(SERVADDR)) <0){ theprintf"Connect Error:%s (errno:%d) \ n", Strerror (errno), errno); +         return 0; A     } the     if(Fq = fopen ("/root/workspace/socket-picture/bizhi.jpg","RB") ) ==NULL) { +printf"File open.\n"); - Close (SOCKFD); $Exit1); $     } -  -Bzero (Buffer,sizeof(buffer)); the      while(!feof (FQ)) { -Len = fread (buffer,1,sizeof(buffer), FQ);Wuyi         if(Len! =Write (sockfd, buffer, Len)) { theprintf"write.\n"); -              Break; Wu         } -     } About Close (SOCKFD); $ fclose (FQ); -  -     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, client clients exit directly after execution, and you can see the terminal window of the server output "Recv msg from client:". Open the current directory (refers to the directory where the executable file server is located), you can see the new1.jpg file has been generated, double-click Open, the contents of the file is not lost.

Linux C + + TCP socket transfer file or picture instance

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.