深圳大学实验报告课程名称:计算机网络实验项目名称:Socket网络编程学院:计算机与软件学院专业:物联网工程指导教师:崔来中报告人:陈秋明学号:2014150320班级:实验时间:2016.04.20实验报告提交时间:2016.04.29教务处制实验目的学习网络编程基本概念、InetAddress的应用、URL的应用、URLConnection的应用。掌握Socket的TCP通信、Socket的UDP通信掌握通过TCP实现网络文件传输的方法,掌握TCP连接是基于字符流实验环境使用Windows操作系统;Internet连接Eclipse+Java开发环境实验内容:5-1使用InetAddress类的方法获取本地机的名称和IP地址。使用InetAddress类的方法获取网站地址,如果存在多个IP地址,要求全部返回。使用URL类下载深圳大学首页,并统计下载得到网页文件的大小5-2利用Socket类和ServerSocket类,编写一个C/S程序,实现聊天功能。5-3利用Socket类和ServerSocket类,编写一个C/S程序,实现网络文件传输。实验步骤:(用文字描述实验过程,可用截图辅助说明)5-1(1)使用InetAddress类的方法获取本地机的名称和IP地址。packagesocket;importjava.net.*;publicclassone{publicstaticvoidmain(String[]args){try{InetAddressadd=InetAddress.getLocalHost();System.out.println(add);}catch(UnknownHostExceptione){System.out.println(error);}}}(2)使用InetAddress类的方法获取网站地址,如果存在多个IP地址,要求全部返回。packagesocket;importjava.net.*;publicclassone{publicstaticvoidmain(String[]args){try{InetAddressadd=InetAddress.getByName();System.out.println(add);}catch(UnknownHostExceptione){System.out.println(error);}}}(3)使用URL类下载深圳大学首页,并统计下载得到网页文件的大小packagesocket;importjava.net.*;importjava.io.*;publicclassone{publicstaticvoidmain(Stringaregs[])throwsException{URLurl=newURL();URLConnectionuc=url.openConnection();intsize=uc.getContentLength();InputStreamin=url.openStream();FileOutputStreamfout=newFileOutputStream(newFile(qwe.html));inta=0;while(a-1){a=in.read();fout.write(a);}fout.close();System.out.println(文件大小:+size+B);}}5-2(1)编写完整程序;一个服务器端程序,一个客户端程序。服务器端和客户端都需要打印出接受到的消息和发出的命令客户端packageudp;importjava.net.*;importjava.io.*;publicclassClient{publicstaticvoidmain(Stringargs[]){Strings=null;Stringt=time;Stringq=exit;Socketmysocket;DataInputStreamin=null;DataOutputStreamout=null;try{mysocket=newSocket(localhost,4331);in=newDataInputStream(mysocket.getInputStream());out=newDataOutputStream(mysocket.getOutputStream());System.out.println(服务器启动完毕);System.out.println(创建客户连接);out.writeBytes(t);s=in.readUTF();System.out.println(s);Thread.sleep(500);out.writeBytes(q);s=in.readUTF();System.out.println(s);Thread.sleep(500);}catch(IOExceptione){System.out.println(无法连接);}catch(InterruptedExceptione){}}}服务端packageudp;importjava.net.*;importjava.io.*;importjava.util.*;importjava.text.*;publicclassSS{publicstaticvoidmain(Stringargs[]){ServerSockets=null;Socketfuck=null;DataInputStreamin=null;DataOutputStreamout=null;System.out.println(服务器启动完毕);try{s=newServerSocket(4331);}catch(IOExceptione){System.out.println(error+e);}System.out.println(创建客户连接);try{fuck=s.accept();in=newDataInputStream(fuck.getInputStream());out=newDataOutputStream(fuck.getOutputStream());Stringddate=newSimpleDateFormat(yyyy-MM-ddHH:mm:ss).format(Calendar.getInstance().getTime());out.writeUTF(服务器当前时间为:+ddate);Thread.sleep(500);Stringg=Bye;out.writeUTF(g);}catch(IOExceptione){System.out.println(无法连接);}catch(InterruptedExceptione){}}}(()(2)聊天室packagesocket;importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;importjava.net.*;importjava.io.*;importjava.util.*;publicclassChatroomextendsJFrameimplementsActionListener,Runnable,KeyListener{ImageImage;privateTextAreata;privateJTextFieldip;privateJTextFieldport;privateJButtonss_server;privateJButtonss_client;privateJTextFieldsend_text;privateJTextFieldname;privateJButtonss_send;privateJButtonss_exit;privateJButtonss_clear;privateSocketskt;publicvoidkeyReleased(KeyEventf){}publicvoidkeyPressed(KeyEventf){if(f.getKeyCode()==KeyEvent.VK_ENTER){try{PrintWriterpw=newPrintWriter(skt.getOutputStream());Strings1=name.getText();Strings=send_text.getText();if(s==null)return;Calendarca=newGregorianCalendar();inthh=ca.get(Calendar.HOUR_OF_DAY);intmm=ca.get(Calendar.MINUTE);intss=ca.get(Calendar.SECOND);Stringtime=newString(hh+:+mm+:+ss+);ta.append(time+s1+说:+\n+s+\n);Strings3=time+s1+说:+\n+s;pw.println(s3);pw.flush();send_text.setText();}catch(Exceptione){ta.append(发送失败!\n);}}}publicvoidkeyTyped(KeyEventf){}publicvoidrun(){try{BufferedReaderbr=newBufferedReader(newInputStreamReader(skt.getInputStream()));while(true){Strings=br.readLine();if(s==null)break;ta.append(s+\n);}}catch(Exceptione){e.printStackTrace();}}publicvoidactionPerformed(ActionEvente){if(e.getSource()==ss_server){doServer();}if(e.getSource()==ss_client){doClient();}if(e.getSource()==ss_send){doSend();}if(e.getSource()==ss_exit){doexit();}if(e.getSource()==ss_clear){doclear();}}publicvoiddoServer(){try{ServerSocketserver=newServerSocket(Integer.parseInt(port.getText()));skt=server.accept();ta.append(连接服务器成功!\n);newThread(this).start();}catch(Exceptione){ta.append(服务器启动失败!\n);}}publicvoiddoClient(){try{skt=newSocket(ip.getText(),Integer.parseInt(port.getText()));ta.append(连接服务器成功!\n);newThread(this).start();}catch(Exceptione){ta.append(连接失败!\n);}}publicvoiddoSend(){try{PrintWriterpw=newPrintWriter(skt.getOutputStream());Strings1=name.getText();Strings=send_text.getText();if(s==null)return;Calendarca=newGregorianCalendar();inthh=ca.get(Calendar.HOUR_OF_DAY);intmm=ca.get(Calendar.MINU