赞
踩
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <sys/types.h> /* See NOTES */ #include <sys/socket.h> #include <time.h> #include <sys/socket.h> #include <netinet/in.h> #include <netinet/ip.h> /* superset of previous */ #include <arpa/inet.h> #include <sys/stat.h> #include <fcntl.h> typedef struct sockaddr*(SA); int main(int argc, const char *argv[]) { int conn = socket(AF_INET,SOCK_STREAM,0); if(-1 == conn) { perror("conn error!\n"); exit(1); } struct sockaddr_in ser; bzero(&ser,sizeof(ser)); ser.sin_family = AF_INET; ser.sin_port = htons(50000); ser.sin_addr.s_addr = inet_addr("192.168.1.139"); int ret = connect(conn,(SA)&ser,sizeof(ser)); if(-1 == ret) { perror("connect error!\n"); exit(1); } char buf[1024] = {0}; int fd = open("/home/linux/11.jpg",O_RDONLY); if(-1 == fd) { perror("open error!\n"); exit(1); } while(1) { int rd_num = read(fd,buf,1024); if(0 == rd_num) { break; } send(conn,buf,rd_num,0); bzero(buf,sizeof(buf)); } close(fd); close(conn); return 0; }
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <sys/types.h> /* See NOTES */ #include <sys/socket.h> #include <time.h> #include <sys/socket.h> #include <netinet/in.h> #include <netinet/ip.h> /* superset of previous */ #include <arpa/inet.h> #include <sys/stat.h> #include <fcntl.h> typedef struct sockaddr*(SA); int main(int argc, const char *argv[]) { int listfd = socket(AF_INET,SOCK_STREAM,0); if(-1 == listfd) { perror("socket error!\n"); exit(1); } struct sockaddr_in ser,cli; bzero(&ser,sizeof(ser)); bzero(&cli,sizeof(cli)); ser.sin_family = AF_INET; ser.sin_port = htons(50000); ser.sin_addr.s_addr = INADDR_ANY; int ret = bind(listfd,(SA)&ser,sizeof(ser)); if(-1 == ret) { perror("bind error!\n"); exit(1); } listen(listfd,3); int len = sizeof(cli); int conn = accept(listfd,(SA)&cli,&len); if(-1 == conn) { perror("accept error!\n"); exit(1); } int fd = open("./1.jpg",O_WRONLY|O_CREAT|O_TRUNC,0666); if(-1 == fd) { perror("open error!\n"); exit(1); } while(1) { char buf[1024] = {0}; int rd = recv(conn,buf,sizeof(buf),0); if(0 == rd)//客户端断开退出 { break; } write(fd,buf,rd); bzero(buf,sizeof(buf)); } close(fd); close(listfd); close(conn); return 0; }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。