Results 1 to 16 of 16

Thread: Error, multiple conections socket

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2008
    Posts
    256

    Error, multiple conections socket

    I have this class i made with you help:

    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;
                }
            Console.ReadKey();
            }
    
        }
    }
    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 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;
            }
        }
    }
    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.

    Where is the error?






  2. #2
    Member
    Join Date
    Dec 2009
    Posts
    34

    Re: Error, multiple conections socket

    My guess is that you should not return false in the catch block where you decrement your users counter.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2008
    Posts
    256

    Re: Error, multiple conections socket

    Its worked, but, when i start server.exe, and run: client.exe it show, Client 1 connected, ip: 127.0.0.1. But if I open another client.exe nothing happend, and when i close the first client.exe it shows: client 1 disconnected, and suddenly appears: client 1 connected!

    Was supposed to appear:
    Client 1 connected
    Client 2 connected
    Received message "something" from Client 1
    Client 2 Disconnected
    Client 1 disconnected

    Its just an example, but its not working >.<
    Last edited by lelejau; Dec 31st, 2009 at 10:07 PM.






  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2008
    Posts
    256

    Re: Error, multiple conections socket

    And now the server.exe its not reading the message i send :S


    @edit
    wll, the error with not receiving the message i already fix it, but the error i said still remains ;s
    Last edited by lelejau; Jan 1st, 2010 at 11:18 AM.






  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2008
    Posts
    256

    Re: Error, multiple conections socket

    Wel, the error was in Soquete.Listen(1). Error fixed. But now, when i do:
    Code:
                        sock.Send(Encoding.ASCII.GetBytes("testing"));
                        Console.Write("Send testing\n");
                        Thread.Sleep(3000);
                        sock.Send(Encoding.ASCII.GetBytes("END"));
                        Console.Write("Send END\n");
    The message END is not received !






  6. #6
    Member
    Join Date
    Dec 2009
    Posts
    34

    Re: Error, multiple conections socket

    I believe that TcpListener.AcceptTcpClient is a blocking method. I bet if you connect another client you will get the END message.

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2008
    Posts
    256

    Re: Error, multiple conections socket

    I dont understand you answer, i want to send several messages to server and the server must to receive all






  8. #8
    Member
    Join Date
    Dec 2009
    Posts
    34

    Re: Error, multiple conections socket

    In other words, the server will execute the first receive ("testing") and then it will go back to the top of the loop and execute:
    ab = tcpl.AcceptTcpClient();

    this will cause the server to wait until another connection is requested by a client. i.e. it blocks the execution of your program until it returns, but it won't return until a client requests a connection. You are probably going to need to asynchronously handle the connection logic.

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2008
    Posts
    256

    Re: Error, multiple conections socket

    i dont know whats is this, can you put this "asynchronously handl" into my loop, so I can understand what it is?






  10. #10
    Member
    Join Date
    Dec 2009
    Posts
    34

    Re: Error, multiple conections socket

    try adding this:

    Code:
    if(tcpl.Pending())
    {
        ab = tcpl.AcceptTcpClient();
    }
    This is not asynchronous but it should fix your problem.

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2008
    Posts
    256

    Re: Error, multiple conections socket

    ok , i will try and then i post here the result.






  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2008
    Posts
    256

    Re: Error, multiple conections socket

    Returns error:

    br:
    Referencia de objeto nao definida para uma instancia de objeto
    eng:
    Object reference not set to an instance of object






  13. #13
    Member
    Join Date
    Dec 2009
    Posts
    34

    Re: Error, multiple conections socket

    Does your loop look like this?

    Code:
    while (true)
    {
                            
          if(tcpl.Pending())
          {
                   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--;
                                }
                        }
                    }

  14. #14

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2008
    Posts
    256

    Re: Error, multiple conections socket

    If i make like this, the prompt show error in line 88: 'use of unassigned local variable "user" '






  15. #15
    Lively Member reado's Avatar
    Join Date
    Mar 2005
    Posts
    78

    Re: Error, multiple conections socket

    I dont know if this will help you but if the client initiates the disconnect, the server will receive a no byte response (rather than a response with data in), this tells you that the client has disconnected. This should then prompt your code to close down that socket in the server and destroy it.

  16. #16

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2008
    Posts
    256

    Re: Error, multiple conections socket

    i dont understand what i can do to solve this problem






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