2013-07-26 121 views
0

我是新来的C#网络和我有一些麻烦,使一个基本的客户端 - 服务器。服务器运行良好,但是当服务器启动并且客户端尝试连接时,会抛出错误“该套接字不能被绑定或连接”。一些额外的细节是服务器和客户端在禁用exclusiveAddressUse的同一台计算机上运行。TCP套接字 - InvalidOperationException

客户端代码:

this.client = new TcpClient(this.settings.GetByName("MasterServerIP").Value, int.Parse(this.settings.GetByName("MasterServerPort").Value)) { ExclusiveAddressUse = false }; 
this.client.Connect(IPAddress.Parse(this.settings.GetByName("MasterServerIP").Value), int.Parse(this.settings.GetByName("MasterServerPort").Value)); 
this.writer = new BinaryWriter(this.client.GetStream()); 
this.reader = new BinaryReader(this.client.GetStream()); 

Server代码:

this.Listener.Start(); 
TcpClient tcpClient; 
while (this.Listener.Server.Connected) 
{ 
tcpClient = this.Listener.AcceptTcpClient(); 
System.Threading.Thread t = new System.Threading.Thread(() => { this.ProcessClient(tcpClient); }); //Runs the thing on another thread so this can be accepting another client 
t.Start(); 
} 

编辑:即使在连接方法调用删除,它仍然抛出了同样的错误。任何帮助?

+1

请不要编辑您的问题以删除相关的细节。提高信息是可以的,但是要做出改变,使迄今为止所有答案无效,从而“打破”问答概念。 – paxdiablo

+0

对不起,我只是以为我会删除它,所以人们不再指出这一点(因为他们没有看到答案) – user1763295

+0

没关系,只是在问题的末尾添加一个编辑,看起来似乎是错误的没有第二个连接,并且错误在构造函数调用中。并给我们从socketexception实际的错误号码:-) – paxdiablo

回答

1

当您使用主机和端口创建TCPClient时,它会自动连接。您不需要再次连接。

有关详细信息,请参阅http://msdn.microsoft.com/en-us/library/System.Net.Sockets.TcpClient(v=vs.110).aspx

关于您的评论,当您删除connect调用时,它仍然失败,要么仍在运行其他代码,要么还有其他错误。下面的代码罚款运行:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Net.Sockets; 
namespace testtcp { 
    class Program { 
     static void Main(string[] args) { 
      TcpClient client = new TcpClient("www.google.com", 80); 
      //client.Connect("www.microsoft.com", 80); 
     } 
    } 
} 

但是,你取消注释Connect电话的瞬间,你会得到一个SocketException与expanatory文字:

{"A connect request was made on an already connected socket"} 

然而,这实际上是一个不同错误信息你得到的那个,导致我们相信上面的“有其他错误”的说法。

如果您检查TcpClient.ExclusiveAddressUsehere在线文档,你会发现下面的代码片段(我的粗体):

之前底层套接字绑定到客户端端口此属性必须设置。如果您拨打Connect,BeginConnect,TcpClient(IPEndPoint)TcpClient(String, Int32),客户端端口被绑定为该方法的副作用,并且您不能随后设置ExclusiveAddressUse属性。

事实上,下面的代码:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Net.Sockets; 
namespace testtcp { 
    class Program { 
     static void Main(string[] args) { 
      TcpClient client = new TcpClient("www.google.com", 80) { 
       ExclusiveAddressUse = false 
      }; 
     } 
    } 
} 

为您提供您所描述的确切例外。

因此,底线,如果你想使用ExclusiveAddressUse,不要试图与主机和端口的构造函数中使用它,因为这将结合插座和尝试的性质变化,然后将引发异常。相反,(作为一种可能性)使用无参数构造函数,更改属性,然后连接,然后

+0

删除connect方法调用后仍然抛出错误。 – user1763295

+1

@ user1763295我认为这是不正确的。我建议你没有运行更正的代码。 – EJP

+0

@EJP,事实证明它是真实的。该错误是构造函数选择(绑定的组合)以及试图更改绑定后无法更改的属性的组合。 – paxdiablo

1

的问题是在客户端代码的前两行:

this.client = new TcpClient(this.settings.GetByName("MasterServerIP").Value, int.Parse(this.settings.GetByName("MasterServerPort").Value)) { ExclusiveAddressUse = false }; 
this.client.Connect(IPAddress.Parse(this.settings.GetByName("MasterServerIP").Value), int.Parse(this.settings.GetByName("MasterServerPort").Value)); 

当你打电话的第一行的构造函数,你已经连接的TcpClient,所以连接呼叫事后无效。删除它,它会工作。

docs,构造:

初始化TcpClient类的新实例,并连接到指定端口指定主机上。

+0

我删除了连接,它仍然抛出相同的错误。 – user1763295

+0

呃,这很奇怪。它是否在服务器或客户端上抛出错误?并在哪一行? – Dirbaio

+0

它将它放在我定义的行上(this.client = new TcpClient等)。只是为了添加一些额外的信息,我没有为它设置一个值(私人TcpClient客户端;) – user1763295