# Download file with socket syscall

**URL:** <https://community.unix.com/t/download-file-with-socket-syscall/341994>\
**Category:** Programming\
**Created:** [April 22, 2014, 7:58am UTC](https://community.unix.com/t/download-file-with-socket-syscall/341994 "2014-04-22T07:58:31Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![recher.jack](https://community.unix.com/letter_avatar/recher.jack/32/5_5575768a8748004e209b776fc1b2916d.png) [@recher.jack](https://community.unix.com/u/recher.jack)\
**Post date:** [April 22, 2014, 7:58am UTC](https://community.unix.com/t/download-file-with-socket-syscall/341994/1 "2014-04-22T07:58:31Z")

</div>

Hello to all  
I want download a file in osx intel 64 with NASM , I want to use socket syscall  
This is part of my code

```nohighlight
section .data
command db "GET /test/2.gif HTTP/1.1\r\nHost: 10.1.1.187\r\n\r\n", 0
; url db "http://172.16.207.153/test/2.gif", 0

global main
section .text

main:    
      
    ;Socket
    mov rdx, 6 ; rdx = IPPROTO_TCP = 6
    mov rsi, 1 ; rsi = AF_NET = 1 
    mov rdi, 2 ; rdi = SOCK_STREAM = 2 
    mov rax, 0x2000061 ; socket syscall = 97
    syscall ; call socket(SOCK_STREAM, AF_NET, IPPROTO_TCP);
    mov r12, rax ; Save the socket
  
  
    ;Sock_addr
    mov r13, 0xB901010A50000101 ; IP = FFFFFFFF, Port = 5C11(4444)
    mov r9b, 0xFF ; The sock_addr_in is + FF from where we need it
    sub r13, r9 ; So we sub 0xFF from it to get the correct value and avoid a null
    push r13 ; Push it on the stack
    mov r13, rsp ; Save the sock_addr_in into r13
; mov r13, 0xB901010A50000002 ; IP = 0A0101B9, Port = 50(80)
; push r13 ; Push it on the stack
; mov r13, rsp ; Save the sock_addr_in into r13
 
    ;Connect 
    mov rax, 0x2000062 ; connect syscall = 98
    mov rdi, r12 ; move the saved socket fd into rdi
    mov rsi, r13 ; move the saved sock_addr_in into rsi
    add rdx, 0x10 ; add 0x10 to rdx
    syscall ; call connect(rdi, rsi, rdx)
   
    ;sendto 
    mov rax, 0x2000085 ; connect syscall = 113
    mov rdi, r12 ; move the saved socket fd into rdi
 
 
   
  
    mov r14 , command
    mov rsi, r14 ; move the saved sock_addr_in into rsi
    
    mov rdx, 0x2e ; add 0x2e to rdx
    
    mov rcx, 0
 
    mov r8, r13
    
    mov r9, 0x10
    
    syscall ; call sendto(rdi, rsi, rdx)
    
    mov rdi, r12
    mov rax, 0x2000006 ; close
    syscall
 
    mov rax, 0x2000001 ; exit
    mov rdi, 0x0
    syscall

```

In this code , I can create socket and connect and close it (I see it in wireshark and dtruss don't show error). but sendto syscall not send my command ( wireshark don't show my http request in flow tcp stream and dtruss show -1 err#22 for sendto in return value).

please help me .

---

<div class="post-metadata">

**Author:** ![Corona688](https://community.unix.com/letter_avatar/corona688/32/5_5575768a8748004e209b776fc1b2916d.png) [@Corona688](https://community.unix.com/u/Corona688)\
**Post date:** [April 22, 2014, 10:47am UTC](https://community.unix.com/t/download-file-with-socket-syscall/341994/2 "2014-04-22T10:47:59Z")

</div>

Moderator comments were removed during original forum migration.
