Socket da deadlock quando tento enviar e ler seguidamente [RESOLVIDO]

3 respostas
mauricioadl

Pessoal, tenho uma conexao com socket onde envio uma string e logo abaixo eu tento ler o retorno do servidor, porem isso faz com que o servidor fique travado na leitura do cliente

codigo do servidor

Object ret = null;
		try {
			BufferedReader reader = new BufferedReader(new InputStreamReader(client.getInputStream()));
			String param = reader.readLine(); // TRAVA AQUI

			String[] array = param.split("\\|");
			Class clas = Class.forName(array[0]);
			Object obj = clas.newInstance();

			Method method = null;
			if (array.length > 2) {
				Class[] params = new Class[array.length - 2];
				for (int i = 0; i < array.length - 2; i++) {
					params[i] = String.class;
				}
				method = clas.getMethod(array[1], params);
				ret = method.invoke(obj, Arrays.copyOfRange(array, 2, array.length));
			} else {
				method = clas.getMethod(array[1]);
				ret = method.invoke(obj);
			}
		} catch (Exception e) {
			ret = e.getClass() + " " + e.getMessage();
			e.printStackTrace();
		} finally {
			try {
				PrintWriter pw = new PrintWriter(client.getOutputStream());
				pw.write(ret.toString());
				pw.flush();
				client.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

codigo do cliente

Socket s = new Socket(ip.getText(), Integer.parseInt(porta
							.getText()));
					BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
					bw.write(classe.getText() + "|" + metodo.getText() + "|" + parametros.getText());
					bw.flush();
					
// ESSA LINHA FAZ COM QUE FIQUE TRAVADO NO SERVIDOR, SEU REMOVE-LA O FLUXO CONTINUA NORMALMENTE
					BufferedReader reader = new BufferedReader(new InputStreamReader(s.getInputStream()));
					retorno.setText(reader.readLine());
					s.close();

Ja tentei varias formas de leitura e escrita, mas todos fazem mesma coisa.

alguma sugestao???

3 Respostas

E

Procure por TCP NODELAY.

mauricioadl

Não deu certo, ainda continua travando.

Mais sugestões???

mauricioadl

pessoal, consegui resolver.
depois que acabo de escrever na saida do servidor eu chamo o metodo .shutdownOutput();

nao sei se isso vai ter algum efeito colateral, mas por equanto ta funfando de boa.

Vlw!

Criado 22 de novembro de 2011
Ultima resposta 23 de nov. de 2011
Respostas 3
Participantes 2