Login
Username:

Password:

Remember me



Lost Password?

Register now!

Sections

Who's Online
110 user(s) are online (95 user(s) are browsing Forums)

Members: 2
Guests: 108

Hypex, livebyfaith74, more...

Support us!

Headlines

 
  Register To Post  

(1) 2 »
sockets suck...
Just can't stay away
Just can't stay away


See User information
Does anyone have a simple, tangible example of how to properly initialize a bsdsocket and connect it to, say, "www.google.com" and download via http??

I have looked everywhere on the web, but I can only find stuff that uses ipv6 and getaddrinfo, which is useless to us...

Help!

Go to top
Re: sockets suck...
Home away from home
Home away from home


See User information
@alfkil

I don't know of any online tutorials, but a book called "BSD Sockets Programming from a Multi-Language Perspective" has all the examples that you would need, in several different programming languages. The book is IPv4 only.

Alternatively, you could side-step the problem and use libCURL instead; it's included in the SDK.

Hans

Join Kea Campus' Amiga Corner and support Amiga content creation
https://keasigmadelta.com/ - see more of my work
Go to top
Re: sockets suck...
Home away from home
Home away from home


See User information


Use socket(AF_INET, SOCK_STREAM, IPPROTO_TCP) then you use
connect(), then you can simply use read() to read from sockfd.

Almost as easy as opening a file, a few extra lines that's all.

Google: man 3 connect
also documented in Autodocs.

(NutsAboutAmiga)

Basilisk II for AmigaOS4
AmigaInputAnywhere
Excalibur
and other tools and apps.
Go to top
Re: sockets suck...
Quite a regular
Quite a regular


See User information
Quote:

Use socket(AF_INET, SOCK_STREAM, IPPROTO_TCP) then you use
connect(), then you can simply use read() to read from sockfd.

Almost as easy as opening a file, a few extra lines that's all.


That's the kind of simplification he tried to get past if I'm not mistaken

He needs to mess with name lookup and wierd antique structs and I do have a very simple example for HTTP but I'm not at home right now.

But if you have managed to connect to a HTTP server you need to send the following string before the server will send you data:

send(sock,"HTTP 1.0 GET www.google.com\r\n",strlen(your string..)); or something similar. See the autodocs for bsdsocket.

Software developer for Amiga OS3 and OS4.
Develops for OnyxSoft and the Amiga using E and C and occasionally C++
Go to top
Re: sockets suck...
Not too shy to talk
Not too shy to talk


See User information

httpget.c example can be found in million places on the net. Just use Google.

http://lmgtfy.com/?q=httpget.c


Go to top
Re: sockets suck...
Just popping in
Just popping in


See User information
If you want a more in depth overview after looking at the httpget.c example:

The "bible" for socket style network programming is W. Richard Stevens: UNIX Network Programming. See http://www.kohala.com/start/unpv12e.html

Most of it translates effortlessly to Amiga bsdsocket code.

Go to top
Re: sockets suck...
Home away from home
Home away from home


See User information
@Deniil

Not a fan of giving the complete solution, most people learn by doing.

If he gets stuck again, I post some more.

(NutsAboutAmiga)

Basilisk II for AmigaOS4
AmigaInputAnywhere
Excalibur
and other tools and apps.
Go to top
Re: sockets suck...
Just can't stay away
Just can't stay away


See User information
@LiveForIt:

Also apparently I need to connect to port 80 to use http (or smth). These are all little things that are not easy to pick up from whatever random info can be found on the web. Now i managed to connect, but calling read just blocks execution. Probably I need to use the suggestion given by Deniil to receive data.

@thread

Thanks for all the input, i will put it to the test once I get home.

Go to top
Re: sockets suck...
Just popping in
Just popping in


See User information
@alfkil

80/tcp is the standard port, but it's not the only port. You might want to start with a text on TCP/IP. Like UNIX Network Programming, W. Richard Stevens's TCP/IP Illustrated, Volume 1 is the bible. Volume 1 introduces and describes TCP/IP. Volume 2 describes a complete implementation based on 4.4BSD-Lite. (Incidentally, Olaf Barthel wrote Roadshow, the IP stack in AmigaOS 4.x, using this volume as a guide.) Volume 3 describe several protocols, some common, some not so common.

Internet sockets are used by the majority of operating systems to provide access to the system's IP stack. If you want to use HTTP over TCP/IP on any mainstream system, it would be helpful to have at least a basic understanding of both IP and sockets. After that, a detailed understanding of HTTP would be more than beneficial.

Go to top
Re: sockets suck...
Quite a regular
Quite a regular


See User information
@alfkil

If you didn't know HTTP runs over port 80 by default you need to read some basics about TCP/IP, as other suggested. :) Trial'n'error would be slow progress in this case. But a httpget.c example from the net would help.

But to get something from this http server you first need to tell it what to get using the HTTP GET command I suggested. (I might have gotten it wrong though, so verify it first if it doesn't work.) Otherwise your read() will just hang, waiting forever for the server to send you something which will never happen.

A few standard ports:
21:FTP
25:SMTP
80:HTTP
120:Telnet (??)

Software developer for Amiga OS3 and OS4.
Develops for OnyxSoft and the Amiga using E and C and occasionally C++
Go to top
Re: sockets suck...
Home away from home
Home away from home


See User information
@Deniil
Telnet is 23 :)

@alfkil
If you want some real help, without "please read books", you can write me mail, i will explain stuff in few phrases :) But in brief, TCP/IP its protocol, which subclasses on small ones. The classic tcp-ip4 packet structured like this:

ethernet->ip->tcp->xxxx (where xxx can be any tcp protocol, like http, pop3 and any other).

Every protocol descirbed pretty well in documents called RFC. For example, RFC793 describe TCP protcol. HTTP 1/1 protocol (which works over TCP), described in RFC2616. And all the others protocols like udp, idcmp, icmp can be found in the RFC docs as well.

So, all what you need, its just to know that TCP/IP stack its big class of small protocol, which works one over other one, which all have their structures/headers/etc, and which described in the RFC docs with all the structures and fields.

Then nextly, you need to read any normal article about non-blocked sockets (not those major books, but just some small article about non-blocked sockets). Just type in google "article about non-blocking sockets".

But, if you just want some real help, write me mail, i will send you ready-to-compile-on-amigaos sources , docs, and will explain all the stuff what need :)


PS: The best reference to "real" understanding about tcp/ip stack (and to any other network related stack) its just read articles from old-hacking diskmag (still alive) called Phrack (just because they well structured, small, and give you real information, not tons of words about nothing, like done in all that majoric books all the time).

Join us to improve dopus5!
AmigaOS4 on youtube
Go to top
Re: sockets suck...
Home away from home
Home away from home


See User information
Quote:

send(sock,"HTTP 1.0 GET www.google.com\r\n",strlen(your string..))


That would be "GET www.google.com HTTP 1.0\r\n"


Go to top
Re: sockets suck...
Quite a regular
Quite a regular


See User information
@kas1e & broadblues

Thanks

But wouldn't you infact do "GET / HTTP 1.0\r\n" to get whatever the server provides at the root, or "GET /index.html HTTP 1.0\r\n"? Was too long ago, too lazy googling and not at home so I can check.... ;)

Software developer for Amiga OS3 and OS4.
Develops for OnyxSoft and the Amiga using E and C and occasionally C++
Go to top
Re: sockets suck...
Just can't stay away
Just can't stay away


See User information
@alfkil
A library like libCURL that someone else mentioned would probably be more useful than direct access from your program. Except for simple D/L sites like Aminet or OS4Depot, the trend seems to be script links that prevent you from direct HTTP access to files. Everybody wants you to be viewing their WEB page and advertisements when accessing files or data. It seems like the days of direct HTTP downloads are numbered. When OWB was first released, I copied the links and used the old HTTPRresume program to perform the download so I could see the file size and progress. That fails so often that I don't bother anymore. Too many D/L links send you to another page that contains a scripted link that only works if your program is javascript capable.

Go to top
Re: sockets suck...
Just can't stay away
Just can't stay away


See User information
@kas1e

Thanks for the note, I will write you shortly.

@thread

I have so far been able to write this function, that just calls up google really short, allocs a signal with setsockopt and waits for bytes to read:
void sockread()
{
    
int sockfd = ::socket(AF_INETSOCK_STREAM0);

#define MAX_BUF 1024

extern unsigned int qt_socketSignalMask//allocated in main code
extern int safewrite(intchar *);

    
int count;
    
char buf[MAX_BUF];

    
int status;

    
struct sockaddr_in my_addr;
    
memset(&my_addr0sizeof(my_addr));
    
my_addr.sin_family AF_INET;
    
my_addr.sin_port htons(80);

    
struct hostent *hent gethostbyname("www.google.com");
    if (!
hent)
        
printf ("gethostbyname failed!\n");
    
memcpy(&my_addr.sin_addrhent->h_addr_list[0], hent->h_length);

    
status = ::connect(sockfd, (struct sockaddr *)&my_addrsizeof(my_addr));
    if (
status == -1)
    {
        
perror("Connection error");
        
printf("errno = %d\n"errno);
        exit(
1);
    }
    
printf("Connected\n");

                
ULONG temp FD_ACCEPT FD_CONNECT FD_READ FD_WRITE FD_ERROR;
                
status setsockopt(sockfdSOL_SOCKETSO_EVENTMASK, &tempsizeof(temp));
                
qDebug() << "setsockopt returned " << status;

    
status safewrite(sockfd"GET www.google.com HTTP/1.0\nAccept: */*\r\n\r\n");
    
qDebug() << "written " << status << "bytes to socket";

    
printf("waiting for signal 0x%x\n"qt_socketSignalMask);

    
unsigned int signal IExec->Wait(qt_socketSignalMask);
    if (
signal qt_socketSignalMask)
        
printf("Got a SOCKET signal!\n");

    
count = ::read(sockfdbufMAX_BUF);
    
printf("read %d bytes from socket %d\n"countsockfd);

    ::
close(sockfd);
}


Now, the problem arises once I try to call this code from various places within qt. At
one point, I get this very strange behavior:

WORKS:
int QAbstractSocket::qt_metacall(QMetaObject::Call _cint _idvoid **_a)
{
     
// .... bla bla ....

    
if (_c == QMetaObject::InvokeMetaMethod) {
sockread();
        switch (
_id) {
        case 
0hostFound(); break;
        case 
1

// .... bla bla ....

        
case 12:

// ... bla bla ...
        
default: ;
        }
        
_id -= 16;
    }
    return 
_id;
}


DOESN'T WORK(HANGS at IExec->Wait()):
int QAbstractSocket::qt_metacall(QMetaObject::Call _cint _idvoid **_a)
{
     
// .... bla bla ....

    
if (_c == QMetaObject::InvokeMetaMethod) {
        switch (
_id) {
        case 
0hostFound(); break;
        case 
1

// .... bla bla ....

        
case 12sockread(); break;

// ... bla bla ...
        
default: ;
        }
        
_id -= 16;
    }
    return 
_id;
}


What could possibly be the catch here?? I have a stack of 10MB, so it is not a stack issue...

Help!

Go to top
Re: sockets suck...
Amigans Defender
Amigans Defender


See User information
If it's hanging at Wait() then surely it is just waiting for a signal?


Go to top
Re: sockets suck...
Just can't stay away
Just can't stay away


See User information
@chris

Yes, it is, and that's the point! setsockopt() is supposed to make it send a signal once data arrives at the socket, and surely if I remove the IExec->Wait() line, I can see that data _has_ arrived at the socket, but the signal never comes. And that is only the second example, in the first one the signal actually arrives, and everything works as it is supposed to...

I'm banging my head against this, please somebody help!

EDIT: Also I can manually send a signal from Ranger, which means everything works _except_ that the signal is never emitted as it is supposed to.

Go to top
Re: sockets suck...
Home away from home
Home away from home


See User information
I normal use IOCTL to check buffers, before reading,

I never use signals, as they can be complicated when you have multi-trading.

Etch individual tread has to have there own signals, so this translates in one signal per task per connection, if I'm right.

(NutsAboutAmiga)

Basilisk II for AmigaOS4
AmigaInputAnywhere
Excalibur
and other tools and apps.
Go to top
Re: sockets suck...
Just can't stay away
Just can't stay away


See User information
@alfkil
I don't know if it matters but you call sockread(); in two different places in your two examples.

Where do you set qt_socketSignalMask ? Is it correct ?

Rock lobster bit me - so I'm here forever
X1000 + AmigaOS 4.1 FE
"Anyone can build a fast CPU. The trick is to build a fast system." - Seymour Cray
Go to top
Re: sockets suck...
Just can't stay away
Just can't stay away


See User information
@TSK
Quote:
I don't know if it matters but you call sockread(); in two different places in your two examples.

Yup, that's pretty much the whole point of the excercise...

Quote:
Where do you set qt_socketSignalMask ? Is it correct ?

I'm pretty much a 110% that it is correct (if not else then because it actually works in a)the other case and b) when I send a signal with Ranger).


Go to top

  Register To Post
(1) 2 »

 




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




Powered by XOOPS 2.0 © 2001-2024 The XOOPS Project