|
-
Dec 31st, 2009, 04:41 PM
#1
Thread Starter
Hyperactive Member
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?
-
Dec 31st, 2009, 05:15 PM
#2
Member
Re: Error, multiple conections socket
My guess is that you should not return false in the catch block where you decrement your users counter.
-
Dec 31st, 2009, 10:03 PM
#3
Thread Starter
Hyperactive Member
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.
-
Dec 31st, 2009, 10:17 PM
#4
Thread Starter
Hyperactive Member
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.
-
Jan 1st, 2010, 11:39 AM
#5
Thread Starter
Hyperactive Member
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 !
-
Jan 1st, 2010, 12:13 PM
#6
Member
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.
-
Jan 1st, 2010, 12:59 PM
#7
Thread Starter
Hyperactive Member
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
-
Jan 1st, 2010, 02:26 PM
#8
Member
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.
-
Jan 1st, 2010, 02:28 PM
#9
Thread Starter
Hyperactive Member
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?
-
Jan 1st, 2010, 03:11 PM
#10
Member
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.
-
Jan 1st, 2010, 03:13 PM
#11
Thread Starter
Hyperactive Member
Re: Error, multiple conections socket
ok , i will try and then i post here the result.
-
Jan 1st, 2010, 03:44 PM
#12
Thread Starter
Hyperactive Member
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
-
Jan 1st, 2010, 04:01 PM
#13
Member
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--;
}
}
}
-
Jan 1st, 2010, 04:49 PM
#14
Thread Starter
Hyperactive Member
Re: Error, multiple conections socket
If i make like this, the prompt show error in line 88: 'use of unassigned local variable "user" '
-
Jan 13th, 2010, 09:21 AM
#15
Lively Member
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.
-
Jan 13th, 2010, 10:01 AM
#16
Thread Starter
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|