Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

windows sockets and C++

Options
  • 30-08-2001 10:34am
    #1
    Registered Users Posts: 861 ✭✭✭


    Hi,
    I got a quick messsage for any C/C++ programmer's out there. have any of you tried using sockets under windows / MSVC?? if so please help me - whenever i try to make a program that communicates over tcp/ip say over the internet, the programs always loose synch / get confused. I THOUGHT the idea of windoes sockets and the tcp protocol built in made the transport of info pretty safe, but when i try it, it works fine when im using my own pc as the server and client, but when i try it over a netowrk, it sometimes works somtimes fails. It also seems that any data i want to send accross must be split up into very small chunks -or else it really screws up - why is that?
    can anyone give me a quick code example of two-way communication that works for (possibley) large buffers of data?
    Cheers guys!!!

    p.s. i dont check the boards so often, so if you could email it to me:
    tails_nafXXX@yahoo.com

    (XXX is a spam block)


Comments

  • Registered Users Posts: 897 ✭✭✭Greenbean


    If it works internally on your pc but not over a network then it suggests timing differences and generally lack of data loss protection. Over your own pc all packets are probably arriving in due time and you don't need to order events, but in a network you are suddenly under different sets of much more unpredictable packet arrivals.

    What I'm saying is, it probably isn't coded safely under your own internal test, but since there is rarely congestion, slow downs, whatever you never see the problems. What you will have to do is program in an event driven manner. Go look it up.

    An example is if you have a socket setup and data incoming. You expect to receive a large chunk of data and you then process this and return it or whatever. Interally because the memory subsystem is so fast you never have to worry about all the data not being received when you go to process it. Hence you never notice any problems. But on a network some of the packets are slow and you dont receive them in time.

    So here you would delay the program until all the data is received. How would you know when this is? A handy way is to make the first 4 bytes of the "message" indicate the size of the rest of your "message" and not let the program advance until at least four bytes have been received and the amount of data indicated by the first four bytes have been received. This should also let you ignore most packet fragmentation issues (leave that to the os) and let you simply work with any size of a "message".

    Dunno if this helps. In essense tcp/ip guarantees arrival of data (or as best warning as possible if it doesn't arrive), the order of the data is preserved but not the timing. So the timing is the place to look for problems - with leads to dealing with it eiter via timeouts or event driven programming.


  • Registered Users Posts: 861 ✭✭✭tails_naf


    thanks for the info. i did try to do that as much as possible. but the only semi-reilable way i got of transmitting any data was the spilit it up into tiny packets. isnt tcp supose to in effect "stream" the data back, or is there any set of wrapper functions that does the grunt work for me, i.e. i just call on both send and receivve sides and it does all the checking / data syncing / whatever is required??


  • Registered Users Posts: 16,402 ✭✭✭✭Trojan


    <font face="Verdana, Arial" size="2">Originally posted by tails_naf:
    isnt tcp supose to in effect "stream" the data back, or is there any set of wrapper functions that does the grunt work for me, i.e. i just call on both send and receivve sides and it does all the checking / data syncing / whatever is required??</font>

    TCP should do all that. If you want manual error-check (and you don't seem to) then you could use UDP packets. Think of TCP as a phone call (end to end streaming connect), and UDP as a postcard (one-shot, no delivery guarantee).

    Talking about protocol stuff, have a look for Tannebaums "Computer Networks", (3rd or 4th edition), it should help you out some more with this, and is well worth having on your bookshelf. A search of the history on this board, for the string "Tanne" should result in the amazon links.

    Are you using MFCs CSocket class? Give us some code snippets.

    Al.


  • Registered Users Posts: 16,402 ✭✭✭✭Trojan




  • Closed Accounts Posts: 5,564 ✭✭✭Typedef


    I don't know if the setsockopt function exists in winsock but, if it does you might try setting sockopt to TCP_NODELAY?



  • Advertisement
Advertisement