本文介绍了为什么这个错误“java.net.SocketException:Connection reset”在运行服务器和客户端时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我的srever程序 package server_socket; import java.io. *; import java.net.ServerSocket; import java.net.Socket; class Server_socket { public 静态 void main( String [ ] args) throws IOException { try { ServerSocket sk = new ServerSocket( 9999 ); Socket sock = sk .accept(); BufferedReader br = new BufferedReader( new InputStreamReader(sock.getInputStream())); String temp = br.readLine(); System.out.println( 来自客户端的消息是:: + temp) ; PrintStream pr = new PrintStream(sock.getOutputStream()); 字符串 str = 来自服务器的确认 ; pr.println(str); } catch (例外情况) {} } } 我的客户计划 package client_socket; import java.io. *; import java.net.Socket; import java.sql.Connection; import java.sql.DriverManager; import java.sql.Statement; class client_socket { public static void main( String [] args) throws IOException { Connection c = null; 语句stmt = null; 尝试 { Socket sock = new 套接字( 10.176.24.48, 9999 ); PrintStream pr = new PrintStream(sock.getOutputStream()); System.out.println( 输入要发送给服务器的消息); BufferedReader br = new BufferedReader( new InputStreamReader(System.in)); String temp = br.readLine(); pr.println(temp); BufferedReader ed = new BufferedReader( new InputStreamReader(sock.getInputStream())); String st = ed.readLine(); System.out.println(st); Class.forName( org.postgresql.Driver); c = DriverManager .getConnection( jdbc:postgresql://10.176.24.48:5432 / postgres, postgres, ); stmt = c.createStatement(); 字符串 sql = 创建数据库TEST_DB_1编码'UTF8'; stmt.executeUpdate(sql); stmt.close(); c.close(); } catch (Exception ex) { System.err.println(ex.getClass) ()。getName()+ : + ex.getMessage()); System.exit( 0 ); } System.out.println( DBcreated successfully); } } 解决方案 尝试 { []花式代码] } catch (Exception ex) { ex.printStackTrace(); // 使用此 } 这应该可以帮助您查看错误。 大多数情况下,由于防火墙等原因,它是其中一台机器不接受连接 my srever programpackage server_socket;import java.io.*;import java.net.ServerSocket;import java.net.Socket;class Server_socket{ public static void main(String[] args) throws IOException { try { ServerSocket sk = new ServerSocket (9999); Socket sock =sk .accept(); BufferedReader br=new BufferedReader(new InputStreamReader(sock.getInputStream())); String temp=br.readLine(); System.out.println("The mesage from client is::"+temp); PrintStream pr=new PrintStream(sock.getOutputStream()); String str = "acknowledgement from server"; pr.println(str); } catch (Exception ex) { } }}my client programpackage client_socket;import java.io.*;import java.net.Socket;import java.sql.Connection;import java.sql.DriverManager;import java.sql.Statement; class client_socket{ public static void main(String[] args) throws IOException { Connection c = null; Statement stmt = null; try { Socket sock = new Socket("10.176.24.48",9999); PrintStream pr=new PrintStream(sock.getOutputStream()); System.out.println("enter the message to be send to server"); BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); String temp = br.readLine(); pr.println(temp); BufferedReader ed=new BufferedReader(new InputStreamReader(sock.getInputStream())); String st = ed.readLine(); System.out.println(st); Class.forName("org.postgresql.Driver"); c = DriverManager .getConnection("jdbc:postgresql://10.176.24.48:5432/postgres", "postgres", ""); stmt = c.createStatement(); String sql = "CREATE DATABASE TEST_DB_1 ENCODING 'UTF8'"; stmt.executeUpdate(sql); stmt.close(); c.close(); } catch(Exception ex) { System.err.println(ex.getClass().getName()+": "+ex.getMessage()); System.exit(0); } System.out.println("DBcreated successfully"); }} 解决方案 try{[]fancy code]}catch(Exception ex){ ex.printStackTrace(); // use this}That should help you to see the error.Most times it's one of the machines not accepting the connection due to firewall etc. 这篇关于为什么这个错误“java.net.SocketException:Connection reset”在运行服务器和客户端时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 07-31 22:53