How can I send [SYN] with bare sockets?

352 views Asked by At

I'm writing a bare bone ftp client just using sockets on VxWorks and I now would like to receive directory contents. For that I need to send a Request: LIST and following a [SYN] which initiates the data transfer back to me but I'm wondering how I do this with simple sockets? My code to send the LIST just looks like this:

char lst[6] = "LIST";
lst[4] = 0x0d; // add empty characters on back
lst[5] = 0x0a;
if (write (sFd, (char *) &lst, 6) == ERROR) 
        { 
        perror ("write"); 
        close (sFd); 
        return ERROR; 
        }

if (read (sFd, replyBuf, REPLY_MSG_SIZE) < 0)  { 
        perror ("read"); 
        close (sFd); 
        return ERROR; 
    }

printf ("MESSAGE FROM SERVER:\n%s\n", replyBuf);

but it actually gets stuck in the read() until it times out as the server doesn't respond unless i send a 'SYNC` to initiate the connection.

edit

Upon suggestion, I replaced the addtion of 0x0d and 0x0a at the end of my string with \r\n directly top the string which changed my code to:

char lst[6] = "LIST\r\n";
if (write (sFd, (char *) &lst, strlen(lst)) == ERROR) 
        { 
        perror ("write"); 
        close (sFd); 
        return ERROR; 
        }

if (read (sFd, replyBuf, REPLY_MSG_SIZE) < 0)  { 
        perror ("read"); 
        close (sFd); 
        return ERROR; 
    }

printf ("MESSAGE FROM SERVER:\n%s\n", replyBuf);

but I get exactly the same result, my client does not send a SYNC message - why not I am wondering...?

1

There are 1 answers

1
user207421 On

For that I need to send a Request: LIST and following a [SYN] which initiates the data transfer back to me

No you don't. The [SYN] is sent automatically when you connect a TCP socket.

char lst[6] = "LIST";

The problem is here. It should be

char[] lst = "LIST\r\n";

All FTP commands are terminated by a line terminator, which is defined as \r\n.