Linux Network programming--TCP/UDP programming model

Source: Internet
Author: User
Tags stdin htons

The TCP model is as follows:

The model above is already clear.

The specific function usage will not elaborate.

Take a look at the TCP simple example:

where server.c

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

#define ERR_LOG (ErrLog) do{perror (ErrLog); exit (1);} while (0)
#define N 128

./server 192.168.0.196 10000

int main (int argc, const char *argv[])
{
int sockfd;
int CONFD;
struct sockaddr_in serveraddr, clientaddr;
Char buf[n] = {};

if (argc! = 3)
{
fprintf (stderr, "usage:%s serverip port.\n", argv[0]);
return-1;
}

if ((SOCKFD = socket (af_inet, sock_stream, 0)) < 0)
{
Err_log ("fail to Socket");
}

printf ("SOCKFD =%d\n", SOCKFD);

serveraddr.sin_family = af_inet;
SERVERADDR.SIN_ADDR.S_ADDR = inet_addr (argv[1]);
Serveraddr.sin_port = htons (Atoi (argv[2]));

if (Bind (SOCKFD, (struct sockaddr*) &serveraddr, sizeof (SERVERADDR)) < 0)
{
Err_log ("Fail to bind");
}

if (Listen (SOCKFD, Ten) < 0)
{
Err_log ("Fail to listen");
}

socklen_t addrlen = sizeof (struct sockaddr);
if (CONFD = Accept (SOCKFD, (struct sockaddr *) &clientaddr, &addrlen)) < 0)
{
Err_log ("fail to accept");
}

printf ("CONFD =%d,%s and%d \ n", CONFD, Inet_ntoa (CLIENTADDR.SIN_ADDR), Ntohs (Clientaddr.sin_port));

while (1)
{
if (recv (CONFD, buf, N, 0) < 0)
{
Err_log ("fail to recv");
}
printf ("From client:%s\n", buf);
if (strncmp (buf, "Quit", 4) = = 0)
{
Break
}
strcat (BUF, "from server ...");
if (Send (CONFD, buf, N, 0) < 0)
{
Err_log ("fail to send.\n");
}
}

Close (SOCKFD);

return 0;
}

Where client.c are as follows

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

#define ERR_LOG (ErrLog) do{perror (ErrLog); exit (1);} while (0)
#define N 128

./client 192.168.0.196 10000

int main (int argc, const char *argv[])
{
int sockfd;
int CONFD;
struct sockaddr_in serveraddr, clientaddr;
Char Buf[n] = {0};

if (argc! = 3)

{

fprintf (stderr, "usage:%s serverip port.\n", argv[0]);

return-1;

}

if ((SOCKFD = socket (af_inet, sock_stream, 0)) < 0)

{

Err_log ("fail to Socket");

}

printf ("SOCKFD =%d\n", SOCKFD);

serveraddr.sin_family = af_inet;

SERVERADDR.SIN_ADDR.S_ADDR = inet_addr (argv[1]);

Serveraddr.sin_port = htons (Atoi (argv[2]));

if (Connect (SOCKFD, (struct sockaddr*) &serveraddr, sizeof (SERVERADDR)) < 0)

{

Err_log ("fail to connect");

}

while (1)

{

printf ("Input >");

Fgets (buf, N, stdin);

Buf[strlen (BUF)-1] = ' + ';

if (Send (SOCKFD, buf, N, 0) < 0)

{

Err_log ("fail to send");

}

if (strncmp (buf, "Quit", 4) = = 0)

{

Break

}

if (recv (SOCKFD, buf, N, 0) < 0)

{

Err_log ("fail to recv");

}

printf ("%s\n", buf);

}

Close (SOCKFD);

return 0;

}

The UDP model is as follows

where server.c

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

#define ERR_LOG (ErrLog) do{perror (ErrLog); exit (1);} while (0)
#define N 128

int main (int argc, const char *argv[])
{
int sockfd;
struct sockaddr_in serveraddr;
struct sockaddr_in clientaddr;
socklen_t Addrlen = sizeof (CLIENTADDR);
Char buf[n] = {};

if (ARGC < 3)
{
fprintf (stderr, "usage:%s serverip port.\n", argv[0]);
return-1;
}

if ((SOCKFD = socket (af_inet, SOCK_DGRAM, 0)) < 0)
{
Err_log ("fail to Socket");
}

serveraddr.sin_family = af_inet;
SERVERADDR.SIN_ADDR.S_ADDR = inet_addr (argv[1]);
Serveraddr.sin_port = htons (Atoi (argv[2]));

if (Bind (SOCKFD, (struct sockaddr*) &serveraddr, sizeof (SERVERADDR)) < 0)
{
Err_log ("Fail to bind");
}


while (1)
{
if (Recvfrom (SOCKFD, buf, N, 0, (struct sockaddr*) &clientaddr, &addrlen) < 0)
{
Err_log ("fail to Recvfrom");
}

printf ("From clientaddr:%s\n", buf);
strcat (BUF, "from server ...");

if (strncmp (buf, "Quit", 4) = = 0)
{
Break
}

if (SendTo (SOCKFD, buf, N, 0, (struct sockaddr *) &clientaddr, Addrlen) < 0)
{
Err_log ("fail to SendTo");
}

}

Close (SOCKFD);



return 0;
}

Among them client.c are as follows:

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

#define ERR_LOG (ErrLog) do{perror (ErrLog); exit (1);} while (0)
#define N 128

int main (int argc, const char *argv[])
{
int sockfd;
struct sockaddr_in serveraddr;
struct sockaddr_in clientaddr;
socklen_t Addrlen = sizeof (CLIENTADDR);
Char buf[n] = {};

if (ARGC < 3)
{
fprintf (stderr, "usage:%s serverip port.\n", argv[0]);
return-1;
}

if ((SOCKFD = socket (af_inet, SOCK_DGRAM, 0)) < 0)
{
Err_log ("fail to Socket");
}

serveraddr.sin_family = af_inet;
SERVERADDR.SIN_ADDR.S_ADDR = inet_addr (argv[1]);
Serveraddr.sin_port = htons (Atoi (argv[2]));

while (1)
{
printf ("Input >");
Fgets (buf, N, stdin);
Buf[strlen (BUF)-1] = ' + ';

if (SendTo (SOCKFD, buf, N, 0, (struct sockaddr *) &serveraddr, Addrlen) < 0)
{
Err_log ("fail to SendTo");
}

if (strncmp (buf, "Quit", 4) = = 0)
{
Break
}

if (Recvfrom (SOCKFD, buf, N, 0, NULL, NULL) < 0)
{
Err_log ("fail to Recvfrom");
}

printf ("%s\n", buf);

}

Close (SOCKFD);

return 0;
}

Linux Network programming--TCP/UDP programming model

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.