socket client .NET avec socketlistener serveur en Java
Bonjour a tous.
Je suis acuellement en developpement d'une petite libraire .NET ayant pour but d'accéder a un socketlistner en Java (Je n'ai pas le droit de changer le code en Java, puisque deja utilisé en production)
J'ai essayer d'utiliser le socket TcpClient (la connection doit etre synchrone) ainsi que la classe Socket avec différente configuration (SocketType.Stream, ProtocolType.Tcp), mais pas moyen de recevoir la chaine en retour, quand je ne recoit pas d'exception.
Client .NET
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
|
String ExcepcionOcurrida = "-----Excepcion";
int m_TimeOutEnvio = 0;
int m_TimeOutRecepcion = 0;
String[] Tramas={"SALUT SALUT"};
StringBuilder TraceComunicacion = new StringBuilder();
String[] respuestas = new String[Tramas.Length];
Socket cliente = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.Tcp);
cliente.ReceiveTimeout = 10000; //10sec
cliente.SendTimeout = 10000; //10sec
TraceComunicacion.AppendLine("----Abriendo canal de comunicacion");
int indice = 0;
byte[] ipBytes= {0x7f,0x00,0x00,0x01};
IPAddress serverAddress= new IPAddress(ipBytes);
Console.WriteLine(serverAddress.ToString());
cliente.Connect(serverAddress, 8010);
foreach (String actual in Tramas)
{
Byte[] data;
//NetworkStream ns_IODatos = cliente.GetStream();
String RespuestaActual = String.Empty;
TraceComunicacion.AppendLine("----Enviando Trama: " + actual);
try
{
int longitud;
data = Encoding.ASCII.GetBytes(actual);
longitud = data.Length;
// Console.WriteLine("data.Length" + (string)data.Length.ToString() + Encoding.ASCII.GetString(data, 0, data.Length));
cliente.Send(data, 0, data.Length,SocketFlags.None);
//ns_IODatos.Write(data, 0, longitud);
TraceComunicacion.AppendLine("----Bytes enviados: " + longitud);
TraceComunicacion.AppendLine("----Inicia Recepcion");
// System.Threading.Thread.Sleep(100);
//Longitud de la respuesta
data = new Byte[4];
TraceComunicacion.AppendLine("----Convirtiendo longitud");
data = new Byte[2048];
int receivedBytesNumber = cliente.Receive(data, 0, SocketFlags.None);
//receivedBytesNumber = ns_IODatos.Read(data, 0, data.Length);
RespuestaActual = Encoding.ASCII.GetString(data, 0, receivedBytesNumber);
TraceComunicacion.AppendLine("----Recibiendo respuesta: " + RespuestaActual); |
Serveur Java
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| try {
_ss = new ServerSocket(_iPort);
_iStatus = 1;
while(!_bStop) {
SocketConnection socketConnection = new SocketConnection(_ss.accept(), this);
_vConnections.add(socketConnection);
super.error("SocketListener run() _vConnections.size() [" + _vConnections.size() + "]");
if(_bStop) {
_ss.close();
}
else {
socketConnection.start();
}
}
}
catch(Exception exception1) {
super.error("SocketListener run() Exception [" + exception1.getMessage() + "]");
} |