客户端控制台总显示异常:java.net.ConnectException: Connection refused: connect
/**
* 服务端
*/
public class Service {
public static void main(String []args){
try {
ServerSocket server = new ServerSocket(9111);
Socket socket = server.accept(); //阻塞
InputStream in = socket.getInputStream();
byte[] b = new byte[1024];
OutputStream out = new FileOutputStream("e:\\1.txt");
while (in.read(b)!=-1) {
out.write(b);
out.flush();
}
out.close();
System.out.println("文件接收结束");
} catch (IOException e) {
e.printStackTrace();
}
}
}/**
* 客户端
*/
public class Client2 {
public static void main(String[] args) {
OutputStream out = null;
try {
Socket socket = new Socket("127.0.0.1",8888);
InputStream in = socket.getInputStream();
out = new FileOutputStream("e:\\123.txt");
byte[]data = new byte[1024];
int length = 0;
while((in.read(data))!=-1){
out.write(data );out.flush();
}
out.close();
System.out.println("文件接收结束...");
socket.close();
System.out.println("客户端结束");
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 服务端
*/
public class Service {
public static void main(String []args){
try {
ServerSocket server = new ServerSocket(9111);
Socket socket = server.accept(); //阻塞
InputStream in = socket.getInputStream();
byte[] b = new byte[1024];
OutputStream out = new FileOutputStream("e:\\1.txt");
while (in.read(b)!=-1) {
out.write(b);
out.flush();
}
out.close();
System.out.println("文件接收结束");
} catch (IOException e) {
e.printStackTrace();
}
}
}/**
* 客户端
*/
public class Client2 {
public static void main(String[] args) {
OutputStream out = null;
try {
Socket socket = new Socket("127.0.0.1",8888);
InputStream in = socket.getInputStream();
out = new FileOutputStream("e:\\123.txt");
byte[]data = new byte[1024];
int length = 0;
while((in.read(data))!=-1){
out.write(data );out.flush();
}
out.close();
System.out.println("文件接收结束...");
socket.close();
System.out.println("客户端结束");
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
解决方案 »
- 完全平方数问题
- 小弟求教问题
- 两个线程的小程序,但不知哪里出错了。大家顺便能解释一下吗??
- 如何去掉FCKEditor编辑框内自动给网址加链接的功能?
- 一个关于继承的问题~求助..
- java 1.4中如何获取环境变量~!!~急~!
- 高手请进:面试题:有三种硬币,1角钱,2角钱,5角钱,数量不限,打印出所有相加等于1元的组合,不准重复。
- 如何快速从list中查找一条数据
- 请教怎样透过代理服务器访问URL,目前我的程序只能在不使用代理服务器的环境中使用?
- 明天我帮别人考JAVA,但我不是学这个的,现在有几个例子请大家帮忙做一做。急!!!
- 麻烦帮忙看一个sql 错误
- 请问Timer类定时器如果报错了怎样重新启动这个任务?
InputStream in = null;
try{
ServerSocket service = new ServerSocket(8888);
Socket socket = service.accept();//阻塞
OutputStream out = socket.getOutputStream();
byte [] data = new byte[1024];
//开始读取文件
in = new FileInputStream("e:\\SNET.txt");
while (in.read(data)!=-1) {
out.write(data,0,1024);
out.flush();
}
in.close();
System.out.println("传送文件结束...");
service.close();
System.out.println("服务器关闭");
} catch (IOException e) {
e.printStackTrace();
}
}
}