Send File In C Socket

 Send File In C Socket(2010-08-28 20:19:57)

標籤:sendfile c socket 教育 分類:C語言

 

摘自:blog.sina.com.cn/rekken

Send File In C Socket
 
通過SOCKET發送文件可以按如下步驟進行操作:
1. Open socket connection to remote server.
2. Open file on local system.
3. Read x bytes from file on local system.
4. Write x bytes to socket.
5. Repeat 3 & 4 until all bytes have been read from file and transmitted to remote system.
6. Close file.
7. Close socket.
運行流程
運行server端,開始Listen,運行客戶端輸入主機地址和文件目錄fileDir,例如:./client 202.206.32.9 test.txt
server接收(recv())此文件路徑,打開文件,讀取文件至一個數組sendout[]
server發送(send())載有文件內容的數組sendout[]至client
client接收(recv())數組信息,存放在另一個數組recvfile[]裏
client將recvfile[]裏的信息寫如test.txt裏(此文件同fileDir)
client.c
#include <sys/types.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <string.h>
#include <netdb.h>
#define SERVERPORT 3490
main(int argc,char *argv[])
{
//scokfd:a socket descriptor,  sendbytes and recvbytes used for send() and recv(),
//i :used for 
int sockfd,sendbytes,recvbytes;
FILE *fp; //file pointer
char recvfile[65535]; //store the data received from the server
char *recvdata; //pointer for recvfile[]
char *fileDir=argv[2]; //argv[2] store the file dir received from the keyboard
struct hostent *host; //for dns transmition
struct sockaddr_in serv_addr; //store the ip and other server info
if(argc<3){
fprintf(stderr,"Please enter AS: servername fileDirectory/n");
exit(1);
}
 
//address resolution function
if((host=gethostbyname(argv[1]))==NULL){
perror("gethostbyname");
exit(1);
}
 
//file directory not NULL
 
if(fileDir==NULL){
perror("file");
exit(1);
}
 
//create socket
if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1){
perror("socket");
exit(1);
}
 
//set relevant parameters in the socketr_in struct
serv_addr.sin_family=AF_INET;
serv_addr.sin_port=htons(SERVERPORT);
serv_addr.sin_addr=*((struct in_addr *)host->h_addr);
bzero(&(serv_addr.sin_zero),8);
 
//call the connect() function to connect to server intiative
if(connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(struct sockaddr))==-1){
perror("connect");
exit(1);
}
 
//send message to sever
 
if((sendbytes=send(sockfd,fileDir,120,0))==-1){
perror("send");
exit(1);
}
printf("fileDir:%s/n",argv[2]);
 
//receive the file from the server
recvdata=recvfile;
if((recvbytes=recv(sockfd,recvdata,65535,0))==-1){
 
perror("receive file:");
exit(1);
}
 
 
//write the data the file
if((fp=fopen(fileDir,"wt"))==NULL){
printf("Can't write data into %s/n",fileDir);
exit(1);
}
fputs(recvfile,fp);
 
//close the file pointer
fclose(fp);
 
close(sockfd);
}
server.c
#include <sys/types.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <netinet/in.h>
#include <unistd.h>
#include <string.h>
#define SERVERPORT 3490
#define BACKLOG 10
#define MAX_CONNECTED_NO 10
#define MAXDATASIZE 120
int main()
{
//declare the variable to be used
FILE *fp; //used for file handle
char *data; //a pointer used for receive the data from the file
char sendout[65535]; //used for store the data retrived from the file
struct sockaddr_in server_sockaddr,client_sockaddr; //for create socket
int sin_size,recvbytes,sendbytes,i; //recv and send used for recv() and send()
int sockfd,client_fd; //for socket creation
char buf[MAXDATASIZE]; //store the file dir from recv()
 
//creat socket connection
if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1){
perror("socket");
exit(1);
}
printf("socket success!,sockfd=%d/n",sockfd);
 
//set relevant parameters in the socketr_in struct
server_sockaddr.sin_family=AF_INET;
server_sockaddr.sin_port=htons(SERVERPORT);
server_sockaddr.sin_addr.s_addr=INADDR_ANY;
bzero(&(server_sockaddr.sin_zero),8);
 
//bind the sockfd and port
if(bind(sockfd,(struct sockaddr *)&server_sockaddr,sizeof(struct sockaddr))==-1){
perror("bind");
exit(1);
}
printf("bind success!/n");
 
//begin to listen
if(listen(sockfd,BACKLOG)==-1){
perror("listen");
exit(1);
}
printf("listening..../n");
 
//call the accept(),wait for the peer's connecting
if((client_fd=accept(sockfd,(struct sockaddr *)&client_sockaddr,&sin_size))==-1){
perror("accept");
exit(1);
}
 
//receive the data from the peer
if((recvbytes=recv(client_fd,buf,sizeof(buf),0))==-1){
perror("recv");
exit(1);
}
printf("received a connnection:%s/n",buf);
 
//open the file as the accepted dir in buf
 
if((fp=fopen(buf,"r"))==NULL){
printf("Can't open the file/n");
exit(1);
}
 
//read data from the file
data=sendout;
*data=fgetc(fp);
while(*data!=EOF){
data++;
*data=fgetc(fp);
};
printf("%s/n",sendout);
 
 
fclose(fp);
 
//send the sendout to client
if((sendbytes=send(client_fd,sendout,65535,0))==-1){
perror("send file");
exit(1);
}
 
//close(sockfd);
close(sockfd);
}
發佈了31 篇原創文章 · 獲贊 1 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章