从客户端发送到服务器时无法打开.docx文件

时间:2013-03-31 05:39:16

标签: c sockets client-server

我正在从客户端向服务器发送文件.docx,但是当我尝试在服务器上打开它时,我无法打开它。 如果结束.txt文件,它也可以正确打开而不是.docx文件。 我有什么遗失的吗? 感谢

客户端代码

void error(char *msg)
{
perror(msg);
exit(0);
}

int main(int argc,char *argv[])
{   
FILE *fp;
size_t bytes_read;
int sockfd = 0,n = 0,i;
char recvBuffer[10],option;
struct sockaddr_in server_addr;

if(argc !=2)
{
    printf("\nUsage: %s <ip of server> \n",argv[0]);
    return 1;
}

memset(recvBuffer,'0',sizeof(recvBuffer));
if((sockfd = socket(AF_INET,SOCK_STREAM,0)) < 0)
{
    printf("\n Error : Could not create socket \n");
    return 1;
}
 // printf("%d",sockfd);

memset(&server_addr,'0',sizeof(server_addr));

server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(5000);

if(inet_pton(AF_INET,argv[1],&server_addr.sin_addr) <=0)
{
    printf("\n inet_pton error occureed");
    return  1;
}

if(connect(sockfd,(struct sockaddr *)&server_addr,sizeof(server_addr)) < 0)
{
    printf("Connect failed");
    return 1;
}

    //while(1)
//{
    //printf("Please enter the message you want to send\n");
    fp = fopen("CMEA_projectreport.docx","r");

    //for(i=0;i<10;i++)
    while(!feof(fp))
    {
        bytes_read = fread(recvBuffer,1,sizeof(recvBuffer),fp);
        n = send(sockfd,recvBuffer,bytes_read,0);
        //printf("%zd",bytes_read);         
        if(n<0)
            error("Error writing to socket");
        bzero(recvBuffer,10);
        n = read(sockfd,recvBuffer,sizeof(recvBuffer)-1);

        if(n<0)
            error("Error reading from socket");
        printf("%s %d\n",recvBuffer,i);
    /*  printf("Do u want to continue y or n\n");
        scanf("%c",&option);
        if(option !='y'){
        n = send(sockfd,"exit",4,0);
        else
            goto repeat;
        exit(0);   */
    }
        fclose(fp);
        if(n < 0)
            error("ERROR writing to socket exit statement");

//  }   

    close(sockfd);
    return 0;
}

服务器代码:

void error(char *msg)
{
perror(msg);
exit(0);
}

 int main(int argc,char *argv[])
 {
FILE *fo;
size_t bytes_read,bytes_written;
int listenfd = 0, connfd=0, clilen,n;
struct sockaddr_in server_addr,cli_addr;
char sendBuffer[1024];


listenfd = socket(AF_INET,SOCK_STREAM,0);
memset(&server_addr,'0',sizeof(server_addr));
memset(sendBuffer,'0',sizeof(sendBuffer));

server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
server_addr.sin_port = htons(5000);

bind(listenfd,(struct sockaddr*)&server_addr,sizeof(server_addr));

listen(listenfd,1);
clilen = sizeof(cli_addr);
connfd = accept(listenfd,(struct sockaddr*)&cli_addr,&clilen);
while(1)
{   

    if(connfd < 0)
        error("ERROR on accept");

    fo = fopen("Test5.docx","a+");

    n = read(connfd,sendBuffer,sizeof(sendBuffer));
    sendBuffer[n] = '\0';
    printf("%d\n",n);
    if(n < 0)
        error("ERROR reading from socket");
            bytes_written = fprintf(fo,"%s",sendBuffer);
    printf("%zd\n",bytes_written);
    fclose(fo);
            //sendBuffer[n] = '\0';

    if((strncmp(sendBuffer,"exit",4)) == 0)
        exit(0);
    printf("Here is the message : \n %s\n",sendBuffer);

    n = send(connfd,"i got your message",18,0);
    if(n < 0)
        error("ERROR writing to socket");
}
    close(connfd);
    close(listenfd);
    return 0;
}

0 个答案:

没有答案