Home > other >  Java Client not receiving C# Server response TCP
Java Client not receiving C# Server response TCP

Time:12-11

Can somebody explain to me what I am doing wrong.First time I try to implement TCP between Java and C#:

Sever code c# `

public void CreateServer()
        {
            Thread thread = new Thread(() =>
            {
                IPAddress addr = IPAddress.Parse(localIP);
                tcpListener = new TcpListener(addr, 5053);
                if (tcpListener != null)
                {
                    tcpListener.Start();
                    while (!end)
                    {      
                        TcpClient tcpClient = tcpListener.AcceptTcpClient();
                        var ip = ((IPEndPoint)tcpClient.Client.RemoteEndPoint).Address.ToString();
                        Console.WriteLine("Client connected from " ip);   
                        NetworkStream clientStream = tcpClient.GetStream();
                        StreamReader reader = new StreamReader(clientStream, Encoding.UTF8);
                        try
                        {
                            string request = reader.ReadToEnd();
                            Console.WriteLine("Message from client: "   request);

                            Byte[] StringToSend = Encoding.UTF8.GetBytes("Server");
                            clientStream.Write(StringToSend, 0, StringToSend.Length);
                            Console.WriteLine("Sending response back");
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine(e);
                        } 
                    }
                }
            });
            thread.Start();
        }

`

Client code java `

public class TCP {

    private String IP;
    private InetAddress server;
    private Socket socket;

    public TCP(String IP) {
        this.IP = IP;
    }

    protected void runTCP() {
        try {
            server = InetAddress.getByName(IP);
            socket = new Socket(server, 5053);
            System.out.println("Client connected. Listening on port 5053");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void sendMessage(String message) {
        try {
            System.out.println("Sending data...");
            if (socket.isClosed()) socket = new Socket(server, 5053);
            PrintWriter writer = new PrintWriter(socket.getOutputStream());
            writer.print(message);
            writer.flush();
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void getResponseServer() {
        Thread thread = new Thread() {
            @Override
            public void run() {
                try {
                    System.out.println("Attempting to get response...");
                    if (socket.isClosed()) socket = new Socket(server, 5053);
                    BufferedReader mBufferIn = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                    String mServerMessage = mBufferIn.readLine();
                    System.out.println("Server message: "   mServerMessage);
                } catch (Exception e) {e.printStackTrace();}
            }
        };
        thread.start();
    }
}

`

Output on server I get when sending "Hello" from client to server: Client connected from 192.16.... Message from client: Hello Sending response back Client connected from 192.16....

Output on client: Client connected. Listening on port 5053 Sending data... Attempting to get response... Never gets response... Why?

Tried researching but found nothing yet, tried other code but didnt work aswell...

CodePudding user response:

sorry , I can't comment. maybe you can use telnet command to vertify c# code is corrent.

telnet ip port

first, locate problem, then solve it.

if server is ok , we can use nc command vertify client code, I have test your java code , except every send data will close socket , other is ok.

CodePudding user response:

Fixed it by removing writer.close() cause that causes socket closing and makes another connection to the server by creating again the socket which makes the server wait for a stream of data and the client wait for a response...

        System.out.println("Sending data...");
        PrintWriter writer = new PrintWriter(socket.getOutputStream(), true);
        writer.println(message);

        InputStream input = socket.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(input));
        String line;
        while ((line = reader.readLine()) != null) {
            System.out.println(line);
        }
  • Related