Skip to content

Instantly share code, notes, and snippets.

@LeoAdamek
Last active September 25, 2015 00:38
Show Gist options
  • Save LeoAdamek/834684 to your computer and use it in GitHub Desktop.
Save LeoAdamek/834684 to your computer and use it in GitHub Desktop.
Some scripy cody thing to check open / forwarded ports.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Windows.Forms;
namespace PortChecker
{
public partial class MainWindw : Form
{
public System.Net.IPAddress ServerAddress = System.Net.IPAddress.Parse("192.168.0.5");
public MainWindw()
{
InitializeComponent();
}
private void GoCheck_Click(object sender, EventArgs e)
{
if (int.Parse(PortNumber.Text) > 65535)
{
Result.Text = "Invalid Port";
}
else
{
/*
* Do Check the port
*/
if (TCPPort.Checked)
{
// TCP Port, Create a TCP socket, and form a connection to the Listen server
Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint remoteEP = new System.Net.IPEndPoint(ServerAddress, int.Parse(PortNumber.Text));
// Try and establish a connection to the remote system
try
{
sock.Connect(remoteEP);
Result.Text = "Connected!";
}
catch (SocketException Exep)
{
MessageBox.Show(Exep.Message);
}
// Send some data
try
{
String szData = "Test";
byte[] byData = System.Text.Encoding.ASCII.GetBytes(szData);
sock.Send(byData);
Result.Text = "Data Sent";
}
catch (SocketException Exep)
{
MessageBox.Show("Error Sending Data\n" + Exep.Message);
}
try
{
// Recieve some data.
Result.Text = "Listening";
byte[] buffer = new byte[1024];
int iRx = sock.Receive(buffer);
MessageBox.Show(buffer.ToString());
}
catch (SocketException Exep)
{
MessageBox.Show(Exep.Message);
}
}
else if (UDPPort.Checked)
{
// UDP Port, Create a UDP socket, and then do the UDP thingy.
Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment