Results 1 to 4 of 4

Thread: [RESOLVED] Error receiving text by socket

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2008
    Posts
    256

    Resolved [RESOLVED] Error receiving text by socket

    I have this code for server:
    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();
            }
        }
    }
    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 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;
                } 
            }
    
        }
    }
    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.

    Anyone can help? I dont know what means this error!






  2. #2
    Member
    Join Date
    Dec 2009
    Posts
    34

    Re: Error receiving text by socket

    This is just a quick guess (I haven't done any real socket programming in a while and I can't write any test code right now) but I think what you want to do is:


    Code:
     
         try
        {
             byte[] buffer = new byte[256];
             int bytesReceived = user.ns.read(buffer, 0, buffer.length);
             string message;
             message =System.Text.Encoding.ASCII.GetString(buffer , 0, bytesReceived);
              Console.WriteLine("Server received: " + message + " from client");
             //user.r.ReadLine();
         }
         catch(Exception e)
         {
              Console.WriteLine("Error while reading data");
              Console.WriteLine(e.ToString());
         }

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2008
    Posts
    256

    Re: Error receiving text by socket

    I should paste this into server project?






  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2008
    Posts
    256

    Re: Error receiving text by socket

    Thanks man!!!!! It worked!!!! Reeeeeaaaallyyy thanks!!!






Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width