Ejemplo n.º 1
0
        // 客户端方法,往指定ip发送消息,有接收到文件时,recievedFile就是文件路径
        public static string SendBySocket(string ip, int port, string msgs, ref string recievedFile)
        {
            try
            {
                using (Socket socket = ConnectSocket(ip, port))
                {
                    SocketCommon.SendData(socket, msgs);

                    if (!string.IsNullOrEmpty(recievedFile) && File.Exists(recievedFile))
                    {
                        SocketCommon.SendFile(socket, recievedFile);
                    }

                    // 接收 服务器返回的信息
                    socket.Blocking = true; // 在socket的Receive方法前必须明确指明其为阻塞模式
                    //ClientSocket.ReceiveTimeout = 5000;

                    string ret = SocketCommon.RecieveData(socket, out recievedFile);
                    return(ret);
                }
            }
            catch (Exception exp)
            {
                return("err" + exp);
            }
        }
Ejemplo n.º 2
0
        private static void RecieveAccept(object args)
        {
            string endpoint = string.Empty;
            Socket socket   = null;

            try
            {
                socket = args as Socket;
                if (socket == null)
                {
                    Utils.Output(" socket为空.", "RecieveAccept");
                    return;
                }
                endpoint = socket.RemoteEndPoint.ToString();
                if (!socket.Connected)
                {
                    Utils.Output(endpoint + " 未连接.", "RecieveAccept");
                    return;
                }

                // 接受数据等待时间,超过时,断开连接
                socket.ReceiveTimeout = (int)TimeSpan.FromSeconds(TaskService.WaitClientSecond).TotalMilliseconds;

                // 接收客户端发来的数据
                string recieveFilePath;
                string recieveMsg = SocketCommon.RecieveData(socket, out recieveFilePath);

                // 是否有接收到文件,有接收到时,不再发送(目前没有先接收再发送文件的操作,用这个进行标识)
                bool haveRecieveFile = !string.IsNullOrEmpty(recieveFilePath);

                // 给客户端返回消息
                string response;
                if (Method != null)
                {
                    try
                    {
                        response = Method(recieveMsg, ref recieveFilePath);
                    }
                    catch (Exception exp)
                    {
                        response = "err:" + exp;
                    }
                }
                else
                {
                    response = "err:未设定处理函数";
                }

                Utils.Output(endpoint + "\r\n***接收***:" + recieveFilePath + "\r\n" + recieveMsg +
                             "\r\n***响应***:\r\n" + response, "socketResponse");
                if (socket.Connected)
                {
                    SocketCommon.SendData(socket, response);
                    // 没接收到文件,但是指定了文件路径时,要发送文件
                    if (!haveRecieveFile && !string.IsNullOrEmpty(recieveFilePath) && File.Exists(recieveFilePath))
                    {
                        SocketCommon.SendFile(socket, recieveFilePath);
                    }
                }
            }
            catch (SocketException exp)
            {
                if (exp.ErrorCode == 10060)
                {
                    // ReceiveTimeout到时了
                    //return;
                }
                LogHelper.WriteException("err监听出错:", exp);
                //return;
            }
            catch (Exception exp)
            {
                LogHelper.WriteException("err监听出错:", exp);
                //return;
            }
            finally
            {
                if (socket != null)
                {
                    socket.Close();
                }
                Utils.Output(endpoint + " 成功断开服务器.", "socketDetail");
            }
        }