Socket Programming?

Hello!

I'm trying to start programming sockets, but have no idea where to start from!! There is a lot of documentation, BUT! I'm kind of confused with it.

this is my server code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <cstdlib>
#include <iostream>
#include <netinet/in.h>

using namespace std;

/*
 * 
 */
int main(int argc, char** argv) {

    //We're creating the socket, AF_INET, SOCK_STREAM Type, 6 
    //stands for tcp.
    int fdc, fdl = socket(AF_INET, SOCK_STREAM, 6);
    
    //We're creating a struct from sockaddr_in 
    //AF_INET, custom port (15557), Ip is our loopback
    struct sockaddr_in dir = { AF_INET, htons(15557), INADDR_ANY};
    
    //Binding socket to port.
       char ch;
 
        bind(fdl, (sockaddr*) &dir, sizeof(dir));
        listen(fdl, 1);
        fdc = accept(fdl, NULL, NULL);        
        close(fdl);
        
        while (read(fdc, &ch, 1)) 
            write(1, &ch, 1);
        
        
        
        cout<<"\n";
        
        cout<<"End?";
        
    
    return 0;
}


What about read and write functions?

Why is the socket being closed trought close(fdl);?

AF_INET or AF_INET6?

Should I create this struct?

struct sockaddr_in dir = { AF_INET, htons(15557), INADDR_ANY};

this web page, shows totally different info from what I posted?

http://www.linuxhowtos.org/C_C++/socket.htm

Any Ideas from where to start?

Thanks in advance!

Regards!
Last edited on
What about read and write functions?
What about them?

Why is the socket being closed trought close(fdl);?
The server creates a socket to listen on. That is the one bound to port 15557 on all interfaces on your machine.

When a client connects, that connection is returned by accept() and you store it as fdc (file descriptor for client)
http://pubs.opengroup.org/onlinepubs/007908799/xns/accept.html

The server's listening socket is being closed because it does not want to accept any further connections.

Should I create this struct?
 
struct sockaddr_in dir = { AF_INET, htons(15557), INADDR_ANY};
Yes. You're binding that connection to a socket. That's how the sockets library identifies your server socket as a server socket that will service that end point.

An end point is a port on an address. But as you've specified INADDR_ANY, you're saying you want to bind to port 15557 on any network interface IP4 (AF_INET) that your computer has.

htos() means host to network short and deals with endian issues on your computer.
http://pubs.opengroup.org/onlinepubs/007908799/xns/htonl.html
http://en.wikipedia.org/wiki/Endianness

Any Ideas from where to start?
It's customary to point you here: http://www.beej.us/guide/bgnet/output/html/multipage/index.html
Hmm, Thanks a lot!!!

that web page looks kind of interesting I will read it from start to end.

ok, and, if we want to prepare our server to receive multiple connections, what should we use? Threads?

we need a main while(true) loop or something like that?
Incredible documentation.

Thanks. REALLY, THANKS!
Topic archived. No new replies allowed.