Thread: Sending and receiving data

  1. #1
    Registered User
    Join Date
    Jul 2012

    Sending and receiving data

    Hey, I have a question regarding network programming in general. Lets say I want to write a program in C that connects to a server and interacts with that server. How do I know how to communicate with the remote server? I mean sure I can just use a whole lot of send()s and receive()s but I won't be able to react in a way that is intelligent. I will be forced to "guess" where the server is and what it is saying. I don't know quite else how to explain what I mean. I guess that's what protocols and such are for? In short I am just looking for a way to write a program that can intelligently interact with a server, in C.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2006
    personally, I'd recommend C++ for that, if you know C++, and C is not a requirement. libraries like boost make it very simple, and wrap a lot of the menial tasks into nice classes that hide a lot of the boiler plate from you. no need to re-invent any wheels.

  4. #4
    Registered User
    Join Date
    Jul 2012
    Well, I mean I know how to do it technically, on a low level, like filling in structs and using connect() and such. But on a higher level for instance. Lets say that I was to write a program that interacts with telnet. Well I'm fine with connecting to said host on port 23, I understand how to do that. The problem is when I receive a prompt for login such as "login:" or "username:" how does my program know it is at this prompt? Should I check for data on the connected socket and see if it contains the string "login"? That seems tedious to me and I will always be trying to guess where I'm at in the telnet connection by trying to compare the prompts to strings in my program.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    That's what protocols are for, as you've surmised. Suppose you were communicating with a mail server, for example. You'd (probably) be using the POP protocol, which is very simple: you say "USER someone", then "PASS something", and LIST to list files and so on. The error messages that the mail server presents must follow a specific format so that you always know exactly what's going on.

    Some programs are harder to interact with, usually because they've been designed for human use rather than automation. Yes, sometimes you have to guess in those cases. I'd suggest writing these types of programs in a language like Perl, which makes it easier to do the communication and can do pattern-matching on the text to make very good guesses as to what's going on. You can always exit with an error if you reach a point in the communication that you don't understand.

    In your case, interacting with telnet is a waste of time since telnet is such a simple program. Just write it yourself (if you're using C). If you're just shell scripting you can probably assume that telnet's prompts will always occur in the same order -- or you can pass the information to telnet via command-line parameters. If telnet doesn't do enough for you, you can use curl and wget to do more. You'll probably have to break out the Perl if you need to manage secure connections, fake cookies, gather information from other sources, or do something more complicated like that.

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell

    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ:

    My website:
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Socket Overflow by sending / receiving ACK's
    By MaSSaSLaYeR in forum C Programming
    Replies: 13
    Last Post: 11-24-2011, 02:38 AM
  2. TCP- Sending and Receiving a Struct
    By rishanth12 in forum C Programming
    Replies: 3
    Last Post: 09-12-2011, 12:32 AM
  3. What model should I use when sending receiving packets?
    By Subsonics in forum Networking/Device Communication
    Replies: 22
    Last Post: 05-03-2010, 12:34 AM
  4. Sending / Receiving Info Via TCP or UDP
    By bengreenwood in forum C Programming
    Replies: 0
    Last Post: 03-24-2009, 02:17 AM
  5. Sending/Receiving Messages
    By osal in forum C++ Programming
    Replies: 0
    Last Post: 02-19-2005, 09:11 PM