- 论坛徽章:
- 0
|
改了一下,可以了
- 客户端程序:
- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
- #include<sys/socket.h>
- #include<sys/types.h>
- #include<netdb.h>
- #include<netinet/in.h>
- #include<unistd.h>
- #include<time.h>
-
- #define SERVERPORT 5555
- int main(int argc,char *argv[])
- {
- int sockfd,connectfd,sendfd = 0 ;
- struct sockaddr_in serv_addr;
- struct hostent *host;
- FILE *fp = NULL;
- char ch[256] = {0};
- if(argc!=3)
- {
- printf("format: %s IP file_name\n",argv[0]);
- return 0;
- }
- if((host=(gethostbyname(argv[1])))==NULL)
- {
- printf("error in gethostby name. \n");
- return 0;
- }
- if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1)
- {
- printf("error in socket create.\n");
- return 0;
- }
- 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), sizeof(serv_addr.sin_zero));
- if((connectfd=connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(struct sockaddr)))==-1)
- {
- printf("error in connect.\n");
- return 0;
- }
- fp=fopen(*(argv+2), "rb");
- while(!feof(fp))
- {
- memset(ch, 0x0, 256);
- sendfd=fread(ch, 1, 256, fp);
- //printf("[debug], read %d bytes,send buf:%s\n", sendfd, ch);
- //printf("\n");
-
- int sendBytes = 0;
- sendBytes = send(sockfd,ch,sendfd,0);
- //printf("[debug]: send %d bytes\n", sendBytes);
- sendfd = 0;
- }
- fclose(fp);
- close(sockfd);
- return 0;
- }
- 服务器端:
- #include<stdio.h>
- #include<string.h>
- #include<sys/socket.h>
- #include<sys/types.h>
- #include<time.h>
- #include<stdlib.h>
- #include<netinet/in.h>
- #include<netdb.h>
- #include<unistd.h>
- #define SERVERPORT 5555
- #define BACKLOG 10
- int main(int agrc,char *agrv[])
- {
- int sockfd,bindfd,listenfd,acceptfd,recvfd;
- struct sockaddr_in myaddr;
- struct sockaddr_in myclient;
- char ch[256] = {0x0};
- FILE * fp;
- if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1)
- {
- printf("error! in socket create!\n");
- return 0;
- }
- myaddr.sin_family=AF_INET;
- myaddr.sin_port=htons(SERVERPORT);
- myaddr.sin_addr.s_addr=htonl(INADDR_ANY);
- bzero(&(myaddr.sin_zero), sizeof(myaddr.sin_zero));
-
- int opt=SO_REUSEADDR;
- setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&opt,sizeof(opt));
- if((bindfd=bind(sockfd,(struct sockaddr *)&myaddr,sizeof(myaddr)))==-1)
- //if((bindfd=bind(sockfd,(struct sockaddr *)&myaddr,sizeof(myaddr)))==-1)
- {
- printf("errer in bind\n");
- //close(sockfd);
- return 0;
- }
- if((listenfd=listen(sockfd,BACKLOG))==-1)
- {
- printf("error! the queue_listen is full!\n");
- return 0;
- }
-
- int len_addr=sizeof(struct sockaddr_in);
- if((acceptfd=accept(sockfd,(struct sockaddr *)&myclient,&len_addr ))==-1)
- {
- printf("error in accept.\n");
- return 0;
- }
- fp=fopen("copy_file.txt","wb");
- recvfd = 0;
- do{
- memset(ch, 0x0, 256);
- if((recvfd=recv(acceptfd,ch,256,0))==-1)
- {
- printf("error in recv.\n");
- break;
- }
-
- //printf("[debug],recv %d bytes, content: %s\n", recvfd, ch);
- fwrite(ch, 1, recvfd, fp);
- fflush(fp);
- }while(recvfd > 0);
- fclose(fp);
- close(sockfd);
- return 0;
- }
复制代码 |
|