Home  +  Forums  +  C++ and Sockets  +  C++ and SQL: MySQL, sqlite, ODBC  +  Miscellaneous Projects
Logo
~Sockets~
~New~
~Examples~
~Contact~

C++ Socket library tutorial

UDP socket example

Prev   |   Menu   |   Next

The UDP socket server example.

udpserver.cpp
#include <StdoutLog.h>
#include <SocketHandler.h>
#include "UdpTestSocket.h"


int main(int argc,char *argv[])
{
	SocketHandler h;
	StdoutLog log;
	h.RegStdLog(&log);
	UdpTestSocket s(h);
	port_t port = 5000;

	if (s.Bind(port, 10) == -1)
	{
		printf("Exiting...\n");
		exit(-1);
	}
	else
	{
		printf("Ready to receive on port %d\n",port);
	}
	h.Add(&s);
	h.Select(1,0);
	while (h.GetCount())
	{
		h.Select(1,0);
	}
}



udpclient.cpp
#include <StdoutLog.h>
#include <SocketHandler.h>
#include "UdpTestSocket.h"


int main(int argc,char *argv[])
{
	SocketHandler h;
	StdoutLog log;
	h.RegStdLog(&log);
	UdpSocket s(h);

	s.SendTo("localhost", 5000, "Using SendTo with address to send");
	if (!s.Open("127.0.0.1", 5000))
	{
		printf("Exiting...\n");
		exit(-1);
	}
	for (int i = 0; i < argc; i++)
	{
		s.Send(argv[i]);
	}
}



Prev   |   Menu   |   Next

Valid HTML 4.01!

Validate
Page, code, and content Copyright (C) 2021 by Anders Hedström