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中通过套接字发送文件#_C#_Sockets_Tcp_Console - Fatal编程技术网

C# 如何在c中通过套接字发送文件#

C# 如何在c中通过套接字发送文件#,c#,sockets,tcp,console,C#,Sockets,Tcp,Console,我有服务器和客户端控制台应用程序,它们可以很好地通信,也可以发送一些字符串。这是代码 服务器 public static void Main() { try { IPAddress ipAd = IPAddress.Parse("127.0.0.1"); /* Initializes the Listener */ TcpListener myList = new TcpListe

我有服务器和客户端控制台应用程序,它们可以很好地通信,也可以发送一些字符串。这是代码

服务器

public static void Main()
    {
        try
        {
            IPAddress ipAd = IPAddress.Parse("127.0.0.1"); 

            /* Initializes the Listener */
            TcpListener myList = new TcpListener(ipAd, 1234);

            /* Start Listeneting at the specified port */
            myList.Start();

            Console.WriteLine("The server is running at port 8001...");
            Console.WriteLine("The local End point is  :" + myList.LocalEndpoint);
            Console.WriteLine("Waiting for a connection.....");

            Socket s = myList.AcceptSocket();
            Console.WriteLine("Connection accepted from " + s.RemoteEndPoint);

            byte[] b = new byte[100];
            int k = s.Receive(b);
            Console.WriteLine("Recieved...");
            for (int i = 0; i < k; i++)
                Console.Write(Convert.ToChar(b[i]));

            ASCIIEncoding asen = new ASCIIEncoding();
            s.Send(asen.GetBytes("The string was recieved by the server."));
            Console.WriteLine("\nSent Acknowledgement");
            /* clean up */
            s.Close();
            myList.Stop();

        }
        catch (Exception e)
        {
            Console.WriteLine("Error..... " + e.StackTrace);
        }
    }
publicstaticvoidmain()
{
尝试
{
IPAddress ipAd=IPAddress.Parse(“127.0.0.1”);
/*初始化侦听器*/
TcpListener myList=新的TcpListener(ipAd,1234);
/*在指定的端口开始侦听*/
myList.Start();
WriteLine(“服务器正在端口8001上运行…”);
WriteLine(“本地端点为:“+myList.LocalEndpoint”);
WriteLine(“等待连接…”);
套接字s=myList.AcceptSocket();
Console.WriteLine(“从“+s.RemoteEndPoint”接受连接);
字节[]b=新字节[100];
int k=s.Receive(b);
Console.WriteLine(“接收…”);
for(int i=0;i
客户

public static void Main()
    {
        try
        {
            TcpClient tcpclnt = new TcpClient();
            Console.WriteLine("Connecting...");

            tcpclnt.Connect("127.0.0.1", 1234);

            Console.WriteLine("Connected");
            Console.Write("Enter the string to be transmitted: ");

            String str = Console.ReadLine();
            Stream stm = tcpclnt.GetStream();

            ASCIIEncoding asen = new ASCIIEncoding();
            byte[] ba = asen.GetBytes(str);
            Console.WriteLine("Transmitting...");

            stm.Write(ba, 0, ba.Length);

            byte[] bb = new byte[100];
            int k = stm.Read(bb, 0, 100);

            for (int i = 0; i < k; i++)
                Console.Write(Convert.ToChar(bb[i]));

            tcpclnt.Close();
        }

        catch (Exception e)
        {
            Console.WriteLine("Error... " + e.StackTrace);
        }
    }
publicstaticvoidmain()
{
尝试
{
TcpClient tcpclnt=新的TcpClient();
控制台。写入线(“连接…”);
tcpclnt.Connect(“127.0.0.1”,1234);
控制台。写入线(“连接”);
Console.Write(“输入要传输的字符串:”);
String str=Console.ReadLine();
Stream stm=tcpclnt.GetStream();
ascienceoding asen=新的ascienceoding();
byte[]ba=asen.GetBytes(str);
控制台。写入线(“传输…”);
stm.Write(ba,0,ba.Length);
字节[]bb=新字节[100];
intk=stm.Read(bb,0100);
for(int i=0;i
现在,我需要添加代码算法,它将通过相同的应用程序发送文件。实现的关键是使用Socket。在客户端发送。我不确定文件的序列化和反序列化


任何暗示、建议、建议都更受欢迎。谢谢。

您只需将文件作为字节数组读入,然后通过线路发送该字节数组即可

var bytes = File.ReadAllBytes(path);
stm.Write(bytes , 0, bytes .Length);
将获得文件的字节数组
将把它写入磁盘

为了帮助区分消息的文件/命令/状态/等内容,您可以在消息的前面添加一个字节头。EnUM:字节可能会派上用场。

按要求,您可以在客户端中执行下列操作,如您所建议的,除非您想考虑将同步<代码>发送<代码> >部分转换为异步代码>发送<代码>:

clientSocket.BeginSend(bytes, 0, bytes.Length, SocketFlags.None, endSendCallback, clientSocket); //use async
private static void endSendCallback(IAsyncResult ar) {
    try {
        SocketError errorCode;
        int result = clientSocket.EndSend(ar, out errorCode);
        Console.WriteLine(errorCode == SocketError.Success ?
            "Successful! The size of the message sent was :" + result.ToString() :
            "Error with error code: " + errorCode.ToString() //you probably want to consider to resend if there is error code, but best practice is to handle the error one by one
        );
    } catch (Exception e) { //exception
        Console.WriteLine("Unhandled EndSend Exception! " + e.ToString());
        //do something like retry or just report that the sending fails
        //But since this is an exception, it probably best NOT to retry
    }
}
您的回调可能如下所示:

clientSocket.BeginSend(bytes, 0, bytes.Length, SocketFlags.None, endSendCallback, clientSocket); //use async
private static void endSendCallback(IAsyncResult ar) {
    try {
        SocketError errorCode;
        int result = clientSocket.EndSend(ar, out errorCode);
        Console.WriteLine(errorCode == SocketError.Success ?
            "Successful! The size of the message sent was :" + result.ToString() :
            "Error with error code: " + errorCode.ToString() //you probably want to consider to resend if there is error code, but best practice is to handle the error one by one
        );
    } catch (Exception e) { //exception
        Console.WriteLine("Unhandled EndSend Exception! " + e.ToString());
        //do something like retry or just report that the sending fails
        //But since this is an exception, it probably best NOT to retry
    }
}
上述变更后,客户端代码的完整性如何:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace TcpClientConsoleApplication {
    class Program {
        const int PORT_NO = 2201;
        const string SERVER_IP = "127.0.0.1";
        static Socket clientSocket; //put here
        static void Main(string[] args) {
            //Similarly, start defining your client socket as soon as you start. 
            clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            loopConnect(3, 3); //for failure handling
            string result = "";
            do {
                result = Console.ReadLine(); //you need to change this part
                if (result.ToLower().Trim() != "exit") {
                    byte[] bytes = Encoding.ASCII.GetBytes(result); //Again, note that your data is actually of byte[], not string
                    //do something on bytes by using the reference such that you can type in HEX STRING but sending thing in bytes
                    clientSocket.BeginSend(bytes, 0, bytes.Length, SocketFlags.None, endSendCallback, clientSocket); //use async
                    //clientSocket.Send(bytes); use this for sync send
                }
            } while (result.ToLower().Trim() != "exit");
        }

        private static void endSendCallback(IAsyncResult ar) {
            try {
                SocketError errorCode;
                int result = clientSocket.EndSend(ar, out errorCode);
                Console.WriteLine(errorCode == SocketError.Success ?
                    "Successful! The size of the message sent was :" + result.ToString() :
                    "Error with error code: " + errorCode.ToString() //you probably want to consider to resend if there is error code, but best practice is to handle the error one by one
                );
            } catch (Exception e) { //exception
                Console.WriteLine("Unhandled EndSend Exception! " + e.ToString());
                //do something like retry or just report that the sending fails
                //But since this is an exception, it probably best NOT to retry
            }
        }

        static void loopConnect(int noOfRetry, int attemptPeriodInSeconds) {
            int attempts = 0;
            while (!clientSocket.Connected && attempts < noOfRetry) {
                try {
                    ++attempts;
                    IAsyncResult result = clientSocket.BeginConnect(IPAddress.Parse(SERVER_IP), PORT_NO, endConnectCallback, null);
                    result.AsyncWaitHandle.WaitOne(TimeSpan.FromSeconds(attemptPeriodInSeconds));
                    System.Threading.Thread.Sleep(attemptPeriodInSeconds * 1000);
                } catch (Exception e) {
                    Console.WriteLine("Error: " + e.ToString());
                }
            }
            if (!clientSocket.Connected) {
                Console.WriteLine("Connection attempt is unsuccessful!");
                return;
            }
        }

        private const int BUFFER_SIZE = 4096;
        private static byte[] buffer = new byte[BUFFER_SIZE]; //buffer size is limited to BUFFER_SIZE per message
        private static void endConnectCallback(IAsyncResult ar) {
            try {
                clientSocket.EndConnect(ar);
                if (clientSocket.Connected) {
                    clientSocket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(receiveCallback), clientSocket);
                } else {
                    Console.WriteLine("End of connection attempt, fail to connect...");
                }
            } catch (Exception e) {
                Console.WriteLine("End-connection attempt is unsuccessful! " + e.ToString());
            }
        }

        const int MAX_RECEIVE_ATTEMPT = 10;
        static int receiveAttempt = 0;
        private static void receiveCallback(IAsyncResult result) {
            System.Net.Sockets.Socket socket = null;
            try {
                socket = (System.Net.Sockets.Socket)result.AsyncState;
                if (socket.Connected) {
                    int received = socket.EndReceive(result);
                    if (received > 0) {
                        receiveAttempt = 0;
                        byte[] data = new byte[received];
                        Buffer.BlockCopy(buffer, 0, data, 0, data.Length); //There are several way to do this according to https://stackoverflow.com/questions/5099604/any-faster-way-of-copying-arrays-in-c in general, System.Buffer.memcpyimpl is the fastest
                        //DO ANYTHING THAT YOU WANT WITH data, IT IS THE RECEIVED PACKET!
                        //Notice that your data is not string! It is actually byte[]
                        //For now I will just print it out
                        Console.WriteLine("Server: " + Encoding.UTF8.GetString(data));
                        socket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(receiveCallback), socket);
                    } else if (receiveAttempt < MAX_RECEIVE_ATTEMPT) { //not exceeding the max attempt, try again
                        ++receiveAttempt;
                        socket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(receiveCallback), socket);
                    } else { //completely fails!
                        Console.WriteLine("receiveCallback is failed!");
                        receiveAttempt = 0;
                        clientSocket.Close();
                    }
                }
            } catch (Exception e) { // this exception will happen when "this" is be disposed...
                Console.WriteLine("receiveCallback is failed! " + e.ToString());
            }
        }
    }
}
  • 然后通过
    ASync
    BeginConnect
    开始连接。对于这样的故障处理,我通常会通过
    LoopConnect
    更进一步

    static void loopConnect(int noOfRetry, int attemptPeriodInSeconds) {
        int attempts = 0;
        while (!clientSocket.Connected && attempts < noOfRetry) {
            try {
                ++attempts;
                IAsyncResult result = clientSocket.BeginConnect(IPAddress.Parse(SERVER_IP), PORT_NO, endConnect, null);
                result.AsyncWaitHandle.WaitOne(TimeSpan.FromSeconds(attemptPeriodInSeconds));
                System.Threading.Thread.Sleep(attemptPeriodInSeconds * 1000);
            } catch (Exception e) {
                Console.WriteLine("Error: " + e.ToString());
            }
        }
        if (!clientSocket.Connected) {
            Console.WriteLine("Connection attempt is unsuccessful!");
            return;
        }
    }
    
  • 当然,您需要定义您的
    receiveCallback
    ,就像您为服务器所做的一样。是的,正如您所猜测的,它与您为服务器所做的几乎相同

  • 你可以用你的数据做任何你想做的事情。请注意,您收到的数据实际上是
    字节[]
    ,而不是
    字符串
    。所以你可以用它做任何事。但是举例来说,我将只使用
    string
    来显示

    const int MAX_RECEIVE_ATTEMPT = 10;
    static int receiveAttempt = 0;
    private static void receiveCallback(IAsyncResult result) {
        System.Net.Sockets.Socket socket = null;
        try {
            socket = (System.Net.Sockets.Socket)result.AsyncState;
            if (socket.Connected) {
                int received = socket.EndReceive(result);
                if (received > 0) {
                    receiveAttempt = 0;
                    byte[] data = new byte[received];
                    Buffer.BlockCopy(buffer, 0, data, 0, data.Length); //copy the data from your buffer
                    //DO ANYTHING THAT YOU WANT WITH data, IT IS THE RECEIVED PACKET!
                    //Notice that your data is not string! It is actually byte[]
                    //For now I will just print it out
                    Console.WriteLine("Server: " + Encoding.UTF8.GetString(data));
                    socket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(receiveCallback), socket);
                } else if (receiveAttempt < MAX_RECEIVE_ATTEMPT) { //not exceeding the max attempt, try again
                    ++receiveAttempt;
                    socket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(receiveCallback), socket);
                } else { //completely fails!
                    Console.WriteLine("receiveCallback is failed!");
                    receiveAttempt = 0;
                    clientSocket.Close();
                }
            }
        } catch (Exception e) { // this exception will happen when "this" is be disposed...
            Console.WriteLine("receiveCallback is failed! " + e.ToString());
        }
    }
    
  • 最后,在最后,您只需要声明async
    EndSend
    回调函数,就完成了

    private static void endSendCallback(IAsyncResult ar) {
        try {
            SocketError errorCode;
            int result = clientSocket.EndSend(ar, out errorCode);
            Console.WriteLine(errorCode == SocketError.Success ?
                "Successful! The size of the message sent was :" + result.ToString() :
                "Error with error code: " + errorCode.ToString() //you probably want to consider to resend if there is error code, but best practice is to handle the error one by one
            );
        } catch (Exception e) { //exception
            Console.WriteLine("Unhandled EndSend Exception! " + e.ToString());
            //do something like retry or just report that the sending fails
            //But since this is an exception, it probably best NOT to retry
        }
    }
    

  • 提示:你的意思是只针对客户?我现在不在电脑旁。只能通过手机访问。当然,如果你愿意,我会把它寄出去,可能明天。我可能也会用异步发送(而不是同步发送)来更新它。如何使用字节->ba?
    private static void endSendCallback(IAsyncResult ar) {
        try {
            SocketError errorCode;
            int result = clientSocket.EndSend(ar, out errorCode);
            Console.WriteLine(errorCode == SocketError.Success ?
                "Successful! The size of the message sent was :" + result.ToString() :
                "Error with error code: " + errorCode.ToString() //you probably want to consider to resend if there is error code, but best practice is to handle the error one by one
            );
        } catch (Exception e) { //exception
            Console.WriteLine("Unhandled EndSend Exception! " + e.ToString());
            //do something like retry or just report that the sending fails
            //But since this is an exception, it probably best NOT to retry
        }
    }