UNP——TCP分析
作者:互联网
3.1 传递时间示例代码
tcpserver.c
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <time.h>
#include <arpa/inet.h>
#include "proto.h"
#define bakc_log 50
#define praddr_size 50
#define buffer_size 1024
static void server_job(int sockfd)
{
int len;
char buffer[buffer_size];
len = sprintf(buffer,FMT_STAMP, (long long)time(NULL));
if(send(sockfd, buffer, len, 0)<0)
{
perror("send");
exit(1);
}
}
int main()
{
int sd,std;
int flag;
pid_t pid;
char p_raddr[praddr_size];
struct sockaddr_in laddr,raddr;
laddr.sin_family = AF_INET;
laddr.sin_port = htons(LOCALPORT);
inet_pton(AF_INET,"0.0.0.0",&laddr.sin_addr);
socklen_t laddrsize = sizeof(laddr);
socklen_t raddrsize = sizeof(raddr);
sd = socket(AF_INET,SOCK_STREAM,0);
if(sd<0)
{
perror("socket");
exit(1);
}
if(setsockopt(sd,SOL_SOCKET,SO_REUSEADDR,&flag,sizeof(flag))<0)
{
perror("setsocket");
exit(1);
}
if(bind(sd, (void*)&laddr, laddrsize)<0)
{
perror("bind");
exit(1);
}
if(listen(sd,bakc_log)<0)
{
perror("listen");
exit(1);
}
while(1)
{
//accept返回三个值,std即可能是已连接套接字或错误信息,对端地址,对端地址大小
std = accept(sd, (void*)&raddr,&raddrsize); //返回值为全新文件描述符,代表与tcp对端连接,已连接描述符
if(std < 0)
{
perror("accept");
exit(1);
}
pid = fork(); //并发的时候,应该注意:fork之后,子进程和父进程均有已连接描述符和监听描述符,父进程中需close已连接描述符
//,将引用计数减为0
if(pid == 0)
{
close(sd);
inet_ntop(AF_INET,&raddr.sin_addr,p_raddr,praddr_size);
fprintf(stderr,"Client addr is %s , port is %d\n",p_raddr,ntohs(raddr.sin_port));
server_job(std);
close(std);
exit(0); //子进程结束必须exit0,否则会继续执行
}
close(std); //此处必须有,计数减为0,然后才会发送FIN,四次分手
}
close(sd);
exit(0);
}
tcpclient.c
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <arpa/inet.h>
#include <stdio.h>
#include "proto.h"
#define raddr_size 40
int main(int argc, char* argv[])
{
FILE* fp;
long long stamp;
struct sockaddr_in laddr,raddr;
char raddr_ip[raddr_size];
if(argc < 2)
{
fprintf(stderr,"argc");
exit(1);
}
int sd = socket(AF_INET,SOCK_STREAM,0);
if(sd<0)
{
perror("socket");
exit(1);
}
raddr.sin_family = AF_INET;
raddr.sin_port = htons(LOCALPORT);
inet_pton(AF_INET,argv[1],&raddr.sin_addr);
fprintf(stderr,"let's go");
if(connect(sd,(void*)&raddr,sizeof(raddr))<0) //conncet开始三次握手,可能出现三种情况1、FIN无ACK,75s后ETIMEOUT
{
perror("connect");
exit(1);
}
//一切皆文件
fp = fdopen(sd,"r+"); //将文件描述符转换成FILE指针打开,这里不能用w,w+,因无需创建fd
if(fp==NULL)
{
perror("fdopen");
exit(1);
}
if(fscanf(fp,FMT_STAMP,&stamp)<1) //返回成功读取的个数,这里只有一个item,正确为1
{
fprintf(stderr,"error FMT_STAMP");
}
else
{
fprintf(stderr,"stamp = %lld\n",stamp);
}
fclose(fp); //这里需要关闭fp,因为fdopen已经转成fp
exit(0);
}
proto.h
#ifndef PROTO_H__
#define PROTO_H__
#define LOCALPORT 1990
#define FMT_STAMP "%lld\r\n"
#endif
3.2 传递图片代码
tcpserver.c
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <time.h>
#include <arpa/inet.h>
#include "proto.h"
#define bakc_log 50
#define praddr_size 50
#define buffer_size 1024
static void server_job(int sockfd)
{
int len;
char buffer[buffer_size];
size_t num;
FILE* fp;
//len = sprintf(buffer,FMT_STAMP, (long long)time(NULL));
fp = fopen("/home/qianchen/Pictures/muniao.jpg","r");
if(fp == NULL)
{
perror("fopen error");
exit(1);
}
while(1)
{
num = fread(buffer,1,buffer_size,fp);
if(num == 0)
{
perror("fread error");
break;
}
if(send(sockfd, buffer, num, 0)<0) //输出重定向./tcpclient 127.0.0.1 > /home/qianchen/Videos/tcp/1.jpg
{
perror("send");
exit(1);
}
}
fclose(fp);
}
int main()
{
int sd,std;
int flag;
pid_t pid;
char p_raddr[praddr_size];
struct sockaddr_in laddr,raddr;
laddr.sin_family = AF_INET;
laddr.sin_port = htons(LOCALPORT);
inet_pton(AF_INET,"0.0.0.0",&laddr.sin_addr);
socklen_t laddrsize = sizeof(laddr);
socklen_t raddrsize = sizeof(raddr);
sd = socket(AF_INET,SOCK_STREAM,0);
if(sd<0)
{
perror("socket");
exit(1);
}
if(setsockopt(sd,SOL_SOCKET,SO_REUSEADDR,&flag,sizeof(flag))<0)
{
perror("setsocket");
exit(1);
}
if(bind(sd, (void*)&laddr, laddrsize)<0)
{
perror("bind");
exit(1);
}
if(listen(sd,bakc_log)<0)
{
perror("listen");
exit(1);
}
while(1)
{
//accept返回三个值,std即可能是已连接套接字或错误信息,对端地址,对端地址大小
std = accept(sd, (void*)&raddr,&raddrsize); //返回值为全新文件描述符,代表与tcp对端连接,已连接描述符
if(std < 0)
{
perror("accept");
exit(1);
}
pid = fork(); //并发的时候,应该注意:fork之后,子进程和父进程均有已连接描述符和监听描述符,父进程中需close已连接描述符
//,将引用计数减为0
if(pid == 0)
{
close(sd);
inet_ntop(AF_INET,&raddr.sin_addr,p_raddr,praddr_size);
fprintf(stderr,"Client addr is %s , port is %d\n",p_raddr,ntohs(raddr.sin_port));
server_job(std);
close(std);
exit(0); //子进程结束必须exit0,否则会继续执行
}
close(std); //此处必须有,计数减为0,然后才会发送FIN,四次分手
}
close(sd);
exit(0);
}
tcpclient.c
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <arpa/inet.h>
#include <stdio.h>
#include "proto.h"
#define raddr_size 40
#define JPGBUFFERSIZE 1024
int main(int argc, char* argv[])
{
FILE* fp;
size_t num;
long long stamp;
char jpgbuffer[JPGBUFFERSIZE];
struct sockaddr_in laddr,raddr;
char raddr_ip[raddr_size];
if(argc < 2)
{
fprintf(stderr,"argc error\n");
exit(1);
}
int sd = socket(AF_INET,SOCK_STREAM,0);
if(sd<0)
{
perror("socket");
exit(1);
}
raddr.sin_family = AF_INET;
raddr.sin_port = htons(LOCALPORT);
inet_pton(AF_INET,argv[1],&raddr.sin_addr);
fprintf(stderr,"let's go");
if(connect(sd,(void*)&raddr,sizeof(raddr))<0) //conncet开始三次握手,可能出现三种情况1、FIN无ACK,75s后ETIMEOUT
{
perror("connect");
exit(1);
}
//一切皆文件
fp = fdopen(sd,"r+"); //将文件描述符转换成FILE指针打开,这里不能用w,w+,因无需创建fd
if(fp==NULL)
{
perror("fdopen");
exit(1);
}
fprintf(stderr,"Let's go!\n");
while(1)
{
num = fread(jpgbuffer,1,JPGBUFFERSIZE,fp);
if(num==0)
{
fprintf(stderr,"fread");
break;
}
fwrite(jpgbuffer,1,num,stdout);
fprintf(stderr,"end\n");
}
fclose(fp); //这里需要关闭fp,因为fdopen已经转成fp
exit(0);
}
proto.h
#ifndef PROTO_H__
#define PROTO_H__
#define LOCALPORT 1990
#define FMT_STAMP "%lld\r\n"
#endif
3.3 静态进程池tcp连接
tcppoolserver.c
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <time.h>
#include <arpa/inet.h>
#include <sys/wait.h>
#include "proto.h"
#define POOL_SIZE 3
#define BAKC_LOG 50
#define PRADDER_SIZE 50
#define BUFFER_SIZE 1024
int sd;
int flag;
pid_t pid;
char p_raddr[PRADDER_SIZE];
struct sockaddr_in laddr,raddr;
socklen_t laddrsize = sizeof(laddr);
socklen_t raddrsize = sizeof(raddr);
static void do_it(int sockfd);
static void server_job(int sockfd)
{
int len;
char buffer[BUFFER_SIZE];
size_t num;
len = sprintf(buffer,FMT_STAMP, (long long)time(NULL));
if(send(sockfd,buffer,len,0)<0)
{
perror("send");
exit(1);
}
}
int main()
{
laddr.sin_family = AF_INET;
laddr.sin_port = htons(LOCALPORT);
inet_pton(AF_INET,"0.0.0.0",&laddr.sin_addr);
sd = socket(AF_INET,SOCK_STREAM,0);
if(sd<0)
{
perror("socket");
exit(1);
}
if(setsockopt(sd,SOL_SOCKET,SO_REUSEADDR,&flag,sizeof(flag))<0)
{
perror("setsocket");
exit(1);
}
if(bind(sd, (void*)&laddr, laddrsize)<0)
{
perror("bind");
exit(1);
}
if(listen(sd,BAKC_LOG)<0) //多个c端握手,内核保证BAKC_LOG大小的两个队列,进行握手
{
perror("listen");
exit(1);
}
for(int i=0; i<POOL_SIZE; i++) //产生静态进程池,用这三个进程处理
{
pid = fork();
if(pid<0)
{
perror("fork");
exit(1);
}
if(pid == 0)
{
do_it(sd);
exit(0);
}
}
for(int i=0; i<POOL_SIZE; i++)
{
wait(NULL);
}
close(sd);
exit(0);
}
static void do_it(int sockfd)
{
int std;
while(1)
{
//accept返回三个值,std即可能是已连接套接字或错误信息,对端地址,对端地址大小
std = accept(sockfd, (void*)&raddr,&raddrsize); //返回值为全新文件描述符,代表与tcp对端连接,已连接描述符
if(std < 0)
{
perror("accept");
exit(1);
}
//close(sockfd); 注意,这里不能close(sockfd),否则在while里,运行POOL_SIZE次后,fd会被close掉
inet_ntop(AF_INET,&raddr.sin_addr,p_raddr,PRADDER_SIZE);
fprintf(stderr,"Client addr is %s , port is %d, pid is %d\n",p_raddr,ntohs(raddr.sin_port),getpid());
server_job(std);
close(std);
//exit(0); //子进程结束必须exit0,否则会继续执行
}
// }
// close(std); //此处必须有,计数减为0,然后才会发送FIN,四次分手
}
tcppollclient.c
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <arpa/inet.h>
#include <stdio.h>
#include "proto.h"
#define raddr_size 40
int main(int argc, char* argv[])
{
FILE* fp;
long long stamp;
struct sockaddr_in laddr,raddr;
char raddr_ip[raddr_size];
if(argc < 2)
{
fprintf(stderr,"argc");
exit(1);
}
int sd = socket(AF_INET,SOCK_STREAM,0);
if(sd<0)
{
perror("socket");
exit(1);
}
raddr.sin_family = AF_INET;
raddr.sin_port = htons(LOCALPORT);
inet_pton(AF_INET,argv[1],&raddr.sin_addr);
fprintf(stderr,"let's go");
if(connect(sd,(void*)&raddr,sizeof(raddr))<0) //conncet开始三次握手,可能出现三种情况1、FIN无ACK,75s后ETIMEOUT
{
perror("connect");
exit(1);
}
//一切皆文件
fp = fdopen(sd,"r+"); //将文件描述符转换成FILE指针打开,这里不能用w,w+,因无需创建fd
if(fp==NULL)
{
perror("fdopen");
exit(1);
}
if(fscanf(fp,FMT_STAMP,&stamp)<1) //返回成功读取的个数,这里只有一个item,正确为1
{
fprintf(stderr,"error FMT_STAMP");
}
else
{
fprintf(stderr,"stamp = %lld\n",stamp);
}
fclose(fp); //这里需要关闭fp,因为fdopen已经转成fp
exit(0);
}
#ifndef PROTO_H__
#define PROTO_H__
#define LOCALPORT 1990
#define FMT_STAMP "%lld\r\n"
#endif
标签:UNP,分析,raddr,int,size,TCP,buffer,include,define 来源: https://blog.csdn.net/weixin_44537992/article/details/106772982