2017-03-15 6 views
-2

을 사용하는 서버로 보내는 방법 은행 서버에서 세부 정보를 얻으려면 IP와 PORT 번호를 알려줄 것입니다. 메시지를 보내고 받으려면 ISO8583 메시징 형식 (NuGet Manager의 OpenIso8583 라이브러리)을 사용하고 있습니다. IP 주소와 포트 번호를 사용하여 은행 서버에 ISO8583 메시지를 보내는 방법에 대한 솔루션을 제게 제공하십시오.Iso8583 형식의 메시지를 C#

공공 무효 포스트() {

 var msg = new Iso8583(); 
     msg.MessageType = Iso8583.MsgType._0200_TRAN_REQ; 

     //Following feilds are mandotary to get balanace info 
     msg[Iso8583.Bit._003_PROC_CODE] = "820000";//balanace enquery 
     msg[Iso8583.Bit._102_ACCOUNT_ID_1] = "0021210020320000069";//bankid(002)branchid(121)accnumber(0020320000069)   

     //send iso message to server 
     var response = NetworkSend("192.32.179.171", 45510, msg);//ipaddress,port,msg 

     if (response[Iso8583.Bit._039_RESPONSE_CODE] == "000") 
     { 
       //success 

      // read data of iso message 
     } 

    } 

    private static Iso8583 NetworkSend(string ip, int port, Iso8583 msg) 
    { 

     // We're going to use a 2 byte header to indicate the message length 
     // which is not inclusive of the length of the header 
     var msgData = new byte[msg.PackedLength + 2]; 

     // The two byte header is a base 256 number so we can set the first two bytes in the data 
     // to send like this 
     msgData[0] = (byte)(msg.PackedLength % 256); 
     msgData[1] = (byte)(msg.PackedLength/256); 

     // Copy the message into msgData 
     Array.Copy(msg.ToMsg(), 0, msgData, 2, msg.PackedLength); 

     // Now send the message. We're going to behave like a terminal, which is 
     // connect, send, receive response, disconnect 
     var client = new TcpClient(); 
     var endPoint = new IPEndPoint(IPAddress.Parse(ip), port); 
     client.Connect(endPoint); 
     var stream = client.GetStream(); 

     // Send the packed message 
     stream.Write(msgData, 0, msgData.Length); 

     // Receive the response 

     // First we need to get the length of the message out of the socket 
     var lengthHeader = new byte[2]; 
     stream.Read(lengthHeader, 0,2);//this line giving error 

     // Work out the length of the incoming message 
     var rspLength = lengthHeader[0] * 256 + lengthHeader[1]; 
     var rspData = new byte[rspLength]; 

     // Read the bytes off the network stream 
     stream.Read(rspData, 0, rspLength); 

     // Close the network stream and client 
     stream.Close(); 
     client.Close(); 

     // Parse the data into an Iso8583 message and return it 
     var rspMsg = new Iso8583(); 

     rspMsg.Unpack(rspData, 0); 
     return rspMsg; 
    } 

답변

0

첫 번째 문제는 메시지의 포장입니다. msgdata [0]과 msgdata [1]의 값은 바꿔줘야합니다. 그렇지 않으면 호스트가 보내고있는 것보다 훨씬 더 많은 것을 기대하고 있습니다.

들어오는 데이터를 기다리는 부분은 패킷 조각화 때문에 같은 문제가 발생했습니다. 내가 한 일은 내 소켓에 OnReceived 이벤트를 추가하는 것이었고,이 이벤트에서받은 데이터가 1 ​​바이트 이상인 경우 첫 번째 읽기가 확인되었습니다. 그렇다면 첫 번째 2 바이트는 내 길이 표시기 였고 길이 표시기에 표시된 총계에 도달하거나 시간 초과가 발생할 때까지 버퍼에 계속해서 읽고 추가했습니다. 읽을 바이트가 없을 때까지 OnReceived 이벤트를 재귀 적으로 다시 입력하여이 작업을 수행했습니다.