Created
February 19, 2011 18:42
-
-
Save LeoAdamek/835259 to your computer and use it in GitHub Desktop.
Client for checking open ports
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.ComponentModel; | |
using System.Data; | |
using System.Drawing; | |
using System.Diagnostics; | |
using System.Linq; | |
using System.Net; | |
using System.Net.Sockets; | |
using System.Text; | |
using System.Windows.Forms; | |
using System.Threading; | |
namespace PortChecker | |
{ | |
public partial class MainWindw : Form | |
{ | |
System.Net.IPAddress ServerAddress = System.Net.IPAddress.Parse("178.32.186.41"); | |
int requestPort = 8080; | |
Socket checkSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); | |
public MainWindw() | |
{ | |
InitializeComponent(); | |
} | |
private void GoCheck_Click(object sender, EventArgs e) | |
{ | |
int scanPort = (int)PortNumberUD.Value; | |
/* | |
* Do Check the port | |
*/ | |
if (TCPPort.Checked) | |
{ | |
// Make the Request to the Checking server | |
MakeRequest("TCP", scanPort); | |
// TCP Port, Create a TCP socket, and form a connection to the Listen server | |
TCPPortCheck(scanPort); | |
} | |
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); | |
MessageBox.Show("This is not implimented yet."); | |
} | |
} | |
public void TCPPortCheck(int portNumber) | |
{ | |
// Socket checkSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); | |
IPEndPoint remoteEP = new System.Net.IPEndPoint(ServerAddress, portNumber); | |
// Try to bind and listen to the socket. | |
try | |
{ | |
IPAddress localIP = (Dns.Resolve(IPAddress.Any.ToString()).AddressList[0]); | |
IPEndPoint localEP = new IPEndPoint(localIP, portNumber); | |
checkSock.Bind(localEP); | |
checkSock.Listen(5); | |
} | |
catch (SocketException sockExep) { MessageBox.Show("Error:\n\n" + sockExep.Message); | |
} | |
Thread ListenThread = new Thread(ListenLoop); | |
ListenThread.Start(); | |
} | |
private void ListenLoop() | |
{ | |
int checkiRx = 0; | |
Stopwatch checkWatch = Stopwatch.StartNew(); | |
while (checkiRx == 0) | |
{ | |
// Try to recieve some data. To check the port is working | |
Socket handlerSock = checkSock.Accept(); | |
byte[] checkbuffer = new byte[1024]; | |
checkiRx = handlerSock.Receive(checkbuffer); | |
if (checkiRx == 22) | |
{ | |
MessageBox.Show("Message Recieved:\n" + checkiRx.ToString()); | |
Result.Text = "Port Open"; | |
} | |
else if (checkWatch.ElapsedMilliseconds > 5000) | |
{ | |
Result.Text = "Port Close"; | |
break; | |
} | |
} | |
} | |
private void MakeRequest(string portType, int portNumber) | |
{ | |
Socket requestSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); | |
IPEndPoint requestEP = new System.Net.IPEndPoint(ServerAddress, requestPort); | |
// Establish connection | |
try | |
{ | |
requestSock.Connect(requestEP); | |
Result.Text = "Making Request"; | |
} | |
catch (SocketException sockExep) | |
{ | |
MessageBox.Show(sockExep.Message); | |
} | |
// Make the request | |
try | |
{ | |
string requestData = portType + "," + portNumber; | |
byte[] byData = System.Text.Encoding.ASCII.GetBytes(requestData); | |
requestSock.Send(byData); | |
Result.Text = "Request Made"; | |
} | |
catch (SocketException sockExep) | |
{ | |
MessageBox.Show("Error:\n\n" + sockExep.Message); | |
} | |
} | |
private void MainWindw_Load(object sender, EventArgs e) | |
{ | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment