Reputation: 13
I Create simple programm Server-Client in Java My server is
public class Server {
private static ServerSocket serverSocket;
private static Socket clientSocket;
private static BufferedReader bufferedReader;
private static String inputLine;
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
try
{
serverSocket = new ServerSocket(5000);
clientSocket = serverSocket.accept();
// Create a reader
bufferedReader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
// Get the client message
while((inputLine = bufferedReader.readLine()) != null)
System.out.println(inputLine); //Print
}catch(IOException e){
System.out.println(e);
}
}
} Client Side is :
public class Client {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
String hostName = "localhost";
try (
Socket echoSocket = new Socket(hostName, 5000);
PrintWriter out = new PrintWriter(echoSocket.getOutputStream(), true);
BufferedReader in= new BufferedReader(new InputStreamReader(echoSocket.getInputStream()));
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in))) {
String userInput;
while ((userInput = stdIn.readLine()) != null) {
out.println(userInput);
System.out.println("echo: " + in.readLine());
}
} catch (UnknownHostException e) {
System.err.println("Don't know about host " + hostName);
System.exit(1);
} catch (IOException e) {
System.err.println("Couldn't get I/O for the connection to " + hostName);
System.exit(1);
}
}
}
Why my server not reading again? When I debug the programm My programm it's going out of the loop While in Server..I don'w know Why
Upvotes: 0
Views: 155
Reputation: 13
Ok now My server Is run Perfectly ..Now I have one client In android and when i send one message the Server Programm show me the message and then the Server terminated>.. Here is the programm in android Programm When i push the Button
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
messsage = textField.getText().toString(); // get the text message on the text field
textField.setText(""); // Reset the text field to blank
SendMessage sendMessageTask = new SendMessage();
sendMessageTask.execute();
}
});
}
private class SendMessage extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
try {
client = new Socket("192.168.2.3", 5000); // connect to the server
printwriter = new PrintWriter(client.getOutputStream(), true);
printwriter.write(messsage); // write the message to output stream
printwriter.flush();
printwriter.close();
// client.close(); // closing the connection
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
Upvotes: 1
Reputation: 89
Your client is not sending any new line to the server. Its stuck trying to read response from the server.
You have two options to fix this
1 - Send a response in Server back to the client 2 - Remove the in.readLine()
Best of luck
Upvotes: 1
Reputation: 1911
Your server seems to be okay. The reason why your server isn't able to read more than one time is this line on client-side:
System.out.println("echo: " + in.readLine());
The client expects an answer from the server, but your server does not send anything back. So remove the above line or make the server send a response.
Upvotes: 1