tcp 模型如下:
上面的模型已经很清楚了
具体函数用法就不细说了
请看tcp简单的例子:
其中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, 10) < 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 --> %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;
}
其中client.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
// ./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] = '\0';
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;
}
udp模型如下
其中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;
}
其中client.c 如下:
#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] = '\0';
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;
}