Enchom's blog

By Enchom, 10 years ago, In English

Marathon24 finished a few weeks ago and my team has received a chance to go to the Grand Finals. However reading carefully the rules described, I saw the following thing :

Communication is realized with text commands sent through the TCP/IP protocol, described in details in the problem statement.

I also checked problems from last year and indeed, they give you a set of commands you can send through TCP/IP to communicate with the contest system.

Now the problem is that none of our team has any clue how this whole TCP/IP communication works. I would really appreciate it if someone could explain how it works and how to create a C++ code that communicates through TCP/IP, as I believe that's what you are expected to do during the competition.

Thank you in advance! :)

Edit: Turns out we can't participate due to complications with travelling, however the question is still valid! :)

  • Vote: I like it
  • +11
  • Vote: I do not like it

»
10 years ago, # |
  Vote: I like it 0 Vote: I do not like it

I should say it's very easy if you use Java (or C#, as it's almost the same): just find the class Socket and its usage

  • »
    »
    10 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Is it possible to do it in C++? If it isn't, I suppose C# is close enough but C++ would still be more comfortable. Also, is it possible to link me to sample implementation or something? We really have no idea how to do any of it.

    • »
      »
      »
      10 years ago, # ^ |
        Vote: I like it +12 Vote: I do not like it

      You can make a link between Java and C++. Java reads packets from TCP and passes it to a standard input stream of an external program (C++). External program does calculations and writes output to a standard output stream, which is then picked up by a running Java process.

    • »
      »
      »
      10 years ago, # ^ |
        Vote: I like it +16 Vote: I do not like it

      You can use boost in C++.

      I used it at Deadline24.

          #include<boost\asio.hpp>
          ...
          boost::asio::ip::tcp::iostream connection(host, port);
          boost::asio::ip::tcp::no_delay opt(true);
          connection.rdbuf()->set_option(opt);
          if(!connection.good()) return -1;
          string s;
          getline(connection,s);
          connection<<"lalalalalalalala\n";
      

      You just create something like your cin/cout stream and use it as you want.

»
10 years ago, # |
  Vote: I like it +3 Vote: I do not like it

https://github.com/glapul/marathon_lib

wrapper.* and ctcpfwd.*

it may contain bugs.

»
10 years ago, # |
  Vote: I like it 0 Vote: I do not like it

As I rememberو using bash there is some way to pipe I/O of some command into another command, so they read each others I/O and respond to each other. Using this you can pipe the commands nc localhost 5555 and ./a.out together, assuming your programs binary file is a.out and TCP server is running on localhost:5555.

I don't remember the syntax, so if anybody knows how to do that, please let us know. :)

  • »
    »
    10 years ago, # ^ |
      Vote: I like it +3 Vote: I do not like it

    Finally I found the way to do it, I mailed the person who told me this first and got a reply.

    For the above example, you can run your code with the following bash command:

    read | { ./a.out | nc localhost 5555; } > /dev/fd/0
    

    This way you write a simple code without TCP/IP stuffs, like every other query based or interactive problem in on-line judges, and then you direct the I/O to a socket instead of standard I/O. You can have an eye on what your program sends or receives, by printing into error stream. You might like to read this, to know the details of what I said.

    There is another way to do this with something called fifo in bash. You can make them with mkfifo. But I don't have much information about them, so you can google it.

    Last year during the Marathon24 finals, I knew that there's some way to do this, but I couldn't remember it and it was unfortunate. I just remember that day we did very strange things to communicate with sockets. :D