heizung124
New member
- Joined
- Nov 14, 2016
- Messages
- 1
- Programming Experience
- 1-3
Hello,
I am "working" on a Project wich is an Client/multi threaded Server i started by using an example Code. Everything is working fine so far but there is one thing I've been thinking about it for some time. I have an Client Class on my Server where I accept my connections handle the incoming bytes and send data back.
Currently I have no idea on how I could send data from another class and iam curious on how this would work.
here is my Client Class
As You can see i kinda Worked Around the problem by calling Functions to do stuff but i always need to return the data because I don't know how i could call the Class to Send Data to an specific Client.
I guess it is an easy task i just dont know how :crushed:
Or is the way the way of Handeling just bd like it is right now?
I am "working" on a Project wich is an Client/multi threaded Server i started by using an example Code. Everything is working fine so far but there is one thing I've been thinking about it for some time. I have an Client Class on my Server where I accept my connections handle the incoming bytes and send data back.
Currently I have no idea on how I could send data from another class and iam curious on how this would work.
here is my Client Class
C#:
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
using Data;
using Server.Packets;
namespace Server
{
public class Client : IDisposable
{
private int _id;
private string _guid;
public string Name;
public void Received(Packet p)
{
switch (p.Type)
{
case PacketType.Login:
Login(p.User);
break;
case PacketType.Disconnect:
CloseConnection();
break;
case PacketType.Register:
Register(p.User);
break;
case PacketType.Information:
InformationReceived(p);
break;
case PacketType.SingleChat:
// Reserved
break;
case PacketType.MultiChat:
MultiChat(p);
break;
case PacketType.Ping:
Ping(p);
break;
case PacketType.AccountCheck:
//AccountExist();
break;
default:
break;
}
}
private void Ping(Packet p)
{
var pack = Handler.PingDo(p);
clientSocket.Send(pack.ToBytes());
}
#region Login
public struct LoginResult
{
public bool succes;
public Packet mchat;
public Packet cssend;
}
public void Login(User u)
{
var Clientip = clientSocket.RemoteEndPoint;
var LResult = Handler.LoginDo(u, _guid, Clientip, _id);
if (LResult.succes)
{
MultiChatWithoutMe(LResult.mchat);
clientSocket.Send(LResult.cssend.ToBytes());
}
else
{
clientSocket.Send(LResult.cssend.ToBytes());
}
}
#endregion Login
#region Register
private void Register(User user)
{
var res = Handler.RegisterDo(user);
clientSocket.Send(res.ToBytes());
}
#endregion Register
#region Server
public Socket clientSocket;
private Thread thread;
public Client(int id, Socket socket)
{
this._id = id;
this._guid = Guid.NewGuid().ToString();
this.clientSocket = socket;
}
public Thread SetThread
{
set
{
this.thread = value;
}
}
public int Id
{
get
{
return this._id;
}
}
public void Start(object o)
{
Receive();
}
public void Receive()
{
byte[] buffer;
int readBytes;
while (clientSocket != null && clientSocket.Connected)
{
try
{
buffer = new byte[clientSocket.SendBufferSize];
readBytes = clientSocket.Receive(buffer);
if (readBytes > 0)
{
Packet p = new Packet(buffer);
if (p.Type != PacketType.Disconnect)
{
new Task(() => Received(p)).Start();
}
else
{
CloseConnection();
}
}
}
catch (SocketException e)
{
Console.WriteLine(e);
CloseConnection();
}
}
}
public static void MultiChat(Packet p)
{
Console.WriteLine(p.Message);
foreach (Client c in ServerMain.clients)
{
c.clientSocket.Send(p.ToBytes());
}
}
//List<Client> ClientList = new List<Client>();
public static void SendToSocket(int id)
{
List<Client> ClientList = new List<Client>();
List<Client> newid = ClientList.FindAll(x => x._id == id);
Array tst = newid.ToArray();
foreach (string st in tst)
{
Console.WriteLine(newid.ToString());
}
}
public void MultiChatWithoutMe(Packet p)
{
foreach (Client c in ServerMain.clients)
{
if (c != this)
{
c.clientSocket.Send(p.ToBytes());
}
}
}
private void InformationReceived(Packet p)
{
if (p.Information.Type == InformationType.Writing)
{
MultiChatWithoutMe(p);
}
}
private void CloseConnection()
{
Console.WriteLine("[{0}] User {1} disconnected!", _id, Name);
Packet p = new Packet(PacketType.Information);
p.Information = new Information(InformationType.Leaving, Name, "disconnected!");
MultiChatWithoutMe(p);
Disconnect();
}
private void Disconnect()
{
Dispose();
}
public void Dispose()
{
clientSocket.Close();
clientSocket.Dispose();
clientSocket = null;
ServerMain.clients.Remove(this);
this.thread.Abort();
}
#endregion Server
}
}
As You can see i kinda Worked Around the problem by calling Functions to do stuff but i always need to return the data because I don't know how i could call the Class to Send Data to an specific Client.
I guess it is an easy task i just dont know how :crushed:
Or is the way the way of Handeling just bd like it is right now?
Last edited: