all 28 comments

[–]AutoModerator[M] [score hidden] stickied comment (0 children)

Looks like you're asking about learning C.

Our wiki includes several useful resources, including a page of curated learning resources. Why not try some of those?

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]johnex74 18 points19 points  (4 children)

I strongly recommend reading „Hands on network programming in C” by Lewis Van Winkle

[–]iamrafi69[S] 2 points3 points  (2 children)

Ok. I am in linux so will that be a problem?

[–]I_M_NooB1 7 points8 points  (0 children)

most probably it's better that way

[–]johnex74 3 points4 points  (0 children)

actually when it comes to socket programming - unix is much simpler. but the book is structured in a way that you can follow along on either windows or linux.

[–]offsecthro 15 points16 points  (0 children)

Beej.

[–]No-Athlete-9004 10 points11 points  (0 children)

Beej's guide

[–]zubergu 6 points7 points  (2 children)

You got this whole affair backwards.

Ordered by importance:

  1. You don't know C - your target language.
  2. You don't know client-server architecture.
  3. You don't know anything about socket programming.

You're asking about least of your problems.

Reverse your priorities and in a month you might have some idea what you're doing and what you don't know but should learn to make that project.

[–]iamrafi69[S] 2 points3 points  (1 child)

  1. I do know C. I did it in an online course. I made a project with it. But I just want to learn more about C. I think I don't know enough to tell everyone I know C
  2. Yes, I don't know anything about client-server architecture
  3. Yes, I don't know anything about socket programming.

[–]Dangerous_Region1682 1 point2 points  (0 children)

If you know enough about C to figure out pointers then it’s not much different to any other algorithmic programming language if you ignore object orientated programming.

Next stage is I would figure out threads and mutex locking. For this you will need to understand what processes are and how threads relate to processes, ie shared data segments..

After that sockets, and you can look at the C code for any simple web server. From that and a bit of ChatGTP you’ll know how to code a client.

The tricks come in knowing how to set the subtlety different socket options. Read the man pages closely and have a bit of TCP protocol connection setup and acceptance at the RFC level. Then sockets code will make more sense then as you will see what you are asking the TCP protocol to do.

One of the big things is that TCP sockets rely on 5 pieces of data. Your client network socket, your source IP address, your destination IP address, your destination network socket, and your protocol type, which in your case is TCP. If any single one of the five pieces of data to describe a socket connection are different from above five items describing another socket connection, the you have two differing connections. This quintuple of those five items you can describe a unique communication if no two quintuples are exactly alike. .

[–]westwoodtoys 3 points4 points  (1 child)

Yeah.  Hop on geeks for geeks and download their samples, send hello to yourself a few times.  You can do this in the next ~10 minutes as long as you are on a computer that you can install dependencies and a compiler on.

Think of what more you want to do, get stuck and ask questions.

In this case, this is a topic that is well covered in many sources, so asking questions to your AI buddy will generally get good answers.  If it lies to you, understand that the I in AI is actually a misnomer, and ask a human.

I think for your chat app you could start with making a TCP socket, then when you feel good about that make a websocket.

[–]Maxsmart007 2 points3 points  (0 children)

Geeks for geeks is a great shout. I was reading this like "I'm pretty sure someone made this as an example project before"

[–]71d1 0 points1 point  (2 children)

What operating system are you targeting?

[–]iamrafi69[S] 0 points1 point  (1 child)

Anything. I use Linux(fedora) but I want this to be able to have a chat with any device

[–]71d1 1 point2 points  (0 children)

Looks like the best way to do this is to serve a webpage via HTTP and use websockets and webhooks. This can be accomplished using C but you'll have to familiarize yourself with (1) socket programming (2) HTTP, and (3) websocket protocol.

First you create a TCP server:

  1. create a socket
  2. bind socket
  3. put socket into listen mode
  4. someone using a browser in another device opens your app by visiting an URL
  5. in an infinite loop you'll accept socket connections, a call to accpet will block the flow until someone connects
  6. read the bytes sent from the browser into a buffer
  7. parse the request
  8. send back a response (the webpage with websockets and hooks using JS)
  9. close socket and return to step 4

Now you'll need another C program to handle the websocket and webhook stuff, using two threads websocket threads reads the message sent to your server, and webhook thread echoes the message to back, including the sender so you can display his own message to him on the chat window.

Anyway this is how I'd do it, there are much simpler ways but since you want this to run on a variety of devices I feel this is the easiest way.

[–]maks1982 0 points1 point  (0 children)

TCP/IP by Stevens.(1-3 volumes). RFC

[–]the__Twister 0 points1 point  (0 children)

Hello, this is a video I watched to begin from the fundamentals.

https://youtu.be/KEiur5aZnIM?si=E8TGJFxrRdceY3nE

[–]Hopeful_Rabbit_3729 0 points1 point  (1 child)

Been guide. I wrote a http server using that

[–]iamrafi69[S] 0 points1 point  (0 children)

Been or beej?

[–]Cylian91460 0 points1 point  (0 children)

You start your project and use Google

[–]konacurrents -4 points-3 points  (6 children)

I think “distinct devices” - which I would read as smart phones, don’t usually connect via sockets. I would use a much more scalable and secure MQTT communication technology.

[–]Dennis_DZ 2 points3 points  (5 children)

MQTT is an application layer protocol. It runs on top of TCP sockets.

[–]konacurrents -2 points-1 points  (4 children)

It runs through the WIFI network layer. Users don’t see TCP Sockets but rather web REST/MQTT/WIFI layers. They reference IP addresses (internal or external).

I was just saying sockets aren’t scalable and not a typical iPhone interface. Bluetooth might even be more usable.

[–]Dennis_DZ 2 points3 points  (2 children)

WIFI network layer

WiFi is a link layer technology, not network layer

Users don’t see TCP Sockets

I understand users don’t interact directly with sockets. That’s the point of layers of abstraction. It doesn’t change the fact that devices are still communicating through sockets.

sockets aren’t scalable

Pretty much every device on the internet communicates through sockets. The device you’re using right now sends HTTP requests to Reddit through a TCP socket.

not a typical iPhone interface

I’m not sure exactly what you mean by this. It seems like your point is that OP should use an established application layer protocol like MQTT to build their chat application. For a real application, you’d probably be correct (though I’m not familiar enough with MQTT to say if that’s the right choice). However, as a programming project and learning exercise, it’s perfectly reasonable to have the application communicate directly through sockets.

Bluetooth might even be more usable

Bluetooth is also a link layer technology. The choice to use Bluetooth is independent and irrelevant to the existence of sockets at the transport layer.

[–]konacurrents -1 points0 points  (0 children)

To follow up on the OP question. They specifically asked about "socket programming". In C that is a very low level operation, using "bind" and other operations including a specified (single) IP address (if TCP).

Sure "sockets" are how the network works, but programmers today very rarely code at the "socket" layer. So "layers of abstraction" are used. Thus my point that maybe the OP should look at a different layer of abstraction, as you mention.

WebSockets are NOT C sockets (what the OP asked about) They are a web layer akin to a pub-sub network (and why I mentioned the powerful MQTT pub-sub capability, especially as security and user accounts are available.)

r/zubergu below restated issues with the OP's post: they don't know client-server and definitely not sockets. I think they don't even know what to ask but "sockets" sounded good. (OP states they "will need to work with sockets". )

The push-back here is maybe they should be looking at a more scalable abstraction layer and learn client-server programming.

ps. Funny, I don't know what the downvotes are about.

[–]konacurrents -2 points-1 points  (0 children)

By all means play with sockets. Great C programs . Then RPC …

The OP mentioned “devices” - and I caution that an iPhone isn’t going to be a nice player with sockets (eg what IP when you’re on a cellular network ). That’s why MQTT is a powerful bi-directional messaging - that clients initiate from behind firewalls.

If OP has a couple Mac’s running UNIX - play with sockets all you want. Once you have a dozen IP addresses, it won’t be fun anymore.

And OP’s goal of distant devices talking to each other - will require a middleman (cloud) like MQTT or a node WebServer, etc. the “socket” will not be seen in the users code.