如何使用tcp / ip协议从C发送和接收文本文件到Java?

时间:2015-08-05 06:25:15

标签: c sockets system system-calls tcp-ip

我能够将文本文件发送到java,但是当我收到该文件时,我将以十六进制格式获取文本。如何在没有数据丢失和其他故障的情况下接收文件。我正在提供接收代码以供进一步参考。

  int receive_text(int socket)
  { 
      int buffersize=8192,recv_size=0,read_size = 1, write_size,size; 
      char pBuf[80],a[50],MAXDATASIZE; 
      int errnom,i;
      FILE *text; 
      size_t rec;
      text = fopen("/home/sosdt009/Desktop/receivednew.txt", "a");  
      if (text == NULL) 
      {     
         printf("Error has occurred, text file could not be opened \n");
         return -1;
      }

      //Loop while we have not received the entire file yet

      while(read_size > 0)
      {

            ioctl(socket, FIONREAD, &buffersize);
            printf("The Buffersize is    :%d\n",buffersize);
            gets(a);
            printf("The size of socket is:%d\n",socket);
            gets(a);

         //We check to see if there is data to be read from the socket 

        if (buffersize > 0)
        {
            printf("Buffersize value is  :%d\n", buffersize);
            gets(a);
            if (!pBuf)
            {
                printf(errnom, "Memory Error Cannot Allocate!\n");
                gets(a);
                exit(-1);
            }

            read_size = recv(socket, sizeof(pBuf),MAXDATASIZE,0);
            printf("Read size value is :%d \n",read_size);
            gets(a);
            printf("Buffersize value is:%d \n",pBuf);
            gets(a);
            if (read_size < 0)
            {
                printf("%d\n",strerror(errno));
                printf("Data not written to the file:\n");
                gets(a);
            }
             /Write the currently read data into our text file

             write_size = fwrite(pBuf,1,sizeof(pBuf),text); 
             printf("Write size value is   :%d \n",write_size);
             gets(a);
             printf("Buffer size value is  :%d \n",buffersize);
             gets(a);

             //Increment the total number of bytes read
            recv_size += read_size;
            printf("Received size value is:%d \n",recv_size);
            gets(a);
            printf("Read size value is    :%d \n",read_size);       
            gets(a);    
        }
     }
    free:
    fclose(text);
    close(socket);  
    printf("Text Successfully Received:\n");
    gets(a);
    return 0;
}

这是c

中的发件人功能
 int send_text(int socket)
 { 
     FILE *text;
     char a[50];
     int size, read_size, stat, packet_index;
     char send_buffer[8008], read_buffer[8008];
     int wrt=0,sock_fd,tsize=0;
     packet_index = 1;
     int i=0; 
     text = fopen("/home/sosdt009/Desktop/character3.txt", "r"); 
     if (text == NULL)
     {
         printf("Error Opening text File:");
         exit(-1);
     }  
     printf("Getting text Size:\n");
     gets(a);
     fseek(text, 0, SEEK_END);
     size = ftell(text);
     fseek(text, 0, SEEK_SET);
     printf("Total text size: %d \n", size);    
     gets(a);

     //Send text Size

     printf("Sending text Size:\n",size);
     gets(a);
     send(socket, (void *)&size, sizeof(size), 0);

     while(stat < 0)
     printf("Socket data:%s \n", read_buffer); 
     gets(a);   
     while(size>tsize) 
     {

         //Read from the file into our send buffer
         read_size = fread(send_buffer,1,sizeof(send_buffer),text);
         printf("The size of send buffer:%c \n", sizeof(send_buffer));
         gets(a);
         printf("The read size value is :%d \n", read_size);
         gets(a);

    //Send data through our socket 

    do
    {           
        stat = send(socket, send_buffer, read_size, 0);
        printf("The send size value is: %d \n", size);
        gets(a);
        printf("The read size value is: %d \n", read_size);
        gets(a);

    } while (stat < 0); 
    printf("Packet %d, sent %d bytes.\n", packet_index, read_size);
    gets(a);

    //packet_index++;
    //Zero out our send buffer

    tsize = tsize+read_size;
    printf("The tsize value is:%d \n",tsize);
    gets(a);
        memset(send_buffer,0, sizeof(send_buffer));

    if(read_size<=NULL)
    {
        printf("The connection is transferred to received text: \n");
        gets(a);
    }
    }
    fclose(text);
    printf("Text successfully send:\n");
     gets(a);
    return 0;   
}

0 个答案:

没有答案