Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net;
- using System.Net.Sockets;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- namespace Server
- {
- class tcpConexion
- {
- private TcpClient client;
- private TcpListener listener;
- private Server s = new Server();
- private List<Tuple<TcpClient, int>> clientList = new List<Tuple<TcpClient, int>>();
- public void startServer()
- {
- new Thread(() => this.Listener()).Start();
- }
- private void Listener()
- {
- this.listener = new TcpListener(IPAddress.Any, 3030);
- this.listener.Start();
- while (true)
- {
- this.client = this.listener.AcceptTcpClient();
- handleClient.newClient(this.client);
- }
- }
- public void startReading(TcpClient client, int clientID)
- {
- this.clientList.Add(new Tuple<TcpClient, int>(client, clientID));
- this.s.Write(string.Format("{0} Client has connected!", clientID));
- s.updateConsoleUsers(this.clientList.Count);
- NetworkStream stream = client.GetStream();
- Byte[] buffer = new Byte[client.Available];
- while (client.Connected)
- {
- try
- {
- stream.Read(buffer, 0, buffer.Length);
- this.s.Write(string.Format("client: {0} has sent: {1}", clientID, Encoding.Default.GetString(buffer)));
- stream.Flush();
- }
- catch
- {
- this.clientList.Remove(new Tuple<TcpClient, int>(client, clientID));
- s.updateConsoleUsers(this.clientList.Count);
- stream.Flush();
- stream.Close();
- client.Close();
- this.s.Write(string.Format("{0} has disconnected", clientID));
- }
- }
- }
- public void handleData(int packetID, string body)
- {
- switch (packetID)
- {
- case 0:
- break;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement