Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.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# 同时发送和接收文件_C#_Sockets - Fatal编程技术网

C# 同时发送和接收文件

C# 同时发送和接收文件,c#,sockets,C#,Sockets,我不熟悉套接字编程,所以我有一个问题。我正在尝试创建两个应用程序,一个服务器和一个客户端,它们必须通过internet相互发送和接收文件(发送和接收文件)。将只有一个服务器和一个客户机,所以我决定,我不需要异步套接字连接。 我尝试只通过一个套接字连接发送和接收文件,但没有成功。现在我尝试使用两个套接字,使用相同的IP和不同的端口在不同的线程中运行,但它不再工作。我必须在源代码中找到问题,还是必须使用异步套接字连接 这是我的服务器端应用程序。两个套接字的代码非常相似,只有 不同之处在于我使用的端口

我不熟悉套接字编程,所以我有一个问题。我正在尝试创建两个应用程序,一个服务器和一个客户端,它们必须通过internet相互发送和接收文件(发送和接收文件)。将只有一个服务器和一个客户机,所以我决定,我不需要异步套接字连接。 我尝试只通过一个套接字连接发送和接收文件,但没有成功。现在我尝试使用两个套接字,使用相同的IP和不同的端口在不同的线程中运行,但它不再工作。我必须在源代码中找到问题,还是必须使用异步套接字连接

这是我的服务器端应用程序。两个套接字的代码非常相似,只有 不同之处在于我使用的端口

// My main thread starts another one, for the receiving of files
    public static void StartListening() {
        t = new Thread(receiveFile);
        t.Start();
        sendFile();
    }

//

        static void sendFile()
        {
            IPAddress ipAddress = IPAddress.Parse("192.168.1.101");
            IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 10999);
            Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream,   ProtocolType.Tcp);
            try
            {
                listener.Bind(localEndPoint);
                listener.Listen(10);
                while (true)
                {
                    handler = listener.Accept();
                    //code after connecting for sending files
                    while(true) {
                       //...
                    }
                }
            }
            catch(Exception e) {}
         }


    static void receiveFile()
    {
        IPAddress ipAddress = IPAddress.Parse("192.168.1.101");
        IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 11000);
        Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        try
        {
            listener.Bind(localEndPoint);
            listener.Listen(10);
            while(true) {
                handler = listener.Accept();
                //code after connecting for receiving files
                while(true) {
                    //... 
                }
            }
        }
        catch(Exception e) {}
     }
这就是我客户端应用程序中连接到套接字的代码。 同样,唯一的区别在于插座:

//Again i start a second thread, for the second socket
public static void StartClient() {
    t = new Thread(receiveFile);
    t.Start();
    sendFile();
}

static void sendFile()
{
    try
    {
        IPAddress ipAddress = IPAddress.Parse("95.110.62.74");
        IPEndPoint remoteEP = new IPEndPoint(ipAddress, 11000);
        sender = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        try
        {
            sender.Connect(remoteEP);
            //Code  for sending to the server after connecting
            while (true) { }
        }
        catch(Exception e) {}
    }
    catch(Exception e) { }
}

我在服务器和客户端应用程序中使用不同的IP,因为我的服务器位于路由器后面,我告诉路由器将连接转发到两个POTR。当我启动两个程序时,它们只通过其中一个套接字连接,每次都是不同的,我的意思是,有时它们连接到端口10999上的一个,有时连接到另一个,但从不同时连接两个

我不确定,如果我把一切都解释清楚:)


提前感谢

你说的“不起作用”是什么意思?您遇到了什么错误?我认为出于您的目的,同步套接字应该可以正常工作。请给我们完整的复制步骤,一些关于程序在哪里运行的信息(都在本地主机上,同一网络上的两台不同的计算机,不同的网络),所有错误,所有异常堆栈跟踪,一些代码,等等“我制作了一个程序,但它不起作用”绝对没有提示实际问题是什么。请编辑您的问题以提供此详细信息。话虽如此,我打赌您被Windows防火墙或路由器的防火墙阻止。或者您可能必须以管理员身份运行程序……请提供日志(嗅探它)正在发送/接收什么?可能路由器不允许您发送文件,或者需要某种代理或身份验证。请给我们一个日志以查看它:)
static void receiveFile()
{
    try
    {
        IPAddress ipAddress = IPAddress.Parse("95.110.62.74");
        IPEndPoint remoteEP = new IPEndPoint(ipAddress, 10999);
        sender = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        try
        {
            sender.Connect(remoteEP);
            //Code  for sending to the server after connecting
            while (true) { }
        }
        catch(Exception e) {}
    }
    catch(Exception e) { }
}