Linux UDP Port Test
5832 ワード
http://sourceforge.net/projects/sendudp/?source=typ_redirect
sendup.cpp
sendup.cpp
/*
*
* Name: sendudp
* Version: 1.1
* Owner: Ahmed Jolani "[email protected]"
* Contributors:
1- Dan Milligan "[email protected]" (HandleCommandLineOptions, makefile, bash script for testing)
* License: Commons Attribution 3.0 License (CC3)
* Description: sendudp is a tool that allow you to test any UDP port, sendudp doesn't
* guarantee the delivery of the sent packets, it only send the packets for you to test your
* UDP application/software. Feel free to contribute to this tool and enhance it, please
* email me with any contribution to keep me updated.
*
*/
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <netdb.h>
#include <sys/types.h>
#include <errno.h>
#include <time.h>
using namespace std;
const char *VERSION_STRING = "1.1";
const char *s_commandOptions = "d:p:m:v"; // d = destination, p = port, m = message, v = print version
char remoteIpAddr[INET6_ADDRSTRLEN];
char remotePort[8];
char *messageToSend = NULL;
void msg(char * argv)
{
cout << "Usage [" << argv << "]: UDP connection tester." << endl;
cout << "sendudp -d {IPv4/IPv6} -p PORT -m {MESSAGE TO SEND}" << endl;
}
bool HandleCommandLineOptions(int argc, char * argv[])
{
bool success = true; // assume the best
int commandId;
bool hasIp = false;
bool hasPort = false;
bool hasMessage = false;
// const char *s_commandOptions = "d:p:m:v"; // d = destination, p = port, m = message, v = print version
cout << "Parsing command line options" << endl;
while ((commandId = getopt(argc, argv, s_commandOptions)) != -1)
{
switch (commandId)
{
case 'd':
{
memcpy(remoteIpAddr, optarg, strlen(optarg));
cout << "Sending to remote ip address: " << optarg << endl;
hasIp = true;
}
break;
case 'p':
{
memcpy(remotePort, optarg, strlen(optarg));
remotePort[strlen(optarg)] = '\0';
cout << "Sending to remote port number: " << remotePort << endl;
hasPort = true;
}
break;
case 'm':
{
messageToSend = new char[strlen(optarg) + 1];
sprintf(messageToSend, "%s
", optarg);
hasMessage = true;
}
break;
case 'v':
{
cout << "You are running version: " << VERSION_STRING << endl;
}
break;
default:
{
success = false;
}
break;
}
}
// If any one of these has not been set then no go
if ((hasIp != true) || (hasPort != true) || (hasMessage != true))
{
success = false;
}
return success;
}
int main(int argc, char * argv[])
{
if (HandleCommandLineOptions(argc, argv) == true)
{
cout << "Sending message to: " << remoteIpAddr << " using port: " << remotePort << endl;
struct addrinfo hints;
struct addrinfo *response;
memset(&hints, 0, sizeof hints);
hints.ai_socktype = SOCK_DGRAM;
hints.ai_family = AF_UNSPEC;
if (getaddrinfo(remoteIpAddr, remotePort, &hints, &response) == 0)
{
int socket_fd = -1;
struct addrinfo *target = NULL;
for (struct addrinfo *ptr = response; ptr != NULL; ptr = ptr->ai_next)
{
if ((socket_fd = socket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol)) == -1)
{
continue;
}
target = ptr;
break;
}
freeaddrinfo(response);
if ((socket_fd != -1) && (target != NULL))
{
int bytesSent = sendto(socket_fd, messageToSend, strlen(messageToSend), 0, target->ai_addr, target->ai_addrlen);
if (bytesSent == strlen(messageToSend))
{
cout << "Sent message successfully!" << endl;
}
}
else
{
cout << "Failed to open socket." << endl;
}
}
else
{
cout << "Failed to get address information." << endl;
}
}
else
{
msg(argv[0]);
}
if (messageToSend != NULL)
{
delete [] messageToSend;
}
return 0;
}
Makefile:#
# (C) Copyright 2012-2013
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundatio; either version 2 of
# the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston,
# MA 02111-1307 USA
#
VERSION = 1
PATCHLEVEL = 01
SUBLEVEL = 01
EXTRAVERSION =
ARCH = ARMc
ifeq ($(ARCH), ARM)
-include /usr/local/ti-sdk-am335x-evm/Rules.make
CC=$(CROSS_COMPILE)g++
else
CC=/usr/bin/g++
endif
sendudp: sendudp.o
$(CC) -o sendudp sendudp.o
sendudp.o : sendudp.cpp
$(CC) -c sendudp.cpp
clean:
rm -f sendudp
rm -f sendudp.o
test.sh#!/bin/bash
./sendudp -d 224.0.0.1 -p 8888 -m testing