异常
java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:210)
at java.net.SocketInputStream.read(SocketInputStream.java:141)
at java.net.SocketInputStream.read(SocketInputStream.java:127)
at com.psj.TcpServerDemo01.main(TcpServerDemo01.java:24)
代码
public static void main(String[] args){
OutputStream outputStream = null;
Socket socket =null;
try {
//1. which port and ip to connect
InetAddress serverIp = InetAddress.getByName("127.0.0.1");
int port = 9999;
//2. build a socket to connect
socket = new Socket(serverIp, port);
//3. send message
outputStream = socket.getOutputStream();
outputStream.write("你好,这里是服务端".getBytes());
} catch (Exception e){
e.printStackTrace();
}
解决
关闭掉outputStream
if (outputStream!=null){
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}