这几天一直被这个问题困扰着
编写了一个socket 传送BMP 图像的程序
client 向 server 传送client.bmp
但是在server 端接受到的图像不能打开
用 UltraEdit 打开client发送的 client.bmp图像和server接受的图像 server.bmp 对比,
server.bmp文件的前面 1448个字节 与 client.bmp的前1448个字节一样
但是server.bmp文件第1448个字节后的全部字节都为 0x00了
所以 server.bmp 所显示出来的几乎就是黑色的图片了
我的问题是 ---》 传送的是非0值,为什么收了一部分后就全变为0了呢?
请大虾们指点一下,我参考了资料+百度这个问题,还是百思不得其解。
我把我的代码附上[code]----------server-----------
#include
#include
#include
#include
#include
#include
#include
#include
#define SERVPORT 3333
#define BACKLOG 10
#define MAX_CONNECTED_NO 10
#define MAXDATASIZE 100*100*3+54
int main()
{
struct sockaddr_in server_sockaddr,client_sockaddr;
int sin_size,recvbytes;
int sockfd,client_fd;
char buf[MAXDATASIZE];
int nwrite,i;
FILE *fbmp;
char bmpdata[100*100*3+54];
if (( fbmp = fopen("server.bmp", "w+")) == NULL)
{
printf("Open server bmp error !\n");
exit(-1);
}
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);
}
if(( recvbytes = recv(client_fd, bmpdata, MAXDATASIZE, 0)) == -1){
perror("recv");
exit(1);
}
for( i = 0 ; i< MAXDATASIZE ; i++ )
{
printf("%x - ",bmpdata[i] ) ;
}
if ( nwrite = fwrite (bmpdata,MAXDATASIZE,1, fbmp ) < 0)
{
printf("Write BMP error !\n");
exit(-1);
}
fclose(fbmp);
close(sockfd);
}
---------------client---------------------
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define SERVPORT 3333
#define MAXDATASIZE 100*100*3+54
main( int argc , char *argv[] )
{
struct hostent *host;
struct sockaddr_in serv_addr;
int sockfd,sendbytes;
char buf[MAXDATASIZE];
int bmpsize = 0,
i,
headersize = 0,
pixelsize = 0;
FILE *fbmp;
char bmpdata[100*100*3+54];
if (( fbmp = fopen("client.bmp", "r")) == NULL)
{
printf("Open client bmp error !\n");
exit(-1);
}
fseek(fbmp,0,SEEK_SET);
fread(bmpdata,MAXDATASIZE,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);
}
for( i = 0 ; i< MAXDATASIZE ; i++ )
{
printf("%x - ",bmpdata[i] ) ;
}
printf("send over!\n");
fclose(fbmp);
close(sockfd);
}[/code]
rambo0808 于 2007-11-22 13:58:47发表:
〈B〉怎么没有朋友近来啊〈/B〉
怎么没有朋友近来啊
[ 本帖最后由 rambo0808 于 2007-11-22 14:02 编辑 ]
rambo0808 于 2007-11-19 21:36:54发表:
#define MAXDATASIZE 100*100*3+54
我传的一个确定的BMP