Bom dia, estou escrevendo uma aplicação onde o server socket é em C#(devido há alguns drivers tem que ser em c#) e o cliente em Java. Consigo fazer a comunicação perfeitamente entre eles, porém ao passar dados, tenho o seguinte erro:
System.ArgumentOutOfRangeException: Argumento especificado estava fora do intervalo de valores válidos. Nome do parâmetro: size em System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size) em testes.handleClinet.doChat() na d:\c#\socket\testes\handleClinet.cs:linha 40
Código Server c#:
//Class to handle each client request separatly
public class handleClinet
{
TcpClient clientSocket;
string clNo;
public void startClient(TcpClient inClientSocket, string clineNo)
{
this.clientSocket = inClientSocket;
this.clNo = clineNo;
Thread ctThread = new Thread(doChat);
ctThread.Start();
}
private void doChat()
{
int requestCount = 0;
byte[] bytesFrom = new byte[10000];
string dataFromClient = null;
Byte[] sendBytes = null;
string serverResponse = null;
string rCount = null;
requestCount = 0;
while ((true))
{
try
{
requestCount = requestCount + 1;
NetworkStream networkStream = clientSocket.GetStream();
Console.WriteLine(" >> " + (int)clientSocket.ReceiveBufferSize);
networkStream.Read(bytesFrom, 0, (int)clientSocket.ReceiveBufferSize);
dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom);
dataFromClient = dataFromClient.Substring(0, dataFromClient.IndexOf("$"));
Console.WriteLine(" >> " + "From client-" + clNo + dataFromClient);
rCount = Convert.ToString(requestCount);
serverResponse = "Server to clinet(" + clNo + ") " + rCount;
sendBytes = Encoding.ASCII.GetBytes(serverResponse);
networkStream.Write(sendBytes, 0, sendBytes.Length);
networkStream.Flush();
Console.WriteLine(" >> " + serverResponse);
}
catch (Exception ex)
{
Console.WriteLine(" >> " + ex.ToString());
return;
}
}
}
}
Código Client: Java
public static void main(String[] args) throws IOException {
String sentence;
String modifiedSentence;
BufferedReader inFromUser = new BufferedReader( new InputStreamReader(System.in));
Socket clientSocket = new Socket("localhost", 9999);
DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
sentence = inFromUser.readLine();
outToServer.writeBytes(sentence + '\n');
modifiedSentence = inFromServer.readLine();
System.out.println("FROM SERVER: " + modifiedSentence);
clientSocket.close();
}
Novo client em Java:
public class Cliente {
public static void main(String[] args) {
try {
Socket s = new Socket("127.0.0.1", 9999);
InputStream i = s.getInputStream();
OutputStream o = s.getOutputStream();
String str;
while(true){
byte[] line = new byte[100];
System.in.read(line);
o.write(line);
i.read(line);
str = new String(line);
System.out.println(str.trim());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Grato pela ajuda.