Java.net.SocketException:Connection Reset exception for Java network programming

Source: Internet
Author: User
Tags connection reset

I didn't pay attention to the flush () method of write when I first learned stream, and today when I was learning Network programming, I noticed

Server-side:

Import Java.net.*;import java.io.*;p ublic class  client{public static void Main (string[] args) throws Exception { Socket socket = new socket ("localhost", 8888); Bufferedoutputstream out  = new Bufferedoutputstream (Socket.getoutputstream ()); byte[] buff = new Byte[2048];for ( int i = 0; I < 2048; i + +) {Buff[i] = ' a ';} Out.write (buff); Out.flush (); Out.close (); Socket.close (); System.out.println ("Send over!");}}

Client:

Import Java.net.*;import java.io.*;p ublic class  client{public static void Main (string[] args) throws Exception { Socket socket = new socket ("localhost", 8888); Bufferedoutputstream out  = new Bufferedoutputstream (Socket.getoutputstream ()); byte[] buff = new Byte[2048];for ( int i = 0; I < 2048; i + +) {Buff[i] = ' a ';} Out.write (buff); Out.flush (); Out.close (); Socket.close (); System.out.println ("Send over!");}}

When a client lacks a flush statement and close is not closed

----------Java----------
Wait for connection ...
Exception in thread "main" java.net.SocketException:Connection Reset
At Java.net.SocketInputStream.read (Unknown Source)
At Java.net.SocketInputStream.read (Unknown Source)
At Java.io.BufferedInputStream.fill (Unknown Source)
At Java.io.BufferedInputStream.read1 (Unknown Source)
At Java.io.BufferedInputStream.read (Unknown Source)
At Java.io.FilterInputStream.read (Unknown Source)
At Server.main (server.java:13)


Output complete (5 seconds)-Normal termination

Java.net.SocketException:Connection Reset exception for Java network programming

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.