0

pretty small and specific question if anyone might know, about the socket system call function protocol parameter,in the manual it says its where u put the protocol, when i hear socket protocol i start to think this is where u put 23 for telnet, or 80 for http. is this correct?

sockfd = socket(AF_INET, SOCK_STREAM, 0);

I only see 0 or other non integer arguments passed, could i put 80 here to state i want to do HTTP?

also the socket programming can be done by BSD socket libraries imported by C++. but the step from the socket to the HTTP programming seems unclear to me, how would i go about a HTTP layer ontop of this socket network once i have this programmed?

Thank u in advance (first time on this forum)

http://linux.die.net/man/2/socket

what does 0 indicate in socket() system call?

Is it possible to use port 80 for both HTTP and web socket traffic?

https://www.youtube.com/watch?v=V6CohFrRNTo

Community
  • 1
  • 1
iBeyondPower
  • 357
  • 1
  • 5
  • 8
  • 1
    You're confusing protocols with ports. While people often use 80 for HTTP, you can use the HTTP protocol on any port you want and you can use any protocol on port 80. – David Schwartz Apr 15 '15 at 08:28

2 Answers2

0

The third parameter of socket function specifies the type of SOCK_STREAM you require. Besides default TCP, it could be SCTP for example.

HTTP sits much higher, so it needs no special treatment at transport-level. One specifies port only while bind'ing the created socket to network address.

Matt
  • 13,674
  • 1
  • 18
  • 27
  • This is not correct. SCTP would be a type of `SOCK_SEQPACKET`, not `SOCK_STREAM`. My answer explained the layering correctly and yours does not. It is true that the protocol parameter would let you specify a type of `SOCK_STREAM`, if the implementation supported more than one. That could well be HTTP if the implementation supported in-kernel HTTP on top of SOCK_STREAM. – David Schwartz Apr 15 '15 at 09:44
  • @DavidSchwartz: Literal quote from our production source code: `socket(AF_INET, SOCK_STREAM, IPPROTO_SCTP)`. SCTP is a stream protocol like TCP. – MSalters Apr 15 '15 at 12:29
  • @MSalters Then you have a non-standard implementation of `IPPROTO_SCTP` that doesn't respect the [agreed layering](https://tools.ietf.org/html/rfc6458#page-9) for some reason. If you're drawing conclusion based on this anomaly, you'll get wrong conclusions. – David Schwartz Apr 15 '15 at 19:53
  • Did you even read the next line? "Here, `SOCK_SEQPACKET` indicates the creation of a one-to-many style socket.". Our code creates a regular 1-to- 1 socket. – MSalters Apr 16 '15 at 06:49
  • Don't stop reading halfway through the document. The [agreed layering](https://tools.ietf.org/html/rfc6458#page-19) allows both. The half you quoted applies to multicast sockets. – MSalters Apr 16 '15 at 06:55
-2

This third parameter to socket is for protocols that sit above TCP or UDP but below the application. I don't believe any such protocols currently exist, so unless you're doing something very, very unusual, this is always going to be zero.

One could imagine an implementation of an intermediary protocol that would be specified this way. For example, a platform could implement SSL this way.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278
  • oh that clears it up, me mistaking ports with protocols, altho HTTP is a protocol i thought. then how would i make the socket port only open to http? if its not done with this parameter? – iBeyondPower Apr 15 '15 at 08:36
  • HTTP is an application-level protocol. This is socket level, right above TCP. If you had a platform that could do HTTP at socket level, you could specify it here. Such implementations might exist. It would have some constant like `TCPPROTO_HTTP` for you to specify. – David Schwartz Apr 15 '15 at 08:39
  • 2
    @iBeyondPower Actually, the third parameter is the transport-layer protocol which can be seen as a large family of protocols. It could be, for example, TCP or SCTP. HTTP sits two or three levels higher. – Matt Apr 15 '15 at 08:41
  • @DavidSchwartz The third parameter specifies which exactly type of SOCK_STREAM/SOCK_DGRAM one wants. And the choice is not only TCP/UDP, but at least DCCP/STCP too. – Matt Apr 15 '15 at 08:45
  • once i have the fully socket level communication set up, how would i transfer it to be based on HTTP? o.o – iBeyondPower Apr 15 '15 at 09:00
  • This answer is incorrect. The third parameter specifies a protocol within the Address Family (`AF_`) specified by the first parameter. Since HTTP is not specified at IP level, there's no `IPPROTO_HTTP`. Valid protocols within the `AF_INET` family include `IPPROTO_TCP`, `IPPROTO_UDP` and `IPPROTO_SCTP`. Note that SCTP is neither below nor above TCP, but an alternative to TCP. The `socket` interface is not unique to `AF_INET`, which explains why there is some redundancy between the second and third parameter. – MSalters Apr 15 '15 at 12:34
  • @MSalters An in-kernel HTTP implementation would be specified at the IP level and would become a protocol within the address family. – David Schwartz Apr 15 '15 at 19:54
  • IP level protocols are not specified by some OS implementation; they're [IANA assigned](http://www.iana.org/assignments/protocol-numbers/protocol-numbers.xhtml). You will notice that SCTP is specified besides TCP and UDP, whereas HTTP is not. – MSalters Apr 16 '15 at 06:57