简体   繁体   中英

Desktop client web server communication

I am doing client server communication. I got both connected via URLConnection classes.

Now I am trying to send log in information to server, Server will check if information is correct else it will ask me to log in again and for this scenario lets assume log in was unsuccessful. But after getting response from server when I try again to send log in information I am getting

java.net.ProtocolException: Cannot write output after reading input.

Here is my code:

URL url = new URL(uniRL);

java.net.URLConnection connection = url.openConnection();
connection.setAllowUserInteraction(true);
connection.setDoOutput(true);

while(true){
    System.out.println("Enter 1-login , 2-Exit");
useroption = input.nextLine();
numOption = Integer.parseInt(useroption);
if( numOption == 1){
    OutputStreamWriter writer = new OutputStreamWriter(
            connection.getOutputStream());
    user_login = login();
    writer.write(user_login[0]+"@");
    writer.write(user_login[1]);
    writer.flush();
    //out.close();
    BufferedReader in = new BufferedReader(
            new InputStreamReader(
                    connection.getInputStream()));
    while ((tempString = in.readLine()) != null) {
        decodedString = tempString;
        //System.out.println(decodedString);
        //System.out.println(decodedString.equalsIgnoreCase("unknown user"));
    }
    in.close();
    if((decodedString.equalsIgnoreCase("unknown user"))){continue;}
    else{break;}
}

When you call in.close() , you're actually closing the stream used by the URL connection. You'd need to call url.openConnection() again to re-open the stream...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM