tcpserver.c tcpclient.c proto.h tcpserver.c tcpclient.c proto.h
3.1 传递时间示例代码
#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 %dn",p_raddr,ntohs(raddr.sin_port)); server_job(std); close(std); exit(0); //子进程结束必须exit0,否则会继续执行 } close(std); //此处必须有,计数减为0,然后才会发送FIN,四次分手 } close(sd); exit(0); }
#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 = %lldn",stamp); } fclose(fp); //这里需要关闭fp,因为fdopen已经转成fp exit(0); }
#ifndef PROTO_H__ #define PROTO_H__ #define LOCALPORT 1990 #define FMT_STAMP "%lldrn" #endif
3.2 传递图片代码
#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 %dn",p_raddr,ntohs(raddr.sin_port)); server_job(std); close(std); exit(0); //子进程结束必须exit0,否则会继续执行 } close(std); //此处必须有,计数减为0,然后才会发送FIN,四次分手 } close(sd); exit(0); }
#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 errorn"); 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,"endn"); } fclose(fp); //这里需要关闭fp,因为fdopen已经转成fp exit(0); }
#ifndef PROTO_H__ #define PROTO_H__ #define LOCALPORT 1990 #define FMT_STAMP "%lldrn" #endif
本网页所有视频内容由 imoviebox边看边下-网页视频下载, iurlBox网页地址收藏管理器 下载并得到。
ImovieBox网页视频下载器 下载地址: ImovieBox网页视频下载器-最新版本下载
本文章由: imapbox邮箱云存储,邮箱网盘,ImageBox 图片批量下载器,网页图片批量下载专家,网页图片批量下载器,获取到文章图片,imoviebox网页视频批量下载器,下载视频内容,为您提供.
阅读和此文章类似的: 全球云计算