Exemplo n.º 1
0
int main(int argc, char** argv){

	// Creating required variables for UDP socket
	int userChoice;
	struct sockaddr_in server_address;
	int client_socket;
	socklen_t slen = sizeof(server_address);

	// Creating buffer for sending/receiving datagrams (512 characters, defined in header)
	char buf[BUFLEN];

	// Create the socket
	if ((client_socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1){
		perror("Socket error\n");
		exit(1);
	}

	// Set up the address
	memset((char *)&server_address, 0, sizeof(server_address));
	server_address.sin_family = AF_INET;
	server_address.sin_port = htons(PORT);

	// Internet address manipulation?
	if (inet_aton(SERVER_ADDR, &server_address.sin_addr) == 0){
		perror("inet_aton() failed\n");
		exit(1);
	}

	// Keep allowing user to choose, choice of 4 will exit, nice visual menu for easy understanding
	while(1){
		printf("+-------- C Datagram Client --------+\n");
		printf("|     1. Echo Server                |\n");
		printf("|     2. DNS Lookup service         |\n");
		printf("|     3. Get server time.           |\n");
		printf("|     4. Quit                       |\n");
		printf("+-----------------------------------+\n");
		printf("What would you like to do: ");
		scanf("%d", &userChoice);

		if(userChoice == 1) { 
			// Let user send a message to the server
			echoServer(client_socket, server_address, slen); 
		}

		else if(userChoice == 2) {
			// Let user get the IP for their requested URL
			dnsLookup(client_socket, server_address, slen);
		}
		else if(userChoice == 3) {
			// Get the server time
			serverTime(client_socket, server_address, slen);
		}
		// Exit the client
		else if(userChoice == 4) return 0;

		// Invalid input/choice
		else printf("You must enter 1, 2, 3, or 4.\n\n");
	}
}
Exemplo n.º 2
0
//Initialize GroveStreams
void GroveStreams::begin(void)
{
    int ret = dnsLookup(_serverName, serverIP);
    if (ret == 1)
    {
        Serial << millis() << F(" GroveStreams ") << serverIP << endl;
    }
    else
    {
        Serial << millis() << F(" GS DNS lookup fail, ret=") << ret << endl;
        mcuReset();
    }
    ipToText(_localIP, Ethernet.localIP());
    ipToText(_groveStreamsIP, serverIP);
}
Exemplo n.º 3
0
bool initAddress(SOCKADDR_IN& saddr, const std::string& addr, unsigned short port, bool getByName){
	memset(&saddr, 0x00, sizeof(SOCKADDR_IN));

	saddr.sin_family = AF_INET;

	if(addr.size()==0){
		saddr.sin_addr.S_un.S_addr = INADDR_ANY;
	}
	else{
		if(getByName){
			if(dnsLookup(addr, &saddr.sin_addr)==false)
				return false;
		}
		else{
			saddr.sin_addr.S_un.S_addr = inet_addr(addr.c_str());
		}
	}

	saddr.sin_port = htons(port);

	return true;
}
Exemplo n.º 4
0
 object *socket::connect(string *host, integer *port)
 {
     if (socket_val < 0)
     {
         exceptions::SocketError *exc = exceptions::SocketError::Create(
             1,
             new string("Socket already closed.")
         );
         exc->throw_exception();
     }
     
     if (dnsLookup(host->string_val.c_str(), port->val, 0) < 0)
     {
         exceptions::SocketError *exc = exceptions::SocketError::Create(
             1,
             new string("Could not open connection.")
         );
         exc->throw_exception();
     }
     
     return new boolean(true);
 }
Exemplo n.º 5
0
 object *socket::bind(string *host, integer *port)
 {
     if (socket_val < 0)
     {
         exceptions::SocketError *exc = exceptions::SocketError::Create(
             1,
             new string("Socket already closed.")
         );
         exc->throw_exception();
     }
     
     int ret = dnsLookup(host->string_val.c_str(), port->val, 1);
     if (ret < 0)
     {
         exceptions::SocketError *exc = exceptions::SocketError::Create(
             1,
             new string("Could not look up host.")
         );
         exc->throw_exception();
     }
     
     return new boolean(true);
 }
Exemplo n.º 6
0
int main( int argc, char** argv) {

	// Creating required variables for UDP socket
	struct sockaddr_in server_address, client_address;
	int server_socket;
	ssize_t recv_len;
	socklen_t client_address_len = sizeof(client_address);

	// Most buffers used will be 512 characters (BUFLEN)
	char buf[BUFLEN];

	// Store client's choice for each server loop iteration
	char choice;

	// Create a UDP socket
	if ((server_socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1){
		perror("Error creating socket.");
		exit(1);
	}

	// Zero out the structure
	memset((char *) &server_address, 0, sizeof(server_address));

	// Setting address
	server_address.sin_family = AF_INET;
	server_address.sin_addr.s_addr = htonl(INADDR_ANY);
	server_address.sin_port = htons(PORT);

	//bind socket to port
	if(bind(server_socket, (struct sockaddr*)&server_address, sizeof(server_address)) == -1){
		perror("Error binding socket.");
		exit(1);
	}

	//keep listening for data
	while(!0){

		// Initially waiting for datagram
		printf("Waiting for data...\n\n");

		// Clear out the buffer memory for safe usage each time
		memset(buf, '\0', sizeof(buf));

		//try to recieve some data, this is a blocking call, show error if something goes wrong
		if ((recv_len = recvfrom(server_socket, buf, sizeof(buf), 0, (struct sockaddr *)&client_address, &client_address_len)) == -1)
		{
			perror("Error in recvfrom");
			exit(1);
		}

		// Check for the requested service to send back to client (first char of received message)
		// 1 = Echo a message
		// 2 = URL to IP lookup
		// 3 = Get current time

		choice = buf[0];

		// Choice 1: Echo
		if(choice == '1') {

			// For each case, let a function handle what needs to be done
			echoMessage(server_socket, client_address, client_address_len);

		} 
		// Choice 2: DNS Lookup 
		else if(choice == '2') {

			// Pass in the socket and address to be able to send datagrams back to client
			dnsLookup(server_socket, client_address, client_address_len);

		} 
		// Choice 3: Get current time
		else {

			getTime(server_socket, client_address, client_address_len);
		}

	}

	close(server_socket);
	return 0;
}