0

Hello all I hava a big problem with my client-server interaction, when I send a data from client to server it's work but when I send a data from server to my C# client the data are not recieved.

my code: server:

public void ReceiveData(Socket socket, String command) throws IOException {
    System.out.println("reception");

    String[] dataIn;

    BufferedReader br = new BufferedReader(new InputStreamReader(
            socket.getInputStream()));
    String input = br.readLine();
    System.out.println("receive: " + input);

    dataIn = input.split(" ");
    username = dataIn[1];
    System.out.println(dataIn[0]);
    System.out.println(dataIn[1]);
    System.out.println(dataIn[2]);
}

public void SendData(Socket socket, Object data) throws IOException {
    BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));

    bw.write((String) data);
    bw.flush();
    bw.close();
    System.out.println("send: " + data);
}

my client code:

public void SendLogin(NetworkStream ns, string command, string username, string password)
{
    StreamWriter sw = new StreamWriter(ns);
    sw.WriteLine(command + " " + username + " " + password);
    sw.Flush();
    sw.Close();
}

public String ReceiveString(NetworkStream ns)
{
    string value = "";
    StreamReader sr = new StreamReader(ns, Encoding.ASCII);
    value = sr.ReadLine();
    ns.Close();
    return value;
}

thanks

Isador
  • 41
  • 6
  • You should post the `main` method of both Java and C#. But my take is that you should not close the stream every time you write/read something. Close it when **all** IO operation are done. – ortis Nov 28 '14 at 15:38
  • I find that network stream become null enter send and receive data – Isador Nov 28 '14 at 15:57
  • nobody know why networkstream become null? – Isador Nov 29 '14 at 09:57

0 Answers0