一、主要涉及的类
Socket
ServerSocket
DataOutputStream
DataInputStream
二、例子
1.发送文本数据
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667importjava.net.*;importjava.io.*;/**作用:使用tcp向服务端发送文本数据时间:2015年5月29日01:42:01步骤:1.创建socket服务2.获取socket的输出流3.使用输出流发送数据4.关闭资源*/classTCPClient01{publicstaticvoidmain(String[]agrs)throwsException{//1.创建socket服务Sockets=newSocket("127.0.0.1",8888);//2.获取输出流DataOutputStreamdos=newDataOutputStream(s.getOutputStream());//3.使用输出流发送数据dos.writeUTF("我是小明!!");dos.writeUTF("我是土豪!!");dos.writeUTF("我是大人物!!");dos.flush();//4.关闭资源dos.close();s.close();}}/**作用:接收客户端发送过来的文本数据时间:2015年5月29日01:59:11步骤:1.创建socket服务端2.获取客户端对象3.获取输入流4.使用输入流读取数据,并打印在控制台上5.关闭资源*/classTCPServer01{publicstaticvoidmain(String[]agrs)throwsException{//1.创建socket服务端ServerSocketss=newServerSocket(8888);//2.获取客户端对象Sockets=ss.accept();//3.获取输入流DataInputStreamdis=newDataInputStream(s.getInputStream());//4.使用输入流读取数据,并打印在控制台上这里我们使用EOFException表示数据读完try{while(true){System.out.println(dis.readUTF());}}catch(EOFExceptione){//表示文件已经读完}//5.关闭资源dis.close();s.close();}}
2.图片上传
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157importjava.net.*;importjava.io.*;/**作用:上传jpg照片到服务器中时间:2015年5月29日02:37:56步骤:1.创建一个socket2.创建一个File(图片)3.把文件的流数据写入到socket的输出流中4.等待服务器的响应5.关闭资源*/classTCPPicLoadClient{publicstaticvoidmain(String[]args){Sockets=null;FileInputStreamfis=null;DataOutputStreamdos=null;DataInputStreamdis=null;try{//1.创建一个sockets=newSocket("127.0.0.1",8888);//2.创建一个文件Filefile=newFile("22.jpg");//3.把文件的流数据写入到socket的输出流中fis=newFileInputStream(file);dos=newDataOutputStream(s.getOutputStream());intlength;byte[]sendBuf=newbyte[1024];System.out.println("发送图片中....");while((length=fis.read(sendBuf,0,sendBuf.length))>0){dos.write(sendBuf,0,length);dos.flush();}//告诉服务端数据已经写完s.shutdownOutput();//4.等待服务器的响应System.out.println("等待服务器的响应中....");dis=newDataInputStream(s.getInputStream());System.out.println(dis.readUTF());}catch(IOExceptione){e.printStackTrace();}finally{try{//5.关闭资源if(fis!=null){fis.close();}if(dos!=null){dos.close();}if(dis!=null){dis.close();}if(s!=null){s.close();}}catch(IOExceptione){e.printStackTrace();}}}}/**作用:接收客户端发送过来的数据时间:2015年5月29日02:57:44步骤:1.创建一个ServerSocket2.获取到Socket对象3.创建一个文件4.把Socket中的流数据写入到文件输出流中5.返回响应给客户端6.关闭资源*/classTCPPicLoadServer{publicstaticvoidmain(String[]args){ServerSocketss=null;Sockets=null;FileOutputStreamfos=null;DataInputStreamdis=null;DataOutputStreamdos=null;try{//1.创建一个ServerSocketss=newServerSocket(8888);//2.获取到Socket对象s=ss.accept();System.out.println("客户端来了:"+s.getRemoteSocketAddress().toString());dis=newDataInputStream(s.getInputStream());dos=newDataOutputStream(s.getOutputStream());//3.创建一个FileFilefile=newFile(System.currentTimeMillis()+".jpg");fos=newFileOutputStream(file);intlength;byte[]recBuf=newbyte[1024];System.out.println("保存图片中....");//4.把Socket中的流数据写入到文件输出流中while((length=dis.read(recBuf,0,recBuf.length))>0){fos.write(recBuf,0,length);fos.flush();}System.out.println("完成保存:"+file.getName());//5.返回响应给客户端dos.writeUTF("上传成功");dos.flush();}catch(IOExceptione){e.printStackTrace();}finally{try{//6.关闭资源if(dis!=null){dis.close();}if(fos!=null){fos.close();}if(s!=null){s.close();}if(ss!=null){ss.close();}}catch(IOExceptione){e.printStackTrace();}}}}
本文提供了TCP客户端与服务器端通过文本数据传输和图片上传的实现方式,包括创建socket、发送数据、接收数据以及图片数据的传输流程。详细步骤覆盖了从创建socket服务到关闭资源的全过程。
1112

被折叠的 条评论
为什么被折叠?



