I have this code for server:
This code for client: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 ConsoleApplication2 { class User { public TcpClient conn = null; public string name = "Anônimo"; public string ip; public NetworkStream ns; public StreamWriter w; public StreamReader r; public int tent = 0; public int win = 0; public User(TcpClient conn) { this.conn = conn; this.ns = this.conn.GetStream(); this.r = new StreamReader(this.ns); this.w = new StreamWriter(this.ns); } public string Name { get { return this.name; } set { this.name = value; } } } class Program { private static Socket Soquete; private static TcpListener tcpl = new TcpListener(IPAddress.Parse(IPAddress.Loopback.ToString()), 10009); static TcpClient a; public static bool Iniciar() { Int32 porta = 10009; User user; try { Soquete = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); Soquete.Bind(new IPEndPoint(IPAddress.Any, porta)); Soquete.Listen(1); Console.WriteLine("Server online at por " + porta + ", IP:" + IPAddress.Loopback.ToString() + "."); try { Int32 usuarios = 0; tcpl.Start(); Console.WriteLine("Waiting for connections on port "+ porta +"."); while (true) { a = tcpl.AcceptTcpClient(); user = new User(a); usuarios++; user.ip = ((IPEndPoint)a.Client.RemoteEndPoint).Address.ToString(); //Console.WriteLine(String.Format("Client {0} connected -> IP: {1}",usuarios, user.ip)); try { user.r.ReadLine(); } catch(Exception e) { Console.WriteLine("Error while reading data"); Console.WriteLine(e.ToString()); } } } catch (Exception ex) { Console.WriteLine(ex.Message); } return true; } catch (Exception ex) { Console.WriteLine(ex.Message); return false; } } static void Main(string[] args) { Iniciar(); Console.ReadKey(); } } }
But when i run client to send text, server returns error! The error is in PT-BR, so i wont put it here, because , if I translate it, it will be not so right.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 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; } } } }
Anyone can help? I dont know what means this error!








Reply With Quote