1
Answer

C# TCP LOOP HELP!!!

Joshua Garrett

Joshua Garrett

15y
3.8k
1
Hey guys, for some reason this stupid TCP server doesn't loop after a client disconnects from it. How can I have it continue to loop so when people do disconnect from it, it continues to accept connections. Here is the code.


using System;
using System.Text;
using System.Net;
using System.Net.Sockets;

public class serv
{
public static void Main()
{
int c = 1234;
try
{
IPAddress ipAd = IPAddress.Parse("69.10.61.145"); //use local m/c IP address, and use the same in the client

/* Initializes the Listener */
TcpListener myList = new TcpListener(ipAd, 7575);

/* Start Listeneting at the specified port */
myList.Start();

Console.WriteLine("Login Server is running.");
Console.WriteLine("Connection :" + myList.LocalEndpoint);
Console.WriteLine("Waiting for logins...");
Console.WriteLine("================================");

Socket s = myList.AcceptSocket();
Console.WriteLine("Connection accepted from: " + s.RemoteEndPoint);

byte[] b = new byte[100];
int k = s.Receive(b);



Console.WriteLine("Login Attempt Recieved from: " + s.RemoteEndPoint);
for (int i = 0; i < k; i++)
Console.Write(Convert.ToChar(b[i]));


ASCIIEncoding asen = new ASCIIEncoding();






s.Send(asen.GetBytes("Login is invalid." + s.RemoteEndPoint));
Console.WriteLine("\nAccess Denied.");
Console.ReadLine();
return;
/* clean up */
}



catch (Exception e)
{
Console.WriteLine("Error..... " + e.StackTrace);
Console.ReadLine();
}
}

}

Answers (1)