|
楼主 |
发表于 2007-11-20 21:34:00
|
显示全部楼层
二.用 循环的方法来解决
1.如果位图大小大于 1MB 的时候,server.bmp 最后的填充大小为 1k ,且填充的值全为0x00,send的返回值都比位图本身大小要小
例如: 位图大小为 1418486 bytes,而 send 返回值为 14480,recv循环返回值为 1.
2.如果位图大小约为 800k 的时候,server.bmp 最后的填充大小为 1K. 且填充的值全为0x00,send的返回值比位图本身大小要小
例如: 位图大小为 762054 bytes,而 send 返回值为 50680 ,recv循环返回值为 1.
如果位图大小为 600K 以下的话,就可以完全正确显示。
- --------client----------------
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <errno.h>
- #include <string.h>
- #include <unistd.h>
- #include <netinet/in.h>
- #include <netdb.h>
- #define SERVPORT 3333
- #define MAXDATASIZE 512*512+1024+54
- main( int argc , char *argv[] )
- {
- struct hostent *host;
- struct sockaddr_in serv_addr;
- int sockfd,sendbytes;
- char buf[MAXDATASIZE];
- char size[4];
- int bmpsize = 0,
- i,
- headersize = 0,
- pixelsize = 0,
- vector = 0,
- palette = 0;
- char bmpsizechar;
-
- FILE *fbmp;
- char bmpdata[MAXDATASIZE];
-
- if (( fbmp = fopen("client.bmp", "r")) == NULL)
- {
- printf("Open client bmp error !\n");
- exit(-1);
- }
- fseek(fbmp,10,SEEK_SET);
- fread(&headersize,4,1,fbmp);
- printf("This BMP's headersize = %d \n" , headersize); // 为 54 和1078
-
- fseek(fbmp,34,SEEK_SET);
- fread(&pixelsize,4,1,fbmp);
- printf("This BMP's pixelsize = %d \n" , pixelsize);
-
-
- fseek(fbmp,28,SEEK_SET);
- fread(&vector,2,1,fbmp);
- printf("This BMP's vector = %d \n" , vector);
-
- if(vector == 24 || vector == 18)
- bmpsize = 54 + pixelsize ;
- else
- bmpsize = 1078 + pixelsize ;
-
-
- printf("This BMP's bmpsize = %d \n" , bmpsize);
- fseek(fbmp,0,SEEK_SET);
- fread(bmpdata,bmpsize,1,fbmp);
-
- if( argc < 2) {
- fprintf(stderr, "Please enter the server's hostname!\n");
- exit(1);
- }
-
- if( (host = gethostbyname(argv[1])) == NULL ){
- perror("gethostbyname");
- exit(1);
- }
-
- if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1){
- perror("socket");
- exit(1);
- }
- printf("socket success!,socket = %d\n",sockfd);
-
- serv_addr.sin_family = AF_INET;
- serv_addr.sin_port = htons(SERVPORT);
- serv_addr.sin_addr = *( (struct in_addr *)host->h_addr );
-
- bzero(&(serv_addr.sin_zero), 8);
-
- if( connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(struct sockaddr)) == -1 ){
- perror("connect");
- exit(1);
- }
-
- printf("connect success!\n");
-
- if(( sendbytes = send(sockfd, bmpdata ,bmpsize, 0)) == -1){
- perror("send");
- exit(1);
- }
-
- printf("sent connection :%d\n", sendbytes );
-
-
- printf("send over!\n");
-
- fclose(fbmp);
- close(sockfd);
- }
- -----------------------------server--------------------
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <errno.h>
- #include <string.h>
- #include <unistd.h>
- #include <netinet/in.h>
- #define SERVPORT 3333
- #define BACKLOG 10
- #define MAX_CONNECTED_NO 10
- //#define MAXDATASIZE 800*800+1024+54
- int main()
- {
- struct sockaddr_in server_sockaddr,client_sockaddr;
- int sin_size,recvbytes;
- int sockfd,client_fd;
- int nwrite,i;
-
- char size[4];
- volatile int bmpsize = 800*800;
- int pixelsize,vector;
-
- FILE *fbmp;
- char bmpdata[MAXDATASIZE];
- char *p;
-
-
- if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1){
- perror("socket");
- exit(1);
- }
- printf("socket success!,sockfd = %d\n",sockfd);
-
- server_sockaddr.sin_family = AF_INET;
- server_sockaddr.sin_port = htons(SERVPORT);
- server_sockaddr.sin_addr.s_addr = INADDR_ANY;
-
- bzero(&(server_sockaddr.sin_zero), 8);
-
- if(bind(sockfd, (struct sockaddr *)&server_sockaddr, sizeof(struct sockaddr)) == -1 ){
- perror("bind");
- exit(1);
- }
-
- printf("bind success!\n");
-
-
- if(listen(sockfd, BACKLOG) == -1){
- perror("listen");
- exit(1);
- }
-
- printf("listening - - - \n");
-
- if( (client_fd = accept(sockfd, (struct sockaddr *)&client_sockaddr, &sin_size)) == -1 ){
- perror("accept");
- exit(1);
- }
-
- printf("accept is ok ! \n");
-
- i = 0;
-
- while(1){
-
- if(( recvbytes = recv(client_fd, &bmpdata[i], 1, 0)) == -1){
- perror("recv");
- exit(1);
- }
-
- if(i ==54){
- pixelsize = ((bmpdata[37] & 0x000000ff) *256*256*256 ) + ((bmpdata[36] & 0x000000ff) *256*256 ) \
- + ( (bmpdata[35] & 0x000000ff) *256 ) + (bmpdata[34] & 0x000000ff) *1 ;
- vector =( bmpdata[28] & 0x000000ff) *1 ;
-
- if(vector == 24 || vector == 18)
- bmpsize = 54 + pixelsize ;
- else
- bmpsize = 1078 + pixelsize ;
-
- printf("pixelnumber = %d\n",pixelsize);
- printf("size is change for = %d\n",bmpsize);
- }
-
-
- i++;
-
- if( i == bmpsize)
- break;
-
- }
-
- printf("receiving bytes' number is :%d\n", i);
- printf("received a connection :%d\n", recvbytes );
- if (( fbmp = fopen("server.bmp", "w+")) == NULL)
- {
- printf("Open server bmp error !\n");
- exit(-1);
- }
-
- if ( nwrite = fwrite (bmpdata, bmpsize,1, fbmp ) < 0)
- {
- printf("Write BMP error !\n");
- exit(-1);
- }
- fclose(fbmp);
- close(sockfd);
- }
复制代码 |
|