how create multiple clients with is code
program.cs
class Program
{
static void Main(string[] args)
{
Server.ServerCode(5656);
}
}
Client.cs
class Client
{
public static void ClientCode()
{
//SOCKET CONNECTION
//int Port = (int)port;
Console.WriteLine("......................CLIENT.........................");
TcpClient client = new TcpClient("127.0.01", 5656);
NetworkStream clientStream = client.GetStream();
StreamReader sr = new StreamReader(clientStream);
StreamWriter sw = new StreamWriter(clientStream);
sw.AutoFlush = true;
//CLOSING SOCKET
clientStream.Close();
client.Close();
Console.ReadLine();
}
}
my server code:
sereve.cs
class Server
{
public static void ServerCode(object port)
{
//SOCKET CONNECTION
int Port = (int)port;
Console.WriteLine("................SERVER RUNNING......................");
TcpListener listener = new TcpListener(IPAddress.Parse("127.0.0.1"), Port);
listener.Start();
while (true)
{
TcpClient serverSite = listener.AcceptTcpClient();
if (serverSite.Connected)
{
Thread c = new Thread((fileTransfer));
c.Start(serverSite);
}
}
}
public static void fileTransfer(object clients)
{
Console.WriteLine("new client");*/
TcpClient server = listener.AcceptTcpClient();//(TcpClient)clients;
NetworkStream serverStream = server.GetStream();
StreamReader sr = new StreamReader(serverStream);
StreamWriter sw = new StreamWriter(serverStream);
sw.AutoFlush = true;
//CHECKING USERNAME AND PASSWORD
Console.WriteLine("Connecting to Client... ");
string fromClient_u = sr.ReadLine();
Console.WriteLine("\n Username: " + fromClient_u);
string fromClient_p = sr.ReadLine();
Console.WriteLine("\n Password: *******");
if (fromClient_u == "Client1" && fromClient_p == "123456")
{
Console.WriteLine("\nClient has Successfully Loged In. ");
sw.WriteLine("Connection Complete... "); //msg1
sw.WriteLine("Successfully Connected to the Server."); //msg2
sw.WriteLine("Enter Some Informations for File Transfer..."); //msg3
}
//CLOSING SOCKET
serverStream.Close();
server.Close();
Console.ReadLine();
}
}
its not working. I want the first thread to work and end, then the 2nd thread to start. but this code generates all the three threat at once. my point is , multiple client will be created by this code and they will wait. when the 1st client is done working with the server then the 2nd client will get server connection.
As simple as it may sound, if you want to lock the process until the client ends and then starts the next one, just loop and call it explicitly: