October 11th, 2010, 04:39 PM
#1
Networking Help
Hi, I am trying to create a simple remote desktop application but I need some help, Visual C# says "Only one usage of each socket address (protocol/network address/port) is normally permitted" on statement "this.tcpListener.Start();" when I turn the system on more than once. How could I fix that? Here's the code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Threading;
using System.Net;
using System.Runtime.InteropServices;
namespace NickRemoteDesktop
{
public partial class Main : Form
{
[DllImport("user32.dll")]
public static extern void LockWorkStation();
[DllImport("user32.dll")]
public static extern int ExitWindowsEx(int uFlags, int dwReason);
private bool on = false;
private const int password = -2006878815;
private TcpListener tcpListener;
private Thread listenThread;
public Main()
{
InitializeComponent();
Reset();
}
private void Start()
{
this.tcpListener = new TcpListener(IPAddress.Any, 8000);
this.listenThread = new Thread(new ThreadStart(ListenForClient));
this.listenThread.Start();
}
private void ListenForClient()
{
this.tcpListener.Start();
while (true)
{
TcpClient client = this.tcpListener.AcceptTcpClient();
Thread clientThread = new Thread(new ParameterizedThreadStart(HandleClientComm));
clientThread.Start(client);
}
}
private void HandleClientComm(object client)
{
TcpClient tcpClient = (TcpClient)client;
NetworkStream clientStream = tcpClient.GetStream();
byte[] message = new byte[4096];
int bytesRead;
while (true)
{
bytesRead = 0;
try
{
bytesRead = clientStream.Read(message, 0, 4096);
}
catch
{
break;
}
if (bytesRead == 0)
{
break;
}
ASCIIEncoding encoder = new ASCIIEncoding();
switch (encoder.GetString(message, 0, bytesRead))
{
case "-2006878815NLOCK":
LockWorkStation();
break;
case "-2006878815NLOGOUT":
ExitWindowsEx(0, 0);
break;
case "-2006878815NSHUTDOWN":
System.Diagnostics.Process.Start("Shutdown", "-s -t 10");
break;
case "-2006878815NGLOAT":
MessageBox.Show("Nick Rocks!");
break;
case "-2006878815NBLUESCREEN":
Form1 bluescreen = new Form1();
bluescreen.Show();
break;
default:
break;
}
}
tcpClient.Close();
}
private void Stop()
{
this.listenThread = null;
this.tcpListener = null;
}
private void Reset()
{
maskedTextBox1.Text = "";
if (this.on == true)
{
label2.ForeColor = Color.Green;
label2.Text = "ON";
button1.Text = "Turn Off";
Start();
}
else
{
label2.ForeColor = Color.Red;
label2.Text = "OFF";
button1.Text = "Turn On";
Stop();
}
}
private void SubmitKey()
{
if (on == true)
{
if (maskedTextBox1.Text.GetHashCode() == password) this.on = false;
}
else if (on == false)
{
if (maskedTextBox1.Text.GetHashCode() == password) this.on = true;
}
Reset();
}
private void maskedTextBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter) SubmitKey();
}
private void button1_Click(object sender, EventArgs e)
{
SubmitKey();
}
private void button3_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void button2_Click(object sender, EventArgs e)
{
Hide();
}
private void notifyIcon1_DoubleClick(object sender, EventArgs e)
{
Show();
}
}
}
Thanks.
October 13th, 2010, 02:09 AM
#2
Re: Networking Help
Please use code tags to make the code readable
[ code]your code here [ /code] (but then without the spaces)
Tags for this Thread
Posting Permissions
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
Forum Rules
Click Here to Expand Forum to Full Width
Bookmarks