If I want to have a datagram socket, what do I have to change here ? It's a stream socket right now.
Just change it where it creates the socket ?

sockfd = socket(PF_INET,SOCK_STREAM,0);

Recommended Answers

All 4 Replies

ops. double. Sorry.

It's a tcp server.

sockfd = socket(PF_INET,SOCK_DGRAM,0);
    printf("Socket: %d created. \n", sockfd); 

So once you change that, everything else seems to stay the same..like the binding ? I think that's it, because that's how you define it.

Read the first answer more carefully. It's not quite that simple.

I'll try to explain whats going on in the background, and that might shed some light on why it's not that simple. A datagram has a header and a payload. The header needs to contain information on where the packet is going. This is so it can be easily be routed through complicated networks - for example, to send something over the internet, you need a destination ip address. This is not the case for unix stream sockets, which are more like names pipes.

In the olden days, we used switches to make networks. There kind of like routers - but with one big difference. Each packet is sent to each computer! So, a server needed to be aware of what it's address was so it could ignore packets that's not addressed to it.

It's a tcp server.

Then why are you using datagrams? Datagrams are used in UDP/IP. TCP/IP uses an enturely different system. Unless your trying to say your manually implementing TCP over UDP/Ip. In which case, you're doing something very stange indeed.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.