Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between a protocol and an interface in general?

I understand an interface is a set of publicly exposed things that one system can use to interact with others systems. I'm reading about WEBRTC protocol and to understand what a protocol is I went to the Wikipedia definition. It says more or less that a protocol is a system of rules that allows two systems to communicate. Ain't that the same as interface? Maybe I'm not understanding one or both.

like image 431
Emily Avatar asked Oct 27 '22 17:10

Emily


2 Answers

An interface defines how two entities may communicate. A protocol defines how they should communicate and what that communication means.

Here is an interface:

public interface ICommunicate
{
    string SendMessageAndGetResponse(string message);
}

A protocol then might be:

Send "Hello", if you get back "Hi" then send "How are you?" and the response will be a status. If you get back anything other than "Hi" from the initial message then the system is not functioning properly and you must send the message "Reboot" which you'll then get back "Rebooted!" if successful and anything else for failure.

like image 122
Enigmativity Avatar answered Nov 16 '22 07:11

Enigmativity


In general interface mean "The point of interconnection or contact between entities." and transferred to software it means "The connection between parts of software." and also "In object-oriented programming, a piece of code defining a set of operations that other code must implement." (Source)

In general protocol means "The official formulas which appeared at the beginning or end of certain official documents such as charters, papal bulls etc." and transferred to computers it means "A set of formal rules describing how to transmit or exchange data, especially across a network.". (Source)

So protocol focuses more on the data exchange, whereas interface focuses more on software interaction independent of any data exchange.

Of course, in the end, software interaction is most of the time a data exchange. Passing arguments to a function is a data exchange. Calling a method/function is not directly a data exchange but you need to imagine it like this: Instead of calling different functions:

c = add(a, b);
c = sub(a, b);

you could as well always call the same function and pass the desired functionality as argument:

c = func("add", a, b);
c = func("sub", a, b);

and that way the functionality becomes data as well.

The terms are somewhat interchangeable. E.g. some programming languages call it interface to focus on the pure interaction of components (classes, objects, etc.) and some call it protocol to focus on the data exchange between the components.

On a network, a protocol is how data is exchanged; think of IP protocol or TCP protocol. But if you have communication endpoints you talk to over a network to trigger functionality, e.g. a REST API, than the sum of all these endpoints and their parameters can be called an interface, while triggering one of the interface functions would be done via a HTTP request and HTTP is a protocol and defines how data is transferred.

like image 43
Mecki Avatar answered Nov 16 '22 06:11

Mecki