简体   繁体   中英

Server client communication using Socket C#

I have created an application, which is communicating a server using IP/port. It is sending a ISO request string(Bitmaps in ASCII format) and receiving a response string same format. While Sending the bytes through socket.send and receiving the bytes in socket.receive method. I can see extended ASCII characters are getting changed on other side (Server side/Clint side). I am using below code. Can anyone suggest please, How to resolve the issue.

IPAddress ipAddr = IPAddress.Parse("10.03.0.18");               

IPEndPoint localEndPoint = new IPEndPoint(ipAddr, 12345);            

// Creation TCP/IP Socket using  
// Socket Class Costructor 
Socket sender = new Socket(ipAddr.AddressFamily,SocketType.Stream, ProtocolType.Tcp);             

sender.Connect(localEndPoint);
string requeststring = AccountValidationRequest();              

byte[] messageSent = Encoding.ASCII.GetBytes(requeststring);

int byteSent = sender.Send(messageSent);

byte[] ServerResponse = new byte[1024];
int byteRecv = sender.Receive(ServerResponse); 
string ISO8583Message = Encoding.ASCII.GetString(ServerResponse, 0, byteRecv);

Console.WriteLine("Response received from server: --> :{0}", Encoding.ASCII.GetString(ServerResponse, 0, byteRecv));

As it was suggested in comments you should assert that you read all data, not only the first chunk.

Byte[] bytesReceived = new Byte[256];
int bytes = 0;

var ISO8583Message = "Response: \n";
do {
    bytes = sender.Receive(bytesReceived, bytesReceived.Length, 0);
    ISO8583Message = ISO8583Message + Encoding.ASCII.GetString(bytesReceived, 0, bytes);
}
while (bytes > 0);

There are some nice examples with using sockets on https://docs.microsoft.com/pl-pl/dotnet/api/system.net.sockets.socket?view=netframework-4.8 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM