package com.itheima.net;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
public class UploadClient {
/**
* @param args
* @throws IOException
* @throws UnknownHostException
*/
public static void main(String[] args) throws UnknownHostException, IOException {
System.out.println("请注意,我要上传文件啦。。。。。");
//创建文件类对象
File fl=new File("D:\\log.txt");
System.out.println(fl.exists());
//1
Socket sk=new Socket("192.168.1.100",10008);
//2.缓存流,提供效率
BufferedReader bufr=new BufferedReader(new FileReader(fl));
PrintWriter out=new PrintWriter(sk.getOutputStream(),true);
String line=null;
while((line=bufr.readLine())!=null){
out.print(line);
}
//3告诉服务器写完啦
sk.shutdownOutput();
out.print("哥们我传完了,检查一下");
//4.
BufferedReader bf=new BufferedReader(new InputStreamReader(sk.getInputStream()));
String str=bf.readLine();
System.out.print(str);
bufr.close();
sk.close();
}
}
打印结果:
请注意,我要上传文件啦。。。。。
true
Exception in thread "main" java.net.ConnectException: Connection timed out: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:69)
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:157)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:391)
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.itheima.net.UploadClient.main(UploadClient.java:30)
最近在学习毕老师的网络编程,蛮有意思的,结果老实报错,不知道是什么原因 |
|