C language implementation of the most basic back-up server and client (server with TCP protocol back to the client sent messages)

Source: Internet
Author: User
Tags htons

Words do not say, directly on the dry, the following two programs are Linux programs.

SERVER.C Complete code:

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
int main (int argc, char* argv[], char* envp[])
{
Char buf[1024] = {'} '};
int LISTENFD, new;
struct sockaddr_in servaddr;
struct sockaddr_in peeraddr;
socklen_t Peerlen = sizeof (PEERADDR);
memset (&servaddr, 0, sizeof (SERVADDR));

if ((LISTENFD = socket (pf_inet, Sock_stream, ipproto_tcp)) < 0)
{
printf ("Listenfd fail! \ n ");
return 1;
}

servaddr.sin_family = af_inet;
Servaddr.sin_port = htons (5188);
SERVADDR.SIN_ADDR.S_ADDR = htonl (Inaddr_any);

if (Bind (LISTENFD, (struct sockaddr*) &servaddr, sizeof (SERVADDR))) < 0)
{
printf ("Bind fail! \ n ");
return 2;
}

if ((Listen (LISTENFD, somaxconn)) < 0)
{
printf ("Listen fail! \ n ");
return 3;
}

if (new = Accept (LISTENFD, (struct sockaddr*) &peeraddr, &peerlen)) < 0)
{
printf ("Accept fail! NEW:%d\n ", new);
return 4;
}

while (1)
{
memset (buf, 0, sizeof (BUF));
Read (new, buf, sizeof (BUF));
Fputs (buf, stdout);
Write (new, buf, strlen (BUF));
}
Close (new);
Close (LISTENFD);
return 0;
}

CLIENT.C Complete code:

#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <stdlib.h>

int main (int argc, char* argv[], char* envp[])
{
Char buf[1024] = {'} '};
Char bufa[1024] = {'} '};
int sock;
struct sockaddr_in servaddr;
memset (&servaddr, 0, sizeof (SERVADDR));
servaddr.sin_family = af_inet;
Servaddr.sin_port = htons (5188);
SERVADDR.SIN_ADDR.S_ADDR = inet_addr ("127.0.0.1");

if ((sock = socket (pf_inet, Sock_stream, ipproto_tcp)) < 0)
printf ("Socket fail! \ n ");

if (Connect (sock, (struct sockaddr*) &servaddr, sizeof (SERVADDR)) < 0)
printf ("Connect fail! \ n ");

while (Fgets (buf, sizeof (BUF), stdin))
{
memset (&bufa, 0, sizeof (BUFA));
Write (sock, buf, sizeof (BUF));
Read (sock, Bufa, sizeof (BUFA));
Fputs (Bufa, stdout);
}

Close (sock);
return 0;
}

C language implementation of the most basic back-up server and client (server with TCP protocol back to the client sent messages)

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.