Share your repls and programming experiences

← Back to all posts
Thousandth Cycle Celebration - TCP Server in C++ (Unix sockets)
Highwayman (1357)

PLEASE TAKE NOTE!!!

the repl DOES NOT WORK ON REPLIT. Any attempts to connect to this repl should fail with. Error 502 or something like that.

To celebrate getting 1000 cycles, I’ve decided to post this repl I made a couple weeks ago. It’s a class that creates a TCP server, with connections being handled by a supplied callback in an asynchronous fashion. I think. #include Server.hpp in your main.cpp or wherever it is you want to use it.

When constructing a Server, you must pass 3 arguments:
1) a callback function (used to handle tcp connections, must return void and take a file descriptor and sockaddr_storage pointer)
2) a port number (should actually be a string literal, best between 1030-6500 I think)
3) a Boolean. (This tell me whether or not you want me to automatically close connections for you after the handler function has been called)

After having made the server you then must call the start() function in order for it to actually begin accepting connections. The start() function takes one argument (an int), which tells me how many threads you want running at once to handle connections. It returns a vector of all those threads, including a separate thread that actually accept()s connections as the last one.

I was gonna try and make a chat server out of it as an example, but got bored and unmotivated due to the extremely frustrating and unhelpful quality of this class I made.

I hope you like it.

Edit: oh also it doesn’t work on repl.it unless you make your requests and responses look like they follow HTTP, so... yeah.... That’s not too hard though so don’t sweat it I’ll be including the RFC for HTTP/1.1 here soon and I was thinking about maybe making a second class / lib type thing that parses/generates HTTP packets for you, but don’t count on it.

Edit2: the RFC for HTTP/1.1 is at http://tools.ietf.org/pdf/rfc7230.pdf

Edit3: just tried it with a package that should have been accepted. Didn’t work. :(

Commentshotnewtop
CSharpIsGud (577)

+1 for using sys/socket

Highwayman (1357)

@CSharpIsGud quick question: you upvoted for using sys/socket.h. Why? I kinda was just like ok noice thanks, but why upvote for sys/socket.h when there are probably more cross platform libs out there like curllib or whatever it’s called maybe.

CSharpIsGud (577)

@Highwayman it takes more effort than just plugging in a library and telling it to do something for you, those cross platform libraries have to use a separate networking api for each platform. they just hide it from you

Highwayman (1357)

@CSharpIsGud ok.. but I’d think all the work that is put in for the libs are mainly to make the server portable, and this definitely isn’t portable, lol. But yeah I guess I see where your coming from, thanks! :P

DynamicSquid (3549)

Did your account actually get taken away from you?

Highwayman (1357)

@DynamicSquid yes it was. (Still is, don’t be fooled This is sure to be a temporary solution.) :(

DynamicSquid (3549)

@Highwayman YOU'RE BACK! What happened? Or it's fine if you don't want to tell me, it's up to you

Highwayman (1357)

@DynamicSquid eh it’s ok. My grades were slipping basically, so me mum took my account so.. yeah.. congrats on 14 hundred! Wow! Been a f’ing while XD

DynamicSquid (3549)

@Highwayman oh yeah thanks! you missed a lot while you're gone... well is there anyway you could get your account back? You're like one of the best programmers here, and you did C++

Highwayman (1357)

@DynamicSquid ehh I’ll get it back, but in a reduced capacity. It won’t be like it used to. I’m not really that much of a programmer lol I just started for reals like last year XD ty tho.

DynamicSquid (3549)

@Highwayman oh, well, hope we get you back soon!

Highwayman (1357)

@DynamicSquid wow that was quick I’m back! Lol

DynamicSquid (3549)

@Highwayman wait what? you're account's fine now?

Highwayman (1357)

@DynamicSquid ehh not exactly. I get it back in that reduced capacity I was talking about.

DynamicSquid (3549)

@Highwayman oh, okay. But you're back!! Want an update of things you missed?

DynamicSquid (3549)

@Highwayman

note: NOT in chronological order

1) LizFoster also took a break from repl.it around the same time you did. She stopped posting completely. She's still working on her Pokemon game though

2) TheDrone7 (mod) has been very active! He has a #Weekly Challenges series he posts every week. Solve the challenge, and the winner after a month gets free hacker plan! Here's the result of last month, and for this month, we've only done one. 3 more to go! He also posts a list of some cool repls in May. And, he's been releasing some cool developer spotlights. There fun to read.

3) I got quite a lot of cycles after releasing some arcade games, and some other projects. CodingCactus has 2200 now, CoderGod has 2300, Bookie has 2100, and couple more jumps.

4) BramJam Game Jam happened! That was pretty cool! Next one's in august I think

5) It's pride month, that's why me and quite a few other people have rainbow backgrounds.

6) Some changes to repl.it - the trending algorithm is harder now so low quality posts can't get on easily. Also better support for Git and GitHub. Also audio

7) We (the community) actually had a HUGE debate over cycles, Amasad and a couple other mods got involved, and basically it was about cycle farming, and begging for cycles, and all that stuff. So now, I wouldn't recommend saying "And don't forget to upvote!" or something similar in your posts anymore, since some people hate it and won't uvpote your posts because of that

And yeah, that's pretty much it I think :)

Highwayman (1357)

@DynamicSquid

1) aw. Well at least she’s probably getting more sleep lol.
2) noice! I’ve seen quite a few try but no one ever seemed able to actually get into it...
3) 👏 ohh yeah. LOTS of cycles lol. F’ing 1451 that)s just so sick I wish I’d been able to capitalize on the covid-19 shut-in like that.
4) ? Never heard of it... sounds cool though!
5) I wonder if I can find a rainbow version of a zombie getting shot.... I actually like your rainbow version a lot, even better than your old background it’s super cool ^ ^
6 & 7) FINALLY! too them long enough XD I’m glad. Hum. I did do that once, didn’t I?

[deleted]

Hmmmmmmmmmmmmm........ we can’t reach repl

Highwayman (1357)

@CodeABC123 error 520 o r something right? That’s because I’m not sending a HTTP compliant packet.

Highwayman (1357)

@avibeskrowni ... it was 1003 just a second ago I guess that’s because uh... idk.

avibeskrowni (135)

@Highwayman 1010 cycles NICE!
I'm hoping to be able to break 500 cycles by the end of the year bc I'll be posting a lot more.

Great code too

Highwayman (1357)

@avibeskrowni thanks! Good luck on getting to 500 cycles, I’m sure you’ll get there easily enough.

[deleted]

I want 1000 @avibeskrowni

Highwayman (1357)

@CodeABC123 If I’m able to get 400 in 2 months, hen that means you should be able to get.....
400 / 2 = 200
200 * 12 = 2400
O.o
Um.
Uh.

[deleted]

Oh wow how am I going to make a repl that gets that much cycles. @Highwayman

Highwayman (1357)

@CodeABC123 nah nah nah, it’s not like that that would be collective cycles you get just from commenting and answering ppl’s questions. If you toss in making a repl or two that’ll get in hot it’s probably gonna shoot waaay up.

@avibeskrowni :D

ChezCoder (1497)

@ChezCoder: So, uhh hey, I know ur awesome at C++ and all that stuff so wanna work on something C++?
@Highwayman: Nahhh I suck at C++ XD
@ChezCoder: Oh...
@Highwayman: Creates This

XD