Hi.
I was trying to learn the basics about sockets programming but I'm stuck with some questions and some code I can't fix.
I have been reading some examples and the one I liked the most was this one:
https://www.binarytides.com/socket-prog ... -tutorial/
However, most examples are meant to create a server that listens and accepts connections and automatically replies to the client. But the client here is a telnet connection to some localhost port.
What I want to do is to code a simple program that I can run 2 instances and then, from 2 different terminal windows, I can send and receive messages between the 2 clients.
So, I started coding but I get to a point where the recv() function returns -1 (some error) and I can't figure out why.
But before showing the code, I want to ask at least two questions that I don't find the answer in any of the examples I've seen.
The usual approach I usually find on these examples is:
1 - create a socket
2 - Bind the socket
3 - Listen for connections
4 - Accept connections
5 - Reply to the client
The first question is not exactly about sockets. It's about binding. I still can't understand what binding means in the different contexts it is used in. Binding applications, binding sockets, etc. I think these two examples have slightly different meanings. Can anyone give me a brief explanation of what binding means?
The second question is about the code. We first create a socket and then, when we are using the accept() function, there is another socket created. Why do we need to create 2 sockets?
Now, on to the code.
https://paste.debian.net/1171406/
I know this is a very dummy code, but the point is that when the code tries to receive messages from the telnet connection, it returns -1, which means some error occurred!
Can anyone tell me what am I doing wrong? I know this is not my goal, but when I get this working, I'll move one to my goal of having 2 instances of this code running and being able to send messages back and forth between the 2 instances.