为什么我在客服端“new socket"时老是报错?
public class FileServer { public boolean connectedClient() throws Exception{ ServerSocket serverSocket = new ServerSocket(8899); Socket socket = null; int count = 0; System.out.println("服务器已启动,正在等待客户端的连接。。。"); try { while (true) { socket = serverSocket.accept(); ServerThread st = new ServerThread(socket); st.start(); count++; InetAddress ia = InetAddress.getLocalHost(); System.out.println("这是第"+count+"个客户!"); System.out.println("连接主机的地址:"+ia.getHostAddress()); } }catch (Exception e){ e.printStackTrace(); } return true; } public static void main(String[] args) throws Exception { FileServer fs = new FileServer(); fs.connectedClient(); } }
public class ServerThread extends Thread { private Socket socket; public ServerThread(Socket socket){ this.socket = socket; } public void run() { InputStream is = null; // OutputStream os = null; InputStreamReader isr = null; BufferedReader br = null; PrintWriter pw = null; String s = null; int count = 0; try { is = socket.getInputStream(); isr = new InputStreamReader(is); br = new BufferedReader(isr); while ((s=br.readLine())!=null){ System.out.print(s); } socket.shutdownInput(); pw = new PrintWriter(socket.getOutputStream()); pw.write("服务器欢迎您"); pw.flush(); socket.shutdownOutput(); } catch (Exception e) { e.printStackTrace(); }finally { try { if (is!=null) is.close(); if (isr!=null) isr.close(); if (br!=null) br.close(); if(pw!=null) pw.close(); }catch (Exception e){ e.printStackTrace(); } } } }
public class FileClient { public static void main(String[] args) throws Exception{ Socket socket = null; OutputStream os = null; PrintWriter pw = null; InputStream is = null; InputStreamReader isr = null; BufferedReader br = null; String serverInfo = null; socket = new Socket("localhost",8800); try { os = socket.getOutputStream(); pw = new PrintWriter(os); pw.write("账号:admin 密码:123"); pw.flush(); socket.shutdownOutput(); is = socket.getInputStream(); isr = new InputStreamReader(is); br = new BufferedReader(isr); while ((serverInfo=br.readLine())!=null){ System.out.println("服务器说:"+serverInfo); } socket.shutdownInput(); }catch (Exception e){ e.printStackTrace(); }finally { try { if (is != null) { is.close(); } if (isr != null) isr.close(); if (br != null) br.close(); if (pw != null) pw.close(); socket.close(); }catch (Exception e){ e.printStackTrace(); } } } }
Exception in thread "main" java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:579)
at java.net.Socket.connect(Socket.java:528)
at java.net.Socket.<init>(Socket.java:425)
at java.net.Socket.<init>(Socket.java:208)
at com.com.filetest.FileClient.main(FileClient.java:19)