当前位置: 技术问答>linux和unix
一个LINUX下传文件的程序,各位帮忙看下
来源: 互联网 发布时间:2016-01-01
本文导语: /* Created by Anjuta version 1.2.4a */ /*This file will not be overwritten */ #include #include #include #include #include #include #include #include //inet_addr() #include //inet_ntoa() #include #include #include //waitpid(...
/* Created by Anjuta version 1.2.4a */
/*This file will not be overwritten */
#include
#include
#include
#include
#include
#include
#include
#include //inet_addr()
#include //inet_ntoa()
#include
#include
#include //waitpid()
#include
#include
#define MAXDATASIZE 100
#define MYPORT 3490 //the port users will be conecting to
#define BACKLOG 10 //how many pending connections will hold
#define SERVER_IP "192.168.111.147"
void my_client()
{
char *hostaddr = SERVER_IP; //"192.168.111.147";
int sockfd, numbytes;
int ho; //server
char buf[MAXDATASIZE];
struct hostent *he;
struct sockaddr_in their_addr; /* connector's address information */
if ((he=gethostbyname(hostaddr)) == NULL) { // get the host info
perror("gethostbyname");
exit(1);
}
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("socket");
exit(1);
}
their_addr.sin_family = AF_INET; /* host byte order */
their_addr.sin_port = htons(MYPORT); /* short, network byte order */
their_addr.sin_addr = *((struct in_addr *)he->h_addr);
bzero(&(their_addr.sin_zero), 8); /* zero the rest of the struct */
if ((ho=connect(sockfd, (struct sockaddr *)&their_addr, sizeof(struct sockaddr))) == -1) {
perror("connect");
exit(1);
}
char *input;
//scanf("commond please %s",input);
client_service(sockfd,input);
close(sockfd);
}
void my_server(){
int sockfd,new_fd;
struct sockaddr_in my_addr;
struct sockaddr_in their_addr;
int sin_size;
printf("%sn",SERVER_IP);
if((sockfd = socket(AF_INET,SOCK_STREAM,0))==-1){
perror("socket");
exit(1);
}
my_addr.sin_family = AF_INET; //host byte order
my_addr.sin_port = htons(MYPORT); //short network byte order
my_addr.sin_addr.s_addr = inet_addr(SERVER_IP);
bzero(&(my_addr.sin_zero),8); //zero the rest of struct
if(bind(sockfd,(struct sockaddr *)&my_addr,sizeof(struct sockaddr)) == -1){
perror("bind");
exit(1);
}
if(listen(sockfd,BACKLOG) == -1){
perror("listen");
exit(1);
}
while(1){ //accept loop
sin_size = sizeof(struct sockaddr_in);
if((new_fd =accept(sockfd,(struct sockaddr *)&their_addr,&sin_size)) == -1){
perror("accept");
continue;
}
printf("server: got connection from %sn",inet_ntoa(their_addr.sin_addr));
char input[MAXDATASIZE]; //暂时没用
if(!fork()){ //child process
server_service(new_fd,input);
close(new_fd);
exit(0);
}
close(new_fd);
while(waitpid(-1,NULL,WNOHANG) > 0);
//clean up child process
}
}
void client_service(int sockfd,char *buff) {
int source,k;
char buf[1024];//file[30];
char file[30];
FILE *fp;
char temb[MAXDATASIZE];
int pos;
if((pos =recv(sockfd,temb,MAXDATASIZE,0)) ==-1){
perror("recv");
}else{
temb[pos]='';
printf("%sn",temb);
}
printf("ninput a file name!!!n");
scanf("%s",file);
printf("u had just input %sn",file);
if((source = open(file,O_RDONLY))0)
write(sockfd,buf,k);
int bb;
char bbb[MAXDATASIZE];
if ((bb=recv(sockfd, bbb, MAXDATASIZE, 0)) == -1) {
perror("recv");
exit(1);
}else{
bbb[bb] = '';
printf("%sn",bbb);
}
close(source);
}
void server_service(int sockfd,char *buff) {
//if(strcmp(buff,"put") ==0){
int k,target;
FILE *fp;
char file[30];
char *str;
char buf[1024];
char *outstr = "oh yeah,i got u";
if(send(sockfd,outstr,strlen(outstr),0)==-1){
perror("send");
exit(1);
}
printf("getting file namen");
if((k=read(sockfd,file,sizeof(file)))0)
write(sockfd,buf,k);
int bb;
char bbb[MAXDATASIZE];
if ((bb=recv(sockfd, bbb, MAXDATASIZE, 0)) == -1) { /// 阻塞在这里
server:
while((k=read(sockfd,buf,sizeof(buf)))>0) /// 阻塞在这里
write(target,buf,k);
/*This file will not be overwritten */
#include
#include
#include
#include
#include
#include
#include
#include //inet_addr()
#include //inet_ntoa()
#include
#include
#include //waitpid()
#include
#include
#define MAXDATASIZE 100
#define MYPORT 3490 //the port users will be conecting to
#define BACKLOG 10 //how many pending connections will hold
#define SERVER_IP "192.168.111.147"
void my_client()
{
char *hostaddr = SERVER_IP; //"192.168.111.147";
int sockfd, numbytes;
int ho; //server
char buf[MAXDATASIZE];
struct hostent *he;
struct sockaddr_in their_addr; /* connector's address information */
if ((he=gethostbyname(hostaddr)) == NULL) { // get the host info
perror("gethostbyname");
exit(1);
}
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("socket");
exit(1);
}
their_addr.sin_family = AF_INET; /* host byte order */
their_addr.sin_port = htons(MYPORT); /* short, network byte order */
their_addr.sin_addr = *((struct in_addr *)he->h_addr);
bzero(&(their_addr.sin_zero), 8); /* zero the rest of the struct */
if ((ho=connect(sockfd, (struct sockaddr *)&their_addr, sizeof(struct sockaddr))) == -1) {
perror("connect");
exit(1);
}
char *input;
//scanf("commond please %s",input);
client_service(sockfd,input);
close(sockfd);
}
void my_server(){
int sockfd,new_fd;
struct sockaddr_in my_addr;
struct sockaddr_in their_addr;
int sin_size;
printf("%sn",SERVER_IP);
if((sockfd = socket(AF_INET,SOCK_STREAM,0))==-1){
perror("socket");
exit(1);
}
my_addr.sin_family = AF_INET; //host byte order
my_addr.sin_port = htons(MYPORT); //short network byte order
my_addr.sin_addr.s_addr = inet_addr(SERVER_IP);
bzero(&(my_addr.sin_zero),8); //zero the rest of struct
if(bind(sockfd,(struct sockaddr *)&my_addr,sizeof(struct sockaddr)) == -1){
perror("bind");
exit(1);
}
if(listen(sockfd,BACKLOG) == -1){
perror("listen");
exit(1);
}
while(1){ //accept loop
sin_size = sizeof(struct sockaddr_in);
if((new_fd =accept(sockfd,(struct sockaddr *)&their_addr,&sin_size)) == -1){
perror("accept");
continue;
}
printf("server: got connection from %sn",inet_ntoa(their_addr.sin_addr));
char input[MAXDATASIZE]; //暂时没用
if(!fork()){ //child process
server_service(new_fd,input);
close(new_fd);
exit(0);
}
close(new_fd);
while(waitpid(-1,NULL,WNOHANG) > 0);
//clean up child process
}
}
void client_service(int sockfd,char *buff) {
int source,k;
char buf[1024];//file[30];
char file[30];
FILE *fp;
char temb[MAXDATASIZE];
int pos;
if((pos =recv(sockfd,temb,MAXDATASIZE,0)) ==-1){
perror("recv");
}else{
temb[pos]='';
printf("%sn",temb);
}
printf("ninput a file name!!!n");
scanf("%s",file);
printf("u had just input %sn",file);
if((source = open(file,O_RDONLY))0)
write(sockfd,buf,k);
int bb;
char bbb[MAXDATASIZE];
if ((bb=recv(sockfd, bbb, MAXDATASIZE, 0)) == -1) {
perror("recv");
exit(1);
}else{
bbb[bb] = '';
printf("%sn",bbb);
}
close(source);
}
void server_service(int sockfd,char *buff) {
//if(strcmp(buff,"put") ==0){
int k,target;
FILE *fp;
char file[30];
char *str;
char buf[1024];
char *outstr = "oh yeah,i got u";
if(send(sockfd,outstr,strlen(outstr),0)==-1){
perror("send");
exit(1);
}
printf("getting file namen");
if((k=read(sockfd,file,sizeof(file)))0)
write(sockfd,buf,k);
int bb;
char bbb[MAXDATASIZE];
if ((bb=recv(sockfd, bbb, MAXDATASIZE, 0)) == -1) { /// 阻塞在这里
server:
while((k=read(sockfd,buf,sizeof(buf)))>0) /// 阻塞在这里
write(target,buf,k);