问题描述
- java网络编程错误java.net.ConnectException: Connection refused: connect求教
-
刚开始学习java网络编程,在书上看到一段代码,就动手试一下,结果出现下面错误:
java.net.ConnectException: Connection refused: connect
源代码如下:
import java.io.*;
import java.net.*;class lx01 {
public static void main(String[] args) {
String host = "localhost";BufferedReader br = null; PrintWriter pw = null; Socket s = null; try { s = new Socket(host, 10000); InputStreamReader isr; isr = new InputStreamReader(s.getInputStream()); br = new BufferedReader(isr); pw = new PrintWriter(s.getOutputStream(), true); pw.println("DATE"); System.out.println(br.readLine()); pw.println("PAUSE"); pw.println("DOW"); System.out.println(br.readLine()); pw.println("DOM"); System.out.println(br.readLine()); pw.println("DOY"); System.out.println(br.readLine()); } catch (IOException e) { System.out.println(e.toString()); } finally { try { if (br != null) br.close(); if (pw != null) pw.close(); if (s != null) s.close(); } catch (IOException e) { } } }
}
求教各位大神这是啥原因,如何解决?
解决方案
首先这个一个网络编程的题目,在一般情况下,你的需要编服务端和客户端两个部分,现在你粘出到的为客户端的代码,如果你只编写的客户端的代码在一般情况下是不会成功的,因为没有服务端
时间: 2024-12-31 04:54:32