| 发表于:2007-03-08 18:10:206楼 得分:10 |
import java.net.*; import java.io.*; public class client { private socket you; senddata sends; public client() { try { you = new socket( "localhost ",2007); system.out.println( "连接服务器监听端口:2007...... "); if(you != null) { system.out.println( "连接成功,启动发送线程... "); new thread(new senddata(you)).start(); system.out.println( "发送完毕!关闭线程... "); } }catch(exception e){system.out.println( "服务端运行出错! ");} } public static void main(string args[]) { new client(); } } class senddata implements runnable { private file files; private bufferedreader bin = null; private datainputstream din = null; private dataoutputstream dout = null; private socket mysock = null; private string data = " "; public senddata(socket you) { this.mysock = you; files = new file( "d:\\data.txt "); } public void run() { try { bin = new bufferedreader(new inputstreamreader(new fileinputstream(files))); din = new datainputstream(mysock.getinputstream()); dout = new dataoutputstream(mysock.getoutputstream()); while((data = bin.readline()) != null) { dout.writeint(1); dout.writeutf(data); } dout.writeint(0); clears(); }catch(exception e){} finally { clears(); } } void clears() { try { bin.close(); dout.close(); din.close(); mysock.close(); }catch(exception e){system.out.println( "关闭出错 ");} } } | | |
|