I am making a chat service for a game,
I am using a TCP listener an client for the account information, some sort of login service. I'm wondering if i can keep the socked the client connected to the server with, to check if he is still online, and keep sending him messages if he has new messages.
I already tried making a list of sockets for the login queue, but it disconnected the previous socket to to server as soon as i accepted a new socket.
byte[] usernameByte = new byte[100]; int usernameRecieved = s.Receive(usernameByte); //guiController.setText(System.DateTime.Now + " Recieved Login..."); byte[] passByte = new byte[100]; int passRecieved = s.Receive(passByte); //guiController.setText(System.DateTime.Now + " Recieved Password..."); string username = ""; string password = ""; for (int i = 0; i < usernameRecieved; i++) username += (Convert.ToChar(usernameByte[i])); for (int i = 0; i < passRecieved; i++) password += (Convert.ToChar(passByte[i])); if (DomainController.getInstance().checkAccount(username, password)) { ASCIIEncoding asen = new ASCIIEncoding(); s.Send(asen.GetBytes("true")); s.Send(asen.GetBytes("U are succesfully logged in, press enter to continue")); guiController.setText(serverName,System.DateTime.Now+""); guiController.setText(serverName, "Sent Acknowledgement - Logged in"); } else { ASCIIEncoding asen = new ASCIIEncoding(); s.Send(asen.GetBytes("false")); s.Send(asen.GetBytes("U are NOT logged in, press enter to continue")); guiController.setText(serverName, System.DateTime.Now + ""); guiController.setText(serverName, "\nSent Acknowledgement - Not logged in"); } This is the code i currently use to check the account information the user send me. Right after i send this the user dropd the connection and i move on to the next one.
I have tried making 1 list of seperate sockets and processing them one by one, but that failed because the previous socket's connection dropped, even tho it were 2 different machines that tried to connect.
Does anyone have a sollution / a way to save sockets, that I can use to make the program keep all the connections alive? so i can send a message from user 1 to user 2, and just use the socket they connected with? or do i need to add an id every time they make a connection?
EDIT
The client Code: (this is just a test client)
while (true) { TcpClient tcpclnt = new TcpClient(); Console.WriteLine("Connecting....."); tcpclnt.Connect("xx.xxx.xxx.xx", 26862); // use the ipaddress as in the server program while(!(checkResponse(tcpclnt.GetStream()))) { Thread.Sleep(1000); } Console.WriteLine("Connected"); Console.Write("Enter the string to be transmitted : "); String str = Console.ReadLine(); if (str == "") { str = " "; } Stream stm = tcpclnt.GetStream(); ASCIIEncoding asen = new ASCIIEncoding(); byte[] ba = asen.GetBytes(str); Console.WriteLine("Transmitting....."); stm.Write(ba, 0, ba.Length); Console.Write("Enter the string to be transmitted : "); String str2 = Console.ReadLine(); if (str2 == "") { str2 = " "; } Stream stm2 = tcpclnt.GetStream(); ASCIIEncoding asen2 = new ASCIIEncoding(); byte[] ba2 = asen2.GetBytes(str2); Console.WriteLine("Transmitting....."); stm.Write(ba2, 0, ba2.Length); if (str == "false") { blijvenWerken = false; } byte[] bb = new byte[100]; int k = stm.Read(bb, 0, 100); for (int i = 0; i < k; i++) Console.Write(Convert.ToChar(bb[i])); byte[] bb2 = new byte[100]; int k2 = stm.Read(bb2, 0, 100); Console.Write("\n"); for (int i = 0; i < k2; i++) Console.Write(Convert.ToChar(bb2[i])); Console.WriteLine("\n"); tcpclnt.Close(); Thread.Sleep(1000); } Server getting the sockets: This bit of code is on the loginserver, its because i can only accept 1 socket every time to keep the connection alive, that i put queueCount on a maximum of 1. I want to be able to make a list of Sockets that i accepted to add to a User account.
while (loginServerOn) { if (queueCount < 1) { if (loginServer.getLoginListener().Pending()) { loginQueue.Add(loginServer.getSocket()); ASCIIEncoding asen = new ASCIIEncoding(); Socket s = loginQueue.First(); try { s.Send(asen.GetBytes("true")); queueCount++; } catch { loginQueue.Remove(s); } } } } The function that returns the accepted socket.
public Socket getSocket() { return myList.AcceptSocket(); } EDIT: Essence of the question
I want to add the socked or client recieved to my Account object, so every connection has an Account its linked to, when i want to send a message to a certain account, it should send a message to the socked or client bound to that account, can you help/show me how i can achieve this?