I have this class i made with you help:
Client
ServerCode:using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net; using System.IO; using System.Net.Sockets; using System.Threading; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { IPAddress host = IPAddress.Parse("127.0.0.1"); IPEndPoint hostep = new IPEndPoint(host, 10009); Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); try { sock.Connect(hostep); } catch (SocketException e) { Console.WriteLine("Problem connecting to host"); Console.WriteLine(e.ToString()); sock.Close(); return; } try { // sock.Send(Encoding.ASCII.GetBytes("testing")); } catch (SocketException e) { Console.WriteLine("Problem sending data"); Console.WriteLine(e.ToString()); sock.Close(); return; } Console.ReadKey(); } } }
Its fine, when i connect: Client 1 connected... When i disconnect: Client 1 disconnected. Wel, when i try to reconect withou restarting server it does not show Client conected.Code:using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net; using System.IO; using System.Net.Sockets; using System.Threading; namespace ServerSocket { class Soquets { private static Socket Soquete; private static TcpListener tcpl = new TcpListener(IPAddress.Parse(IPAddress.Loopback.ToString()), 10009); public static string server_ip; public static Int32 porta; public static void Pause() { Console.ReadKey(); } public static bool Iniciar() { User user; try { Soquete = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); Soquete.Bind(new IPEndPoint(IPAddress.Parse(server_ip), porta)); Soquete.Listen(1); Console.WriteLine(String.Format("Server online at ip {0}.",server_ip)); try { TcpClient ab; Int32 usuarios = 0; tcpl.Start(); Console.WriteLine(String.Format("Waiting for connections on port {0}.",porta)); while (true) { ab = tcpl.AcceptTcpClient(); user = new User(ab); usuarios++; user.ip = ((IPEndPoint)ab.Client.RemoteEndPoint).Address.ToString(); Console.WriteLine(String.Format("Client {0} connected -> IP: {1}", usuarios, user.ip)); try { byte[] buffer = new byte[4096]; int bytesReceived = user.ns.Read(buffer, 0, buffer.Length); string message; message = System.Text.Encoding.ASCII.GetString(buffer, 0, bytesReceived); Console.WriteLine(String.Format("Received {0} from client {1}.", message, usuarios)); } catch (Exception) { Console.WriteLine(String.Format("Client {0} disconeccted.", usuarios)); usuarios--; return false; } } } catch (Exception ex) { Console.WriteLine(ex.Message); } } catch (Exception ex) { Console.WriteLine(ex.Message); } return true; } } }
Where is the error?








Reply With Quote