C Language for TCP communication

Source: Internet
Author: User
Tags htons

Server-side:

#include <sys/types.h> #include <sys/socket.h> #include <stdio.h> #include <stdlib.h> #include <errno.h> #include <string.h> #include <unistd.h> #include <netinet/in.h> #define Servport 3333 #define BACKLOG 10#define max_connected_no 10#define maxdatasize 5int Main () {struct sockaddr_in server_sockaddr, Clie    NT_SOCKADDR;    int sin_size, recvbytes;    int SOCKFD, CLIENT_FD;        Char Buf[maxdatasize];        if (SOCKFD = socket (af_inet, sock_stream, 0)) = =-1) {perror ("socket");    Exit (1); } printf ("Socket success!        SOCKFD =%d\n ", SOCKFD);    server_sockaddr.sin_family = af_inet;    Server_sockaddr.sin_port = htons (Servport);    SERVER_SOCKADDR.SIN_ADDR.S_ADDR = Inaddr_any;        Bzero (& (Server_sockaddr.sin_zero), 8);        if (bind (SOCKFD, struct sockaddr *) &server_sockaddr, sizeof (struct sockaddr)) = =-1) {perror ("bind");    Exit (1);        } printf ("Bind success!\n"); if (Listen (SOCKFD, BACKLOG) = = -1) {perror ("listen");    Exit (1);        } printf ("listening!\n");        if (client_fd = Accept (SOCKFD, (struct sockaddr *) &client_sockaddr, &sin_size)) = =-1) {perror ("accept");    Exit (1);        } if ((Recvbytes = recv (client_fd, buf, maxdatasize, 0)) = =-1) {perror ("recv");    Exit (1);    }//cout << "received a connection:" << buf << Endl;    Close (SOCKFD); return 0;}

Client:

#include <stdio.h> #include <errno.h> #include <string.h> #include <netdb.h> #include <sys/ types.h> #include <netinet/in.h> #include <sys/socket.h> #define Servport 3333#define maxdatasize 100int    Main (int argc, char *argv[]) {int sockfd, sendbytes;    Char Buf[maxdatasize];    struct Hostent *host;    struct sockaddr_in serv_addr;        if (argv < 2) {fprintf (stderr, "Please enter the server is hostname!\n");    Exit (1);        } if ((host = gethostbyname (argv[1)) = = NULL) {perror ("gethostbyname");    Exit (1);        } if ((SOCKFD = socket (af_inet, sock_stream, 0)) = =-1) {perror ("socket");    Exit (1);    } serv_addr.sin_family = Af_inet;    Serv_addr.sin_port = htons (Servport);    serv_addr.sin_addr = * (struct in_addr *) host->h_addr);        Bzero (& (Serv_addr.sin_zero), 8);    if (Connect (SOCKFD, struct sockaddr *) &serv_addr, sizeof (struct sockaddr)) = =-1) {perror ("connect");    Exit (1);        } if ((Sendbytes = Send (SOCKFD, "Hello", 5, 0)) = =-1) {perror ("send");    Exit (1);    } Char revcbuf[100];    Recv (SOCKFD, revcbuf, 100, 0);    Close (SOCKFD); return 0;}



C Language for TCP communication

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.