5 Replies - 133 Views - Last Post: 10 February 2019 - 03:51 PM Rate Topic: -----

#1 overwhelmed_student   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 35
  • Joined: 10-May 18

socket write error

Posted 10 February 2019 - 12:59 PM

Hey guys, still trying to understand sockets. This time I am getting an error when I want to write something to the server.
My code looks like this:
public void write(Outputstream o, Socket s) throws IOException {
   Scanner scanner = new Scanner(System.in);
   String input = scanner.nextLine();
   o.write(input.getBytes());
   
   input = scanner.nextLine();
   o.write(input.getBytes());

   scanner.close();



This is the method that will send a message from the user to the server. I get the error on the second o.write(input.getBytes()). It says:

java.net.SocketException: Software caused connection abort: socket write error

Why am I losing connection? Or do I not lose it and the problem is something else?

Is This A Good Question/Topic? 0
  • +

Replies To: socket write error

#2 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 757
  • View blog
  • Posts: 5,709
  • Joined: 25-December 13

Re: socket write error

Posted 10 February 2019 - 01:03 PM

Hard to say what the problem is without seeing the code.
Can you post a small, complete program (server and client) that shows the problem?
Don't post too much code. Strip out anything not directly related the problem.

Note: Closing a System.in connection is not a good idea. Once closed, it stays closed for the rest of the program's execution.

This post has been edited by NormR: 10 February 2019 - 01:17 PM

Was This Post Helpful? 1
  • +
  • -

#3 overwhelmed_student   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 35
  • Joined: 10-May 18

Re: socket write error

Posted 10 February 2019 - 01:08 PM

View PostNormR, on 10 February 2019 - 01:03 PM, said:

Note: Closing a System.in connection is not a good idea. Once closed, it stays close for the rest of the program's execution.


So, even if I initialize a new scanner with System.in it won't work?
Was This Post Helpful? 0
  • +
  • -

#4 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 757
  • View blog
  • Posts: 5,709
  • Joined: 25-December 13

Re: socket write error

Posted 10 February 2019 - 01:16 PM

Once System.in is closed, it stays closed. Wrapping it in another class will not open it.
Was This Post Helpful? 1
  • +
  • -

#5 overwhelmed_student   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 35
  • Joined: 10-May 18

Re: socket write error

Posted 10 February 2019 - 02:35 PM

View PostNormR, on 10 February 2019 - 01:16 PM, said:

Once System.in is closed, it stays closed. Wrapping it in another class will not open it.


Oh, I see. I didn't know that.
I also fixed my problem and you kind of helped me because my focus was suddenly on scanner.
I was supposed to send only one String to the server, not two. I didn't know that. But when I tried to use only one scanner.nextLine(), it worked and yeah, fixed it. Yey!
Was This Post Helpful? 0
  • +
  • -

#6 g00se   User is online

  • D.I.C Lover
  • member icon

Reputation: 3617
  • View blog
  • Posts: 16,595
  • Joined: 20-September 08

Re: socket write error

Posted 10 February 2019 - 03:51 PM

btw, if you look at the API documents, you will see that it's actually an error to close a Scanner on System.in
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1