0

I have a TCPListener in C# and the client in Unix C. I use TcpClient.Client.SendFile for transmitting a file to client socket in Unix and it works fine for plain txt file. It fails to produce the full file on the unix end, when I send JPEG files. Any Idea?

C# code part
============
static void Main(string[] args)
        {
            TcpListener serverSocket = new TcpListener(10001);           
            TcpClient clientSocket = default(TcpClient);
            serverSocket.Start();
            Console.WriteLine(" >> Server Started");
            clientSocket = serverSocket.AcceptTcpClient();
            Console.WriteLine(" >> Accept connection from client");            

            while ((true))
            {
                try
                {                    
                    NetworkStream networkStream = clientSocket.GetStream();
                    byte[] bytesFrom = new byte[10025]; 
                    networkStream.Read(bytesFrom, 0, (int)clientSocket.ReceiveBufferSize);
                    string dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom).TrimEnd('\0');
                    Console.WriteLine(" >> Data from client - " + dataFromClient +" Length "+ dataFromClient.Length);                   

                    string a1 = "C:\\MRTD\\PICTURE\\abc.jpg";
                   clientSocket.Client.SendFile(a1);  
                                   }
                catch (Exception ex)
                {
                   Console.WriteLine(ex.ToString());
                   Console.ReadLine();
                }
            }

        }

    }
}

Unix C code to read the file
============================
char *tcp_recv( int id_type )
{
    register int nbytes;
    static char readbuf[MAXLINE];
    static char tempbuf[980000];
    int ngot = 0, bulk_data=0, p_read_val;
    char  * x, * y, * z, *j, *k, *x1, *bar;
    char bulk_buf[180000], tempstr1[180000] , fstr[40],  termtor[40];

    int    fd_ready;
    fd_set read_fds;           /* Read file descriptors */
    int    fd_port = gfd_sock;
    struct timeval timeout;


    timeout.tv_sec = 3600;
    timeout.tv_usec = 0;

    while (TRUE)
    {

        FD_ZERO( &read_fds );

        FD_SET( fd_port, &read_fds );

        strcpy(fstr,"FAIL");

        if((fd_ready = select(fd_port+1,&read_fds,NULL,NULL,&timeout))< 0)
        {
          if( errno == EINTR ) { continue; }
            logmsg("ERROR select() returned errno %d", errno );
            logmsg("Select error waiting for packet." );
            return fstr;
        }
        else if( fd_ready == 0 )
        {
            logmsg("Timeout waiting for packet.");
            strcpy(fstr,"TIMEOUT");
            return fstr;
        }


        memset( readbuf, 0x00, sizeof( readbuf ));
        memset( br_kde, 0x00, sizeof( br_kde ));

        if((nbytes = read( fd_port, readbuf, sizeof(readbuf)-1 )) < 0 )
        {
            logmsg( "tcp_recv: nbytes %d, errno %d, %s", nbytes, errno, strerror( errno ) );

            if (errno == EINTR || errno == EAGAIN )
            {
                errno = 0;
                continue;     /* assume SIGCLD */
            }
            else
            {
                /*
                 * connection failer.
                 */
              logcon( "Desko connection failed. Pier link is DOWN!" );
              logmsg( "Desko connection failed. Pier link is DOWN!" );
              close_files();
              exit (1);
              break;
            }   /* end else if */
        }
        else
        {

              logmsg("tcp_recv: readbuf is %s, bytes %d", readbuf, nbytes);         

              strcat(tempbuf, readbuf);
              logmsg("tempbuf is %s", tempbuf );<== full file listing is missing in case of JPEG

         }
          return tempbuf;
}
}
Plusguy
  • 1
  • 1
  • Solving the XY problem, this would be *a lot* easier with node or some other thin http server to save the file, and `HttpClient` to `POST` the file to the node server. – Mitch Apr 06 '16 at 18:15
  • [Length-prefixing?](http://stackoverflow.com/questions/35233852/tcp-client-to-server-communication/35240061#35240061) – Visual Vincent Apr 07 '16 at 19:11

0 Answers0