Beispiel #1
0
        public static bool sendToRecv(string ip, int port, ref string jsondata)
        {
            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            try
            {
                socket.Connect(ip, port);
                SJNetMessage sjnm = new SJNetMessage(jsondata);
                socket.Send(sjnm.Data);
                byte[] bjsondata = System.Text.Encoding.Unicode.GetBytes(jsondata);
                socket.Send(bjsondata);
                byte[] recvData = new byte[recvDataLen];
                socket.Receive(recvData);
                SJNetMessage recvsjnm = new SJNetMessage(recvData);
                Dictionary <string, object> result = recvsjnm.GetMsg();
                if (result == null)
                {
                    LogHandler("接收到的数据处理失败");
                    return(false);
                }
                LogHandler("接收到的数据:" + result.ToString());
                jsondata = result["value"] as string;
                socket.Close();
                return(true);
            }
            catch (Exception ex)
            {
                socket.Close();
                LogHandler("发送失败:ip = " + ip + " port = " + port + " data = " + jsondata + "  错误:" + ex.ToString());
            }
            return(false);
        }
Beispiel #2
0
        public static bool sendTo(string ip, int port, string jsondata)
        {
            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            try
            {
                socket.Connect(ip, port);
                SJNetMessage sjnm = new SJNetMessage(jsondata);
                socket.Send(sjnm.Data);
                socket.Close();
                return(true);
            }
            catch (Exception ex)
            {
                socket.Close();
                LogHandler("发送失败:ip = " + ip + " port = " + port + " data = " + jsondata + "  错误:" + ex.ToString());
            }
            return(false);
        }