Jan 22, 2010 at 12:00am Jan 22, 2010 at 12:00am UTC
Hi,
I would like to create a UDP client that connects and sends data to a server
I have two questions.
Will the standard Winsock library be sufficient?
if not, which socket library would you suggest i require ?
Last edited on Jan 24, 2010 at 5:51pm Jan 24, 2010 at 5:51pm UTC
Jan 22, 2010 at 1:08am Jan 22, 2010 at 1:08am UTC
the standard winsock is sufficient...
Jan 22, 2010 at 2:30pm Jan 22, 2010 at 2:30pm UTC
Thank you for your reply
Last edited on Jan 24, 2010 at 5:51pm Jan 24, 2010 at 5:51pm UTC
Jan 22, 2010 at 5:31pm Jan 22, 2010 at 5:31pm UTC
I really like Poco Library. Its cross compatible. And its sockets are wicked easy to use.
http://pocoproject.org/docs/
Even has socketstream to stream copiers or socketstream to std::string copiers.
Quick example (not UDP) of sending / receiving text using strings:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
#include <Poco/Net/StreamSocket.h>
#include <Poco/Net/SocketStream.h>
#include <Poco/Net/SocketAddress.h>
#include <Poco/StreamCopier.h>
#include <Poco/Thread.h>
#include <string>
const char * host = "127.0.0.1" ;
int main()
{
std::string outstring;
Poco::Net::StreamSocket socket(Poco::Net::SocketAddress(host, "5221" ) );
Poco::Net::SocketStream ss(socket);
ss<<"This will get sent to the server!" <<std::endl;
ss.shutdownsend();
while (true )
{
if (socket.poll(100, SELECT_READ) == true )
{
if (Poco::copyToString(ss, outstring) > 0)
{
std::cout<<"Received " <<outstring<<" from sever" <<std::endl;
break ;
}
}
Poco::Thread::sleep(100);
}
return 0;
}
Note: I didn't compile this. So some tweaking may be necessary. Could even remap std::cout to receive the string stream. I created a simple cout forwarder of TCP/IP using this method. It would buffer up then transmit then rebuffer. Worked pretty well. I remapped the cout to the socketstream
Last edited on Jan 22, 2010 at 5:39pm Jan 22, 2010 at 5:39pm UTC