Hello,
I have created a multi-threaded Client-Server Console application.
But my client can not be only console application. It can be browser also.
I am posting my Server source code here:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using ServerData; using System.Net.Sockets; using System.IO; using System.Threading; using System.Net; namespace Server { class Server { static Socket listenerSocket; static List<ClientData> clients; static void Main(string[] args) { Console.WriteLine("Starting a Server:" + Packet.GetIp4Addresses()); listenerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); clients = new List<ClientData>(); //Create IP end point IPEndPoint ipe = new IPEndPoint(IPAddress.Parse(Packet.GetIp4Addresses()), 27015); //Bind the server socket to machine address listenerSocket.Bind(ipe); //Create listen thread and start Thread listenThread = new Thread(ListenThread); listenThread.Start(); } //Listener that listens every client connection static void ListenThread() { for (;;) { //Listen to client socket that is trying to connect to this Server listenerSocket.Listen(0); //Add connected client to list of Clients for broadcasting purpose clients.Add(new ClientData(listenerSocket.Accept())); } } //Run the client thead which handles every client individually public static void Data_IN(object cSocket) { Socket clientSocket = (Socket)cSocket; byte[] Buffer; int readByteSize; for (;;) { try { Buffer = new byte[clientSocket.SendBufferSize]; readByteSize = clientSocket.Receive(Buffer); if (readByteSize > 0) { //Handle Data Packet p = new Packet(Buffer); DataManager(p,clientSocket); } } catch (SocketException e) { Console.WriteLine("Client disconnected!!"); Console.ReadLine(); Environment.Exit(0); } } } public static void DataManager(Packet p,Socket clientSocket) { switch (p.packetType) { case Packet.PacketType.Chat: clientSocket.Send(p.toBytes()); break; } } } class ClientData { public Socket clientSocket; public Thread clientThread; String id; public ClientData() { id = Guid.NewGuid().ToString(); clientThread = new Thread(Server.Data_IN); clientThread.Start(clientSocket); SendRegistrationPacket(); } public ClientData(Socket clientSocket) { this.clientSocket = clientSocket; id = Guid.NewGuid().ToString(); clientThread = new Thread(Server.Data_IN); clientThread.Start(clientSocket); SendRegistrationPacket(); } public void SendRegistrationPacket() { Packet p = new Packet(Packet.PacketType.Registration, "server"); p.GData.Add(id); clientSocket.Send(p.toBytes()); } } }
Here are my questions:
1. I have only handled client request from console client application. I also have to handle it from Web page. How can I differentiate this request type?
2. For each client request, I have to maintain session by a cookie [session cookie]. For this, my server needs be a HTTP server? I really don't understand how my above Server implementation is different from HTTP server?
3. If my server implementation is correct, how can I create session using session cookie and send it back to client?
Thank you in advance!
Navnath