Hi,
I'm trying to code a prog which listens on a port and so on.... this is part of my code so far but I'm now getting an error trying to call bind?
If anyone has any ideas I'd love to know wats wrong here..
Code written in Fasm.
invoke WSAStartup,0x202,wsadata
invoke socket,AF_INET,SOCK_STREAM,IPPROTO_TCP
mov ,eax
invoke WSACreateEvent
mov ,eax
invoke WSAEventSelect,,,FD_READ or FD_ACCEPT or FD_CLOSE
mov ,AF_INET
invoke htons,
mov ,ax
invoke bind,,sockaddress,sockaddress.sin_size
invoke listen,,1
thnx
Posted on 2003-01-16 11:59:33 by keyoke
You didn't set all the fields of sockaddress (like the IP address).
Thomas
Posted on 2003-01-16 12:28:20 by Thomas
sin_addr is set to zero in the structure automatically if no values are entered heres the strucs
edited by Thomas:
Arrghh.. sorry, I must have pressed 'edit post' instead of 'quote', so I wrote over your reply... :stupid: :stupid: -- Thomas
Posted on 2003-01-16 12:50:44 by keyoke
sin_addr is set to zero in the structure automatically if no values are entered heres the strucs
Depends on where you store the variable But as you store it in the data section this is okay.
Did you convert the port number to network byte order?
Thomas
Posted on 2003-01-16 13:27:28 by Thomas
it's ok.
ya using htons...
here is my whole source so far...i'm new to winsock so ya :( have a look at wat ive done so far and if u have suggections would be good :) wat i want my prog to do is create new thread for each new connection and then to relay to another address. eventually want it to be sorter like proxy but different....ya
section '.data' data readable writeable
hSocket dd ?
hInstance dd ?
hWSAEvent dd ?
pWSAEvents dd ?
hWSAEvents WSANETWORKEVENTS
ThreadID dd ?
Port dd ?
Temp dd ?
CommandLine dd ?
LocalPort dd 0x69
wsadata WSADATA
sockaddress sockaddr_in
section '.text' data readable
Title db 'Bouncer v1.0',0
Caption db 'This is a thread',0
section '.code' code readable writeable executable
start:
invoke GetModuleHandle,NULL
mov ,eax
invoke GetCommandLine
mov ,eax
Winsock:
invoke WSAStartup,0x202,wsadata
invoke socket,AF_INET,SOCK_STREAM,IPPROTO_TCP
mov ,eax
mov ,AF_INET
invoke htons,
mov ,ax
invoke bind,,sockaddress,sockaddress.sin_size
invoke listen,,0
invoke accept ----?
WorkerThread:
invoke CreateThread,NULL,NULL,ThreadProc,Temp,0,ThreadID
invoke CloseHandle,eax
Exit:
invoke closesocket,
invoke WSACleanup
invoke ExitProcess,0
proc ThreadProc,Param:DWORD
enter
pushad
invoke WSACreateEvent
mov ,eax
mov ,eax
invoke WSAEventSelect,,,FD_READ or FD_ACCEPT or FD_CLOSE
invoke WSAWaitForMultipleEvents,0x1,pWSAEvents,FALSE,INFINITE,FALSE
invoke WSAEnumNetworkEvents,,,
invoke WSACloseEvent,
popad
return
Posted on 2003-01-16 14:56:32 by keyoke
ya using htons...
Should have seen that in your code :rolleyes:. Must be the lack of coffee today :)
here is my whole source so far...i'm new to winsock so ya :( have a look at wat ive done so far and if u have suggections would be good :) wat i want my prog to do is create new thread for each new connection and then to relay to another address. eventually want it to be sorter like proxy but different....ya
The bind looks okay, does it succeed or not? Which error code does it give (in case it fails), you can get the error code with WSAGetLastError.
btw have you seen my winsock tutorial? The latest chapter describes the most import functions in short.
Thomas
Posted on 2003-01-16 15:24:47 by Thomas
yeah i have your tutorial it's great but sum of the stuff that i need to know isnt in it like the Event stuff...but cant wait for following chapters :)
what is the sequence of events when connection is incoming
1listen
accept
createevent
wsaeventselect
wsawaitformultipleevents
Posted on 2003-01-16 15:32:54 by keyoke
dont worry about it im gonna leave it awhile cant think straight right now :(
thnx for the help
Posted on 2003-01-16 15:54:41 by keyoke
i have a similar problem, but with only udp sockets. tcp works and i have send recv data.
heres my tcp code, its c++ but any asm proger should understand it, as its mostly api. i want to know why changing hSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); to hSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); doesnt work( doesnt bind.)
if (WSAStartup(MAKEWORD(iReqWinsockVer,0), &wsaData)==0)
{
// Check if major version is at least iReqWinsockVer
if (LOBYTE(wsaData.wVersion) >= iReqWinsockVer)
{
//SOCK_DGRAM,SOCK_STREAM
hSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (hSocket==INVALID_SOCKET){
printf("Error at socket()");
WSACleanup();
return 0;
}//INADDR_ANY
sockAddr.sin_family = AF_INET;
sockAddr.sin_addr.S_un.S_addr = inet_addr("192.168.0.112");
sockAddr.sin_port = htons(Srvprt); // Convert int 8888 to a value for the port field
nRet = bind(hSocket, (sockaddr*)(&sockAddr), sizeof(sockAddr));
if (nRet == SOCKET_ERROR) {
printf("Error at bind()");
WSACleanup();
return 0;
}
nRet = listen(hSocket, SckQueue); // 10 is the number of clients that can be queued
if (nRet == SOCKET_ERROR) {
printf("Error at listen()");
WSACleanup();
return 0;
}
std::cout << "Sock Listining at 192.168.0.112 on "<< Srvprt << "\n";
int somevalue;
somevalue=sizeof(sockAddrCLIENT);
while(true)
{
Lsock = accept(hSocket,(sockaddr*)(&sockAddrCLIENT),&somevalue); // ECEPT CONNECTION AND SPAWN NEW SOCKET.
if (Lsock == INVALID_SOCKET) {
printf("Error at accept()");
//WSACleanup();
return 0;
Posted on 2003-01-25 18:04:13 by Qages