Socket - Java client, C # server

I am doing a program in 2 parts.

Part 1: C # server-socket An application running on a PC, listening for commands and actions, respectively.

Part 2: a Java client socket application running on the phone that sends a command to the computer when the button is clicked.

Currently, I can send commands from the client to the server, and all this is good. But my problem is this: when I send a certain command to the server, I want the server to respond to the client and the client to read this answer.

It's just that when a client tries to read, it's timeouts.

Java client program:

class ClientThread implements Runnable { public void run() { try { Socket socket = new Socket(serverIpAddress, serverPort); socket.setSoTimeout(5000); while (true) { try { PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true); Log.d("Nicklas", "Out it goes"); out.println(Command); if (Command == "CMD:GetOptions<EOF>") { Log.d("Nicklas", "Getting options"); try { Log.d("Nicklas", "Line 1"); BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream())); Log.d("Nicklas", "Line 2"); String answer = in.readLine(); Log.d("Nicklas", "answer = " + answer ); } catch (Exception ee) { Log.d("Nicklasasasas", ee.toString()); } } break; } catch (Exception e) { Log.d("Nicklas", "CAE = " + e.toString()); break; } } socket.close(); } catch (ConnectException ee) { Log.d("Nicklas", "Kunne ikke forbinde"); } catch (Exception e) { Log.d("Nicklasssssss", e.toString()); } } } 

Called with:

 Thread cThread = new Thread(new ClientThread()); cThread.start(); 

And uses the global variable "Command", which will contain various information, depending on which button was pressed.

The program does not work on the string "String answer = in.readline ();" with the exception of "java.net.SocketTimeoutException".

This is part of the C # program server:

 private void ListenForClients() { this.tcpListener.Start(); while (true) { //blocks until a client has connected to the server TcpClient client = this.tcpListener.AcceptTcpClient(); //create a thread to handle communication //with connected client Thread clientThread = new Thread(new ParameterizedThreadStart(HandleClientComm)); clientThread.Start(client); } } private void HandleClientComm(object client) { TcpClient tcpClient = (TcpClient)client; NetworkStream clientStream = tcpClient.GetStream(); byte[] message = new byte[4096]; int bytesRead; while (true) { bytesRead = 0; try { //blocks until a client sends a message bytesRead = clientStream.Read(message, 0, 4096); } catch { //a socket error has occured break; } if (bytesRead == 0) { //the client has disconnected from the server break; } //message has successfully been received ASCIIEncoding encoder = new ASCIIEncoding(); //System.Diagnostics.Debug.WriteLine(encoder.GetString(message, 0, bytesRead)); string Input = (encoder.GetString(message, 0, bytesRead)); Input = Input.Trim(); object[] obj = new object[1]; obj[0] = Input; if (Input == "CMD:GetOptions<EOF>") { try { byte[] buffer = encoder.GetBytes("CMD:Accepted"); clientStream.Write(buffer, 0, buffer.Length); clientStream.Flush(); MessageBox.Show("Client program asked for reply"); } catch (Exception e) { MessageBox.Show("Oh it no work!: " + e.ToString()); } } else { Udfor(Input); } } tcpClient.Close(); } 

Called with the following: in Form1 ()

 this.tcpListener = new TcpListener(IPAddress.Any, 4532); this.listenThread = new Thread(new ThreadStart(ListenForClients)); this.listenThread.Start(); 

C # server seems to be working fine and showing the message "client program requested a response"

Anyone who can spot the error?

+6
source share
1 answer

I get it! The problem was in C #. When the server sent back the β€œCMD: Accepted” command, it never closed the socket, so the Android application had no idea if it had been read! Closing the nest immediately after washing +, of course, without closing it again, if I already did this, did the trick!

+1
source

Source: https://habr.com/ru/post/918739/


All Articles