flat assembler
Message board for the users of flat assembler.

Index > Windows > Winsock Send Help.

Author
Thread Post new topic Reply to topic
Overflowz



Joined: 03 Sep 2010
Posts: 1046
Overflowz 10 Sep 2010, 13:14
Hello everyone. I have problem about send command from winsock.. It dont sends nothing just closes itself this is code and tell me what I'm doing wrong.. Thanks.
Code:
format PE console 4.0
entry main
include 'WIN32AX.INC'
.data
server dd ?
welcome db "Hi",0
saddrlen dd sizeof.sockaddr_in
saddr sockaddr_in
wsaData WSADATA
PORT = 23
.code
main:
invoke WSAStartup,0202h,wsaData
invoke WSASocket, AF_INET, SOCK_STREAM, 6, 0, 0, 0
mov [server], eax
mov [saddr.sin_family], AF_INET
invoke htons, PORT
mov [saddr.sin_port], ax
invoke htonl, 0
mov [saddr.sin_addr], eax
invoke bind, [server], saddr, saddrlen
invoke listen, [server], 1
invoke accept, [server], saddr, saddrlen
invoke send,[server],welcome,2,0
invoke ExitProcess,0
data import
library kernel32,'kernel32.dll',ws2_32,'ws2_32.dll'
include 'API\KERNEL32.INC'
include 'API\WS2_32.INC'
end data    
Post 10 Sep 2010, 13:14
View user's profile Send private message Reply with quote
Picnic



Joined: 05 May 2007
Posts: 1403
Location: Piraeus, Greece
Picnic 10 Sep 2010, 14:29
Hi,

If accept() succeeds, the return value is a new socket created on the server side.
therefore it would be better:
Code:
invoke accept, [server], saddr, saddrlen
mov [client],eax
invoke send,[client],welcome,2,0
    

Close sockets and cleanup. Code should work now.

closesocket()
WSACleanup()
Post 10 Sep 2010, 14:29
View user's profile Send private message Visit poster's website Reply with quote
Nameless



Joined: 30 Apr 2010
Posts: 95
Nameless 10 Sep 2010, 14:37
but i think accept is non-blocking API? am i right? that means it needs to be inside a loop
Post 10 Sep 2010, 14:37
View user's profile Send private message Reply with quote
Overflowz



Joined: 03 Sep 2010
Posts: 1046
Overflowz 10 Sep 2010, 14:39
Hi, I understand now whats problem but I have bit problem. when I'm calling WSACleanup, the same problem are occuired. here's code. Thanks.
Code:
format PE console 4.0
entry main
include 'WIN32AX.INC'
.data
server dd ?
client dd ?
welcome db "Hi",0
saddrlen dd sizeof.sockaddr_in
saddr sockaddr_in
wsaData WSADATA
PORT = 23
.code
main:
invoke WSAStartup,0202h,wsaData
invoke WSASocket, AF_INET, SOCK_STREAM, 6, 0, 0, 0
mov [server], eax
mov [saddr.sin_family], AF_INET
invoke htons, PORT
mov [saddr.sin_port], ax
invoke htonl, 0
mov [saddr.sin_addr], eax
invoke bind, [server], saddr, saddrlen
invoke listen, [server], 1
invoke accept, [server], saddr, saddrlen
mov [client],eax
invoke send,[client],welcome,2,0
invoke closesocket,client
invoke closesocket,server
invoke WSACleanup
invoke ExitProcess,0
data import
library kernel32,'kernel32.dll',ws2_32,'ws2_32.dll'
include 'API\KERNEL32.INC'
include 'API\WS2_32.INC'
end data    
Post 10 Sep 2010, 14:39
View user's profile Send private message Reply with quote
Nameless



Joined: 30 Apr 2010
Posts: 95
Nameless 10 Sep 2010, 15:02
ive nothing to test on here, but its up to u now
here is what i wrote so far

Code:
_data rb      1024

...........

.accept_loop:
    invoke accept, [server], saddr, saddrlen
    cmp     eax, -1
     je      .accept_loop
        mov     [client], eax ;if it reached this line, it means that we have a new connection
      jmp     .recv_loop      ;take that new socket to the receiving loop
 
.recv_loop:
 xor     eax, eax
    mov     dword [_data], eax
  invoke  recv, [client], _data, 1024, 0
      cmp     eax, 0
      je      .end_recv_loop
      ;add one more compare of eax, is its -1, re-initialize all ur sockets again
 jmp     .process_data
               
.end_recv_loop:
    ;Connection Closed, Get Back to listening for new connection
 invoke  closesocket, [client]
       jmp     .accept_loop
                
.process_data:
      ;Process ur data received here
    
Post 10 Sep 2010, 15:02
View user's profile Send private message Reply with quote
Overflowz



Joined: 03 Sep 2010
Posts: 1046
Overflowz 10 Sep 2010, 15:10
Oh Thanks. Smile BTW I'm trying to figure out how to do like send and recv functions both, like telnet chat ? I know Netcat using that function. with this command nc -L -v -p 23 and when connected I can write something and data are transfered to remote and reverse. How can I do that ? Razz Thanks.
Post 10 Sep 2010, 15:10
View user's profile Send private message Reply with quote
Nameless



Joined: 30 Apr 2010
Posts: 95
Nameless 10 Sep 2010, 15:21
i dont really know what netcat traffic looks like, but here is the simplest way i can think of to process commands

Code:
.process_data: 
        ;Process ur data received here
        invoke StrCmpI, _data, "COMMAND1"
        je .process_command1
        invoke StrCmpI, _data, "COMMAND2"
        je .process_command2
        jmp .recv_loop ;no commands found, re-do the recv loop incase we got something to recoginize

.process_command1:
       ;Do whatever function u want here
       ;and to send()
       invoke send, [client], _data , 1024, 0 ;data or whatever other variable that holds the data u want to send
       jmp  .recv_loop ; must do this after each command, to receive the queued data

.process_command2:
      ;bla bla bla
       jmp      .recv_loop ;exactly as the previous one
    


StrCmpI

EDIT: oops, forgot to say that the _data is the variable that holds the received data from the connection, as it was done in my previous post
Post 10 Sep 2010, 15:21
View user's profile Send private message Reply with quote
Overflowz



Joined: 03 Sep 2010
Posts: 1046
Overflowz 10 Sep 2010, 15:46
Ah I got it. thanks. Smile And, Is there any other way to do without StrCmpI? I mean without using library. Is that like cmp in asm language ? Smile
Post 10 Sep 2010, 15:46
View user's profile Send private message Reply with quote
Picnic



Joined: 05 May 2007
Posts: 1403
Location: Piraeus, Greece
Picnic 10 Sep 2010, 17:30
Overflowz wrote:
Hi, I understand now whats problem but I have bit problem. when I'm calling WSACleanup, the same problem are occuired.
Code:
invoke closesocket,[client]
invoke closesocket,[server]
    


Nameless wrote:
but i think accept is non-blocking API? am i right? that means it needs to be inside a loop

A socket it is automatically set to be blocking so accept() actually blocks until a connection request comes.
Post 10 Sep 2010, 17:30
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.