- <div class="blockcode"><blockquote>java.net.ConnectException: Connection refused: connect
- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- at java.net.DualStackPlainSocketImpl.connect0(Native Method)
- at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
- at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
- at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
- at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
- at java.net.PlainSocketImpl.connect(Unknown Source)
- at java.net.SocksSocketImpl.connect(Unknown Source)
- at java.net.Socket.connect(Unknown Source)
- at java.net.Socket.connect(Unknown Source)
- at java.net.Socket.<init>(Unknown Source)
- at java.net.Socket.<init>(Unknown Source)
- at 文件传输.main(文件传输.java:11)
- Exception in thread "Thread-0" java.lang.NullPointerException
- at 服务端.run(文件传输.java:75)
- at java.lang.Thread.run(Unknown Source)
复制代码
import java.io.*;
import java.net.*;
public class 文件传输 {
public static void main(String[] args) {
Socket socket=null;
ServerSocket ss=null;
try {
socket=new Socket(InetAddress.getByName("127.0.0.1"),10001);
ss=new ServerSocket(10001);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
new Thread(new 服务端(ss)).start();
// 为了让服务端先开启,main线程停10s
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("**********************************************************************");
new Thread(new 客户端(socket,"G:\\test\\test.txt")).start();
}
}
class 客户端 implements Runnable {
private Socket socket;
private String path;
客户端(Socket socket, String path) {
this.socket = socket;
this.path=path;
}
@Override
public void run() {
BufferedInputStream br = null;
OutputStream os = null;
try {
os = socket.getOutputStream();
br = new BufferedInputStream(new FileInputStream(path));
byte[] buf = new byte[1024];
int len = 0;
while ((len = br.read(buf)) != -1) {
os.write(buf, 0, len);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
class 服务端 implements Runnable{
private ServerSocket ss;
private Socket socket;
服务端(ServerSocket ss){
this.ss=ss;
}
@Override
public void run() {
// TODO Auto-generated method stub
BufferedInputStream is=null;
BufferedOutputStream os=null;
try {
socket=ss.accept();
is=new BufferedInputStream(socket.getInputStream());
os=new BufferedOutputStream(new FileOutputStream("G:\\test\\test1.txt"));
byte[] buf=null;
int len=0;
while((len=is.read(buf))!=-1){
os.write(buf, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
|
|