java网络编程复习资料new

java网络编程复习资料new

ID:18759112

大小:56.00 KB

页数:6页

时间:2018-09-22

java网络编程复习资料new_第1页
java网络编程复习资料new_第2页
java网络编程复习资料new_第3页
java网络编程复习资料new_第4页
java网络编程复习资料new_第5页
资源描述:

《java网络编程复习资料new》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库

1、Socket编程客户端代码:publicclassClient{privateStringhost="127.0.0.1";privateintport=8000;privateSocketsocket;/**CreatesanewinstanceofClinet*/publicClient(){try{Socketsocket=newSocket(host,port);InputStreamis=socket.getInputStream();InputStreamReaderisr=newInputStreamReader(is);BufferedReaderbr

2、=newBufferedReader(isr);System.out.println(br.readLine());}catch(UnknownHostExceptione){e.printStackTrace();}catch(IOExceptione){e.printStackTrace();}}publicstaticvoidmain(String[]args){Clientclient=newClient();}}服务器端代码:ServerpublicServer(){try{serverSocket=newServerSocket(port);System.

3、out.println("服务器已启动"+serverSocket.getInetAddress().toString());}catch(IOExceptione){e.printStackTrace();}}publicvoidservice(){while(true){try{Socketsocket=serverSocket.accept();System.out.println("有新连接,IP客户端IP是"+socket.getInetAddress().toString()+",端口是"+socket.getPort()+"");OutputStream

4、os=socket.getOutputStream();PrintWriterpw=newPrintWriter(os);pw.write("欢迎登录!");pw.flush();}catch(IOExceptione){e.printStackTrace();}}}publicstaticvoidmain(String[]args){//TODOcodeapplicationlogichereServerserver=newServer();server.service();}单线程编程:publicclassTalkThreadextendsThread{priv

5、ateintsleeptime;publicTalkThread(){sleeptime=(int)(Math.random()*5000);}publicvoidrun(){System.err.println(this.getName()+"isgoingtostart,itwillsleepfor"+sleeptime+"ms");try{Thread.sleep(sleeptime);}catch(Exceptione){e.printStackTrace();}System.err.println(this.getName()+"wakedup!");}}多

6、线程编程:publicclassMulitiThreadDemo{publicMulitiThreadDemo(){}publicstaticvoidmain(String[]args){TalkThreadtalkThread1=newTalkThread();TalkThreadtalkThread2=newTalkThread();TalkThreadtalkThread3=newTalkThread();TalkThreadtalkThread4=newTalkThread();System.err.println("Threadisstarting...")

7、;talkThread1.start();talkThread2.start();talkThread3.start();talkThread4.start();System.err.println("Threadstarted!");}}JavaMailpublicclassSendMail{publicstaticvoidmain(String[]args)throwsMessagingException{Stringsmtphost="smtp.google.com";Stringuser="test";Stringpwd="123123";S

当前文档最多预览五页,下载文档查看全文

此文档下载收益归作者所有

当前文档最多预览五页,下载文档查看全文
温馨提示:
1. 部分包含数学公式或PPT动画的文件,查看预览时可能会显示错乱或异常,文件下载后无此问题,请放心下载。
2. 本文档由用户上传,版权归属用户,天天文库负责整理代发布。如果您对本文档版权有争议请及时联系客服。
3. 下载前请仔细阅读文档内容,确认文档内容符合您的需求后进行下载,若出现内容与标题不符可向本站投诉处理。
4. 下载文档时可能由于网络波动等原因无法下载或下载错误,付费完成后未能成功下载的用户请联系客服处理。