Ola Galera, estou com um problema para enviar e receber uma string usando socket.
O problema e que quando meu cliente recebe a string o valor dela é “null” e não o texto que eu quero.
**
Enviar:
**
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package a.i.d.s.server;
import static a.i.d.s.server.AIDSServer.*;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Matheus
*/
public class Commands {
static boolean Commad_close;
static String toSend;
static int SendN;
public Commands() throws IOException{
OutputStream outstream = Connect_Client.Client.getOutputStream();
PrintWriter out = new PrintWriter(outstream);
if(Command == "MINE_AUTO"){
toSend = "MINE_AUTO";
}
if(Command == "MINE_ON"){
toSend = "MINE_ON";
}
if(Command == "MINE_OFF"){
toSend = "MINE_OFF";
}
if(Command == "PC_ON"){
new Controle_ON_OFF();
Controle_ON_OFF.Com = Command;
}
if(Command == "PC_OFF"){
new Controle_ON_OFF();
Controle_ON_OFF.Com = Command;
}
if(Command == "RELOAD_IP"){
toSend = "RELOAD_IP";
out.write(toSend);
System.out.println("Comando Enviado: " + Command);
System.out.println("Preparando para Enviar o Novo IP");
try {
Thread.sleep(100);
toSend = TrocarIP_GUI.NewIP;
out.write(toSend);
System.out.println("Novo IP Enviado");
} catch (InterruptedException ex) {
Logger.getLogger(Commands.class.getName()).log(Level.SEVERE, null, ex);
}
}
out.write(toSend);
System.out.println("Comando Enviado: " + Command);
Connect_Client.close = true;
}
public static void main(String[] args) throws IOException {
}
}
**
Receber:
**
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package a.i.d.s.client;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Matheus
*/
public class Server extends Thread{
static ServerSocket server;
static int Port_Server = 7894;
static int Port_Client = 4558;
static String command;
public void run(){
try {
server = new ServerSocket(Port_Server);
while(true) {
Socket client = server.accept();
if(client.isConnected() == true){
BufferedReader entrada = new BufferedReader(new InputStreamReader(client.getInputStream()));
command = entrada.readLine();
System.out.println("Comando Recebido: "+ command);
entrada.close();
client.close();
}
}
} catch (IOException ex) {
Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
}
}
public static void main(String[] args) throws IOException {
// TODO code application logic here
}
}