欢迎来到天天文库
浏览记录
ID:18759112
大小:56.00 KB
页数:6页
时间:2018-09-22
《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
此文档下载收益归作者所有