Login
Username:

Password:

Remember me



Lost Password?

Register now!

Sections

Who's Online
118 user(s) are online (82 user(s) are browsing Forums)

Members: 2
Guests: 116

afxgroup, Lio, more...

Headlines

 
  Register To Post  

Need help whit socket programming.
Home away from home
Home away from home


See User information
need more help whit socket programming,

Can't get bind() or accept() to work whit in CreateNewProcess()


Edited by LiveForIt on 2009/10/26 22:09:22
(NutsAboutAmiga)

Basilisk II for AmigaOS4
AmigaInputAnywhere
Excalibur
and other tools and apps.
Go to top
Re: Need help whit socket programming.
Not too shy to talk
Not too shy to talk


See User information
@LiveForIt

Its very importent that you open the bsdsocket library in the same process that you need to call the recv/send/bind and so on commands.

RWO

Go to top
Re: Need help whit socket programming.
Just popping in
Just popping in


See User information
@LiveForIt

Let me add a bit more to what RWO already said to clarify:


Because of serveral technical reasons bsdsocket.lib only allows to access its socketfunctions from exactly that process, which also opened bsdsocket.lib. So if you have to use bsdsocket.lib in some child process or so, you MUST open a separate copy of bsdsocket.lib in the child process, otherwise it can't use socketfunctions via the SocketBase from the parent process.

That said, you now know what is the officially recommended way of doing what you want to do. But of course there is also a not really recommended way of doing it differently... I'll leave it up to you to figure it out, because you *really* should know what you are doing if you choose the other way.


BTW: All that is pretty well described in the bsdsocket.lib autodoc in the OpenLibrary() section.

AmigaOS 4 core developer
www.os4welt.de - Die deutsche AmigaOS 4 Gemeinschaft

"In the beginning was CAOS.."
-- Andy Finkel, 1988 (ViewPort article, Oct. 1993)
Go to top
Re: Need help whit socket programming.
Home away from home
Home away from home


See User information
@every one

Thanks for the help

(NutsAboutAmiga)

Basilisk II for AmigaOS4
AmigaInputAnywhere
Excalibur
and other tools and apps.
Go to top
Re: Need help whit socket programming.
Home away from home
Home away from home


See User information
@Cyborg

Think I need more help, I tried to do what you where saying...
but bind fails

gcc get_text.c -o get_text.o -c
gcc chat.c get_text.o -o chat.exe -D__USE_INLINE__ -lauto -lamiga -lm

some where inside chat.c / main()

child_msg = (struct MsgPort *) CreateNewProcTags(
        
NP_NotifyOnDeathMessagedmsg,
        
NP_Entryserver_get_text ,
        
NP_ExitDataprocess,
        
NP_Outputout ,
        
TAG_DONE);


get_text.o

#include <proto/dos.h>
#include <proto/exec.h>

#include <proto/bsdsocket.h>
#include <proto/socket.h>

extern int exit_processes;


extern int sd;
extern int ip_port;
extern struct sockaddr_in addr;
struct Node *node;
char buffer[1024];

#define open_lib( base, interface, name, ver, iname )                \
    
if(( base iexec->OpenLibrary(namever) ))                    \
    
{                                                    \
        
interface = (APTRiexec->GetInterface(base,iname,1,0);    \
    
}                                                    \
    
if (!interface) goto err;

#define close_lib( base, interface)                                \
    
if (interface) { interface->Release(); interface = NULL; }        \
    
if (base) { iexec -> CloseLibrary(base); base NULL; }

 
int32 server_get_text(STRPTR args UNUSEDint32 arglen UNUSED
                 
struct ExecBase *sysbase)
{
    
int cnt 0;
    
struct ExecIFace *iexec = (APTR)sysbase->MainInterface;

    
struct Library *dosbase NULL;
    
struct DOSIFace *idos NULL;

    
struct Library *bsdbase NULL;
    
struct SocketIFace *ibsd NULL;

    
iexec->Obtain();

    
open_lib dosbaseidos"dos.library"50"main" );
    
open_lib bsdbaseibsd"bsdsocket.library",4,"main");

    
sd socket(PF_INETSOCK_DGRAM0);
    
bzero(&addrsizeof(addr));

    
addr.sin_family AF_INET;
    
addr.sin_port htons(ip_port);
    
addr.sin_addr.s_addr INADDR_ANY;

    
idos->Printf("OK server soon ready....\n");

    if (
ibsd->bind(sd, (struct sockaddr*) &addrsizeof(addr)) == -1)
    {
        
idos->Printf("bind error %d\n",errno);
        goto 
err;
    }


    while ( ! 
exit_processes )
    {
        
int bytesaddr_len=sizeof(addr);
        
bytes ibsd->recvfrom(sdbuffersizeof(buffer), 0, (struct sockaddr*)&addr, &addr_len);


        if (
bytes>0)
        {
            
buffer[bytes]=0;
            
idos->Printf("msg from %s:%d (%d bytes)\n"inet_ntoa(addr.sin_addr),    ntohs(addr.sin_port), bytes);
/*
            RDetach(win_chat, ID_LOG);
            if (node = AllocListBrowserNode(2,

                LBNA_Column, 0,
                    LBNCA_CopyText, TRUE,
                    LBNCA_Text, "",
                    LBNCA_Editable, FALSE,

                LBNA_Column, 1,
                    LBNCA_CopyText, TRUE,
                    LBNCA_Text, buffer,
                    LBNCA_Editable, FALSE,

                TAG_DONE))
            {
                iexec->AddTail(ra_chat_list, node);
            }
            RAttach( win_chat,ID_LOG, ra_chat_list );
*/
        
}
        else
        {
//            idos->Printf("Nothing %d (error %d)...\n",cnt,errno);
            
cnt cnt;
        }


        
idos->Delay(20);
    }

    
ibsd->CloseSocket(sd);
    
idos->Printf("exit\n");
    
idos->Delay(20);

err:
    
idos->Printf("closing server\n");
    
idos->Delay(80);

    
close_libdosbaseidos );
    
close_libbsdbaseibsd );

    
iexec->Release();

    return(
0);
}

(NutsAboutAmiga)

Basilisk II for AmigaOS4
AmigaInputAnywhere
Excalibur
and other tools and apps.
Go to top
Re: Need help whit socket programming.
Home away from home
Home away from home


See User information
@LiveForIt

I'm not 100% sure what you're probelm is but a few things occur to whilst looking through your code.


Quote:

#define close_lib( base, interface) \
if (interface) { interface->Release(); interface = NULL; } \
if (base) { iexec -> CloseLibrary(base); base = NULL; }


Ouch, don't call interface->Release()

call
IExec->DropInterface((struct Interface *)interface)


also I don;t think you need to call iexc->Obtain() / iexec->Release()

at least I never seen any example code do this, the example in CreateNewProc autodoc certainly doesn't


I notice that you call

sd = socket(PF_INET, SOCK_DGRAM, 0);

without going via your ibsd but everywhere else use ibsd->

socket() will use IBsd internaly thus won't be using your copy of it. This could cause the issue your experiencing.

Also your don't check sd for success

Go to top
Re: Need help whit socket programming.
Home away from home
Home away from home


See User information
@broadblues

I have done some major bad things


“bsdbase” needs to be called “SocketBase”, ScoketBase can't be declared as global

so get_text.c has to be compiled whit the -D__NOLIBBASE__ like this:

gcc get_text.-o get_text.--D__NOLIBBASE__


after fixing this, it does obtain the socket, but never frees it, this gives me an other error,
errno = 48 from ISocket->bind(), so fixed that problem by increasing the tcp_port number, until found one that was free.

Now most of the code works:

bytes ibsd->recvfrom(sdbuffersizeof(buffer), 0, (struct sockaddr*)&addr, &addr_len);


keeps on returning bytes == 0, and no buffer is filled.


Edited by LiveForIt on 2009/10/28 20:33:49
(NutsAboutAmiga)

Basilisk II for AmigaOS4
AmigaInputAnywhere
Excalibur
and other tools and apps.
Go to top
Re: Need help whit socket programming.
Home away from home
Home away from home


See User information
@broadblues

Quote:

I notice that you call

sd = socket(PF_INET, SOCK_DGRAM, 0);


Your correct SD gaves bad socket, as well, if I use ibsd -> bind()

(NutsAboutAmiga)

Basilisk II for AmigaOS4
AmigaInputAnywhere
Excalibur
and other tools and apps.
Go to top

  Register To Post

 




Currently Active Users Viewing This Thread: 1 ( 0 members and 1 Anonymous Users )




Powered by XOOPS 2.0 © 2001-2023 The XOOPS Project