V
Virtualgamer22
Guest
Hello Everyone,
I am working on a messaging client with some friends as an interesting project for spring break.
As I'm working on the project I've found that the server responds locally on my computer but when I open it to the internet through port 65500, it errors out with "System.Net.Sockets.SocketException: 'An attempt was made to access a socket in a way forbidden by its access permissions" and I have no idea how to fix this issue.
My client code:
public static String RecentMessage = null;
static void Main(string[] args)
{
TcpClient client = new TcpClient();
client.Connect("xxx.xxx.xxx.xxx", 65500);
Console.WriteLine("Client Connected!");
NetworkStream ns = client.GetStream();
Thread thread = new Thread(o => ReceiveData((TcpClient)o));
thread.Start(client);
while (!string.IsNullOrEmpty(RecentMessage = Console.ReadLine()))
{
byte[] buffer = Encoding.ASCII.GetBytes(RecentMessage);
ns.Write(buffer, 0, buffer.Length);
}
client.Client.Shutdown(SocketShutdown.Send);
thread.Join();
ns.Close();
client.Close();
Console.WriteLine("Disconnected From Server");
Console.ReadKey();
}
static void ReceiveData(TcpClient client)
{
NetworkStream ns = client.GetStream();
byte[] receivedBytes = new byte[1024];
int byte_count;
while ((byte_count = ns.Read(receivedBytes, 0, receivedBytes.Length)) > 0)
{
String Recieved = Encoding.ASCII.GetString(receivedBytes, 0, byte_count).TrimEnd();
if (!RecentMessage.Contains(Recieved))
{
Console.Write(">> " + Recieved);
}
}
}
My Server Code:
class Program
{
public static List<Client> Clients = new List<Client>();
public static List<Chat> Chats = new List<Chat>();
static void Main(string[] args)
{
int counter = 1;
TcpListener ServerConnection = new TcpListener(IPAddress.Any, 65500);
ServerConnection.Start();
while (true)
{
TcpClient client = ServerConnection.AcceptTcpClient();
Clients.Add(new Client(counter.ToString(), client));
counter++;
}
}
}
class Client
{
public String ID { get; set; }
public String Username { get; set; }
public TcpClient Connection {get; set;}
public List<Chat> Chats { get; set; }
public Client(String id, TcpClient Connect)
{
ID = id;
Connection = Connect;
Chats = new List<Chat>();
Thread Start = new Thread(new ThreadStart(ClientControl));
Start.Start();
}
public void ClientControl()
{
ClientManager.ManageClient(this);
}
}
class ClientManager
{
public static void ManageClient(Client C)
{
while (true)
{
try
{
NetworkStream stream = C.Connection.GetStream();
byte[] buffer = new byte[2048];
int byte_count = stream.Read(buffer, 0, buffer.Length);
if (byte_count == 0)
{
break;
}
string data = Encoding.ASCII.GetString(buffer, 0, byte_count);
broadcast(data);
Console.WriteLine(data);
}
catch
{
C.Connection.Close();
Program.Clients.Remove(C);
}
}
}
public static void broadcast(string data)
{
byte[] buffer = Encoding.ASCII.GetBytes(data + Environment.NewLine);
foreach (Client C in Program.Clients)
{
NetworkStream Stream = C.Connection.GetStream();
Stream.Write(buffer, 0, buffer.Length);
}
}
}
Please let me know what I've done wrong and what I can do to fix it.
Also, I've port forwarded the port on the router and the server has the firewall rules needed to accept the connection.
Thanks in advance for the responses,
Nate
Continue reading...
I am working on a messaging client with some friends as an interesting project for spring break.
As I'm working on the project I've found that the server responds locally on my computer but when I open it to the internet through port 65500, it errors out with "System.Net.Sockets.SocketException: 'An attempt was made to access a socket in a way forbidden by its access permissions" and I have no idea how to fix this issue.
My client code:
public static String RecentMessage = null;
static void Main(string[] args)
{
TcpClient client = new TcpClient();
client.Connect("xxx.xxx.xxx.xxx", 65500);
Console.WriteLine("Client Connected!");
NetworkStream ns = client.GetStream();
Thread thread = new Thread(o => ReceiveData((TcpClient)o));
thread.Start(client);
while (!string.IsNullOrEmpty(RecentMessage = Console.ReadLine()))
{
byte[] buffer = Encoding.ASCII.GetBytes(RecentMessage);
ns.Write(buffer, 0, buffer.Length);
}
client.Client.Shutdown(SocketShutdown.Send);
thread.Join();
ns.Close();
client.Close();
Console.WriteLine("Disconnected From Server");
Console.ReadKey();
}
static void ReceiveData(TcpClient client)
{
NetworkStream ns = client.GetStream();
byte[] receivedBytes = new byte[1024];
int byte_count;
while ((byte_count = ns.Read(receivedBytes, 0, receivedBytes.Length)) > 0)
{
String Recieved = Encoding.ASCII.GetString(receivedBytes, 0, byte_count).TrimEnd();
if (!RecentMessage.Contains(Recieved))
{
Console.Write(">> " + Recieved);
}
}
}
My Server Code:
class Program
{
public static List<Client> Clients = new List<Client>();
public static List<Chat> Chats = new List<Chat>();
static void Main(string[] args)
{
int counter = 1;
TcpListener ServerConnection = new TcpListener(IPAddress.Any, 65500);
ServerConnection.Start();
while (true)
{
TcpClient client = ServerConnection.AcceptTcpClient();
Clients.Add(new Client(counter.ToString(), client));
counter++;
}
}
}
class Client
{
public String ID { get; set; }
public String Username { get; set; }
public TcpClient Connection {get; set;}
public List<Chat> Chats { get; set; }
public Client(String id, TcpClient Connect)
{
ID = id;
Connection = Connect;
Chats = new List<Chat>();
Thread Start = new Thread(new ThreadStart(ClientControl));
Start.Start();
}
public void ClientControl()
{
ClientManager.ManageClient(this);
}
}
class ClientManager
{
public static void ManageClient(Client C)
{
while (true)
{
try
{
NetworkStream stream = C.Connection.GetStream();
byte[] buffer = new byte[2048];
int byte_count = stream.Read(buffer, 0, buffer.Length);
if (byte_count == 0)
{
break;
}
string data = Encoding.ASCII.GetString(buffer, 0, byte_count);
broadcast(data);
Console.WriteLine(data);
}
catch
{
C.Connection.Close();
Program.Clients.Remove(C);
}
}
}
public static void broadcast(string data)
{
byte[] buffer = Encoding.ASCII.GetBytes(data + Environment.NewLine);
foreach (Client C in Program.Clients)
{
NetworkStream Stream = C.Connection.GetStream();
Stream.Write(buffer, 0, buffer.Length);
}
}
}
Please let me know what I've done wrong and what I can do to fix it.
Also, I've port forwarded the port on the router and the server has the firewall rules needed to accept the connection.
Thanks in advance for the responses,
Nate
Continue reading...