This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Mastering Corda: Blockchain for Java Developers and have Jamiel Sheikh on-line!
See this thread for details.
Win a copy of Mastering Corda: Blockchain for Java Developers this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

new to C - I'm a bit lost

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'd like to apologize for the not so meaningful topic title upfront - but it was the first that came to my mind.

So, I recently stumbled into game development. Not the kind of designing something entirely new from scratch but rather modifications to an already existing game. As for my background: I have a couple of years worth of Java knowledge, so I'm familiar with high level object oriented programming, but I'm also quite spoiled with automatic memory management.
So, as the game I'd like to write a modification for somewhat requires a C-family language, although on the docs are also examples in Delphi/Pascal and D, I looked into C++ as I really like the object orientation of Java. And my first impression is: I'm a bit overwhelmed by it. There seem to be different styles/dialects, quite a lot what the standard C/++ library comes with, and a lot it doesn't come with, and just as for Java quite a lot of additional libraries, but just way more.

For my specific task I looked into how to implement a simple TCP client, but it's quite a lot different to what I'm used to from Java.

As I noticed these are important information for others, here are my specific "requirements":
- target os: Linux, 64bit only
- target output: shared object (or: dynamic link library as they're called on Windows)
- things to implement: a TCP client, light multithreading, something equivalent to Javas synchronized keyword, a small collection like a linked list

A bit more specific:

The game is Arma3. It supports an Extensions called modification API. It does have some limiting factors: The communication is done via exchange of strings, currently limited to about 10kB in each direction, and the response has to be quite fast within the time of the frame that triggered the call. As this games engine is quite unoptimized even on very good systems it only runs up to about 100 to 200 frames per second, my personal average is about 30. So as responsetime we speak about times somewhere between 1/30 to 1/200 of a second.
I looked into another rather popular extension, which keeps within the timing by assign a unique id to each request and ofloads them into something like an ExecutorService running on a ThreadPool, and then just returns that id. It's then up to the script within the game engine to keep polling until the process is finished and the response is ready for retrieval.

My plan goes like this: Implement some really basic stub which takes the requests, assigns them an id to return to the game, and just redirects the request via a socket connection to a backend written in Java.
According to the docs there's a way for the extension to asynchronously callback some script in the game code, but I don't have any experience with it yet.

So, my question now is this: As I hardly can ask someone else to do my work I'd like to ask for some advice where to find resources about the topics involved. Maybe some books subjected for beginners to somewhat advanced readers. I guess it'll end up in quite some copy and paste until it fits my needs, as it's only meant to be a bridge between the game engine and Java, as the main development will happen in Java.

I already found the site cppreference.com, but it doesn't have any informations about socket programming. On other search results I can't really tell if it's C or C++ or if it's for Linux or Windows, and if can be used in a library or if it requires it's own application.

Pretty much: I'm quite lost for that what seems an easy task as all that holds me back do implement it in Java is that the fame requires a dynamic library to be loaded during runtim - one thing Java is not able to provide as result output.


Oh, btw: merry christmas to all of you
 
Rancher
Posts: 287
11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Matthew - merry Christmas!

What you're trying to do sounds quite complicated but here is some basic TCP client/server C source code, if it's any help. I've used it before for simple TCP stuff, nothing like the sort of thing you're trying to do:
https://www.geeksforgeeks.org/tcp-server-client-implementation-in-c/

Cheers
John
 
John Matthews
Rancher
Posts: 287
11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I should have said, I only do linux stuff, so I can't guarantee that it's any good for Windows. Apologies if not.
 
Matthew Bendford
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John Matthews wrote:Hi Matthew - merry Christmas!

What you're trying to do sounds quite complicated but here is some basic TCP client/server C source code, if it's any help. I've used it before for simple TCP stuff, nothing like the sort of thing you're trying to do:
https://www.geeksforgeeks.org/tcp-server-client-implementation-in-c/

Cheers
John


John Matthews wrote:I should have said, I only do linux stuff, so I can't guarantee that it's any good for Windows. Apologies if not.


Hello John,
thanks for your reply.

As for Windows: Don't worry, my target platform is Linux 64bit only.
The link you posted, although quite basic, seem to give me at least a jump start have a look how socket communication looks in C, which is quite a lot different to Java.

However, as this is one of many C examples I was able to find myself, I wasn't able to find C++ specific examples. Using something like while ((buff[n++] = getchar()) != '\n') ;? Well, sure, I've seen even worse to read in a line of text from a terminal, but I'm looking for a more "high level" approach. I don't know if you know Java well enough, but a simple echo server with a client reading in from the terminal is quite a lot simpler.
As I had not yet spent time to learn the basics of "the C family of languages" I'm also quite, well, baffled? by all those "sizeOf()" and explicit sizes and lengths - but I've also read the news about TLS heartbleed how a missing length check allowed an attack to read out memory remotely without causing a seg-fault on the server. But I guess without reading a beginners book (or maybe even a book targeted to some advanced users ?) explaining all that I just have to take it as given (a thing I had to learn over a long time with a lot of patience).

Does sound my goal complicated? Well, I guess it does to someone not knowing the game and never looked into its "modding capabilities", but from my few years of programming experience it only seems to be some basic glue logic.
 
John Matthews
Rancher
Posts: 287
11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
https://stackoverflow.com/questions/118945/best-c-c-network-library ?

Just noticed that post is 12 years old, but the handful of links I clicked on all seemed to be still valid.
 
Matthew Bendford
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Quite interesting to see the differences between C and Java just in that one point. Guess I have to keep searching.

As for my other topics: How about baking all that into a dynamic loadable library?
Currently I just copied the example code from the docs page, looked up how to use gcc to get a .so and tried to load it in the game, which, to my surprise, actually worked right at the first time. As I try to orient myself along other existing extensions I noticed many use constructors and destructors to set up / start up the inner workings of the extension, like connecting to a database, and stop it when the game "destructs" the extension, although at least the I try to follow doesn't seem to get correctly unloaded.
As I'm not familiar with using system resources directly, but rather relied on the Java VM to take care of that for me, I'm not sure if I need to follow suite and implement an explicit constructor and desctrutor, or if it'll be ok if I just have a global pointer I check for if it's null, establish the connection if it's not yet done, and then start using it without caring about it's correct disconnecting on shutdown, or if I do have to keep track of system resources and release them explicitly cause otherwise I would clog up the system over time. Any advices on this one?
 
Saloon Keeper
Posts: 23006
156
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just to be pedantic, a Linux/Unix shared object isn't quite the same as a DLL. A DLL has a certain required payload, as I recall, and 'so's can be versioned, which greatly reduces the "DLL Hell" effect that Windows is infamous for,

*n*x Network programming hasn't changed all that much over the years. Probably the biggest change was the addition of IPV6, but the BSD socket stack was designed back when TCP/IP was only one of several network architectures in wide use back then. so augmenting it wasn't a major trauma.

Java is "write once/run anywhere". but its primary model was Unix, so there's a lot of similarity between the native Unix/Linux C network functions and their Java equivalents. Windows, incidentally, plundered the Unix network stack extensively, so there was only minimal corruption of that model.
 
I have gone to look for myself. If I should return before I get back, keep me here with this tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic