Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#:如何向本地网络中的特定IP地址发送/接收字符串_C#_String_Network Programming_Ip - Fatal编程技术网

C#:如何向本地网络中的特定IP地址发送/接收字符串

C#:如何向本地网络中的特定IP地址发送/接收字符串,c#,string,network-programming,ip,C#,String,Network Programming,Ip,我想做一个像即时通讯器一样工作的程序,我已经完成了,但我不知道如何向特定的IP地址发送/接收字符串 我给自己写了一个方法,这个东西属于: //is called every second and when you commit a message public void Update(ref eStatus prgmStatus, ref eProfile userProfile) { UpdateUI(ref prgmStatus); [ Some other Update M

我想做一个像即时通讯器一样工作的程序,我已经完成了,但我不知道如何向特定的IP地址发送/接收字符串

我给自己写了一个方法,这个东西属于:

//is called every second and when you commit a message
public void Update(ref eStatus prgmStatus, ref eProfile userProfile)
{
    UpdateUI(ref prgmStatus);
    [ Some other Update Methods ]
    [ Catch the string and add it to userProfile.CurrentChatHistory]
}

public void EnterText(object sender, EventArgs e)
{
    _usrProfile.CurrentChatHistory.Add(chatBox.Text);
    [ send chatBox.Text to IP 192.168.0.10 (e.g.) ]
}
我想使用客户端到客户端的系统,而不运行任何额外的服务器软件


我可以使用什么系统名称空间和方法来实现这一点?

您需要查看system.Net名称空间

如果您正在进行点对点聊天,可能需要在没有中央服务器的情况下向多个IP地址发送消息,那么最好使用UDP

从您的评论中可以看出,您没有中央服务器,我建议您使用UDP,至少在最初,用于快速启动。这里是您的朋友,允许您将数据包发送到任何指定的网络地址

基本上,您可以创建一个新的UdpClient实例,将要绑定的已知端口号传递到构造函数中

然后,使用Receive方法读取该端口上的数据包


然后,您还可以在同一实例上使用Send方法将数据包发送到网络地址。

我不久前发布了另一个问题。如果您想使用.Net4.5的异步/等待功能,这里有一个简单的echoserver,可以帮助您开始:

void Main()
{
    CancellationTokenSource cts = new CancellationTokenSource();
    TcpListener listener = new TcpListener(IPAddress.Any,6666);
    try
    {
        listener.Start();
        AcceptClientsAsync(listener, cts.Token);
        Thread.Sleep(60000); //block here to hold open the server
    }
    finally
    {
        cts.Cancel();
        listener.Stop();
    }

    cts.Cancel();
}

async Task AcceptClientsAsync(TcpListener listener, CancellationToken ct)
{
    while(!ct.IsCancellationRequested)
    {
        TcpClient client = await listener.AcceptTcpClientAsync();
        EchoAsync(client, ct);
    }

}
async Task EchoAsync(TcpClient client, CancellationToken ct)
{
    var buf = new byte[4096];
    var stream = client.GetStream();
    while(!ct.IsCancellationRequested)
    {
        var amountRead = await stream.ReadAsync(buf, 0, buf.Length, ct);
        if(amountRead == 0) break; //end of stream.
        await stream.WriteAsync(buf, 0, amountRead, ct);
    }
}

必须使用类创建客户机/服务器体系结构


服务器可以是第三台计算机或chatter中的一台。如果选择第二个选项,开始聊天的第一个人必须在特定端口上运行侦听套接字,第二个人必须使用IP地址和端口连接到该端口。

Google上的第一个结果:Internet上有大量的C#网络教程。这没有多大用处,因为我的程序是客户端到客户端的messenger,没有任何服务器在这种情况下,每个客户端也必须是一个服务器…这在大多数情况下通常是有问题的,因为防火墙必须打开,路由器配置为转发端口(或者如果你非常大胆,由uPnP配置)等等。这里没有魔弹。你需要学会如何做到这一点。这里没有人会为你写这封信。看起来真的很复杂。。。我不理解这个片段--这就是为什么我问,我的应用程序是完整的,除了网络部分,因为我没有看到它,但你真的没有看到它。因此,你的问题实际上是“过于宽泛”,应该是封闭的。看来异步部分很复杂,这就是他为什么这么说的原因。