none
Handling sessions using session cookie RRS feed

  • Question

  • 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

    Tuesday, November 28, 2017 1:53 PM

All replies

  • Please post this question in the ASP.NET forums.

    Your application isn't responsible for session nor does it have direct access to it from a web app. The code you posted could be run on a web server as part of a web request but wouldn't continue running after which prevents it from receiving any socket requests after the fact. This is ignoring the fact that a web server would generally be locked down enough such that the socket call wouldn't work anyway. The code you wrote isn't really usable from a web app, in my opinion. 

    If you want something that can be called from a console and/or web app then you need to use an IPC mechanism. WCF and web APIs are possible solutions on Windows. But they are request focused so keeping a running socket isn't the ideal solution. It sounds like you might actually want something more like a windows service that can run indefinitely. But I wonder if you're actually solving the wrong problem given your code and the questions you have. Ask this in the ASP.NET forums and they can probably give you better guidance on what you'll need for a web app.


    Michael Taylor http://www.michaeltaylorp3.net

    Tuesday, November 28, 2017 3:03 PM
    Moderator
  • Hi,

    Thank you for posting here.

    If you want to use the WCF technology, you can post the relevant issue on WCF forum.

    Since your issue is not related to code issue, I will move the case to off-topic forum.

    Best Regards,

    Hart


    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, November 29, 2017 8:50 AM