黑马程序员——Java基础->网络编程(TCP)

来源:互联网 发布:shell脚本编程诀窍 编辑:程序博客网 时间:2024/06/06 18:04

——Java培训、Android培训、iOS培训、.Net培训、期待与您交流! ——-

TCP传输

Socket和ServerSocket

  • 建立客户端和服务器端
  • 建立连接后,通过Socket中的IO流进行数 据的传输
  • 关闭socket

同样,客户端与服务器端是两个独立的应用程序。

基本思路(客户端)

  • 客户端需要明确服务器的ip地址以及端口,这样才 可以去试着建立连接,如果连接失败,会出现异 常。
  • 连接成功,说明客户端与服务端建立了通道,那么 通过IO流就可以进行数据的传输,而Socket对象已 经提供了输入流和输出流对象,通过
    getInputStream(),getOutputStream()获取即可。
  • 与服务端通讯结束后,关闭Socket。
class  TcpClient{    public static void main(String[] args) throws Exception     {        //创建客户端的socket服务。指定目的主机和端口        Socket s = new Socket("192.168.1.254",10003);        //为了发送数据,应该获取socket流中的输出流。        OutputStream out = s.getOutputStream();        out.write("tcp ge men lai le ".getBytes());        s.close();    }}

基本思路(服务端)

  • 服务端需要明确它要处理的数据是从哪个 端口进入的。
  • 当有客户端访问时,要明确是哪个客户 端,可通过accept()获取已连接的客户端 对象,并通过该对象与客户端通过IO流进 行数据传输。
  • 当该客户端访问结束,关闭该客户端。
class  TcpServer{    public static void main(String[] args) throws Exception    {        //建立服务端socket服务。并监听一个端口。        ServerSocket ss = new ServerSocket(10003);        //通过accept方法获取连接过来的客户端对象。        while(true)        {        Socket s = ss.accept();        String ip = s.getInetAddress().getHostAddress();        System.out.println(ip+".....connected");        //获取客户端发送过来的数据,那么要使用客户端对象的读取流来读取数据。        InputStream in = s.getInputStream();        byte[] buf = new byte[1024];        int len = in.read(buf);        System.out.println(new String(buf,0,len));        s.close();//关闭客户端.        }        //ss.close();    }}

Tcp传输最容易出现的问题

• 客户端连接上服务端,两端都在等待,没 有任何数据传输。
通过例程分析:
• 因为read方法或者readLine方法是阻塞式。
解决办法:
• 自定义结束标记
• 使用shutdownInput,shutdow

TCP应用场景:
因为TCP是面向连接的,虽然速度稍慢,但保证了安全性,因此可以用于上传下载,大数据的传输等等。

TCP-客户端并发上传图片案例:

import java.io.*;import java.net.*;//客户端class  PicClient{    public static void main(String[] args)throws Exception     {        if(args.length!=1)        {            System.out.println("请选择一个jpg格式的图片");            return ;        }        File file = new File(args[0]);        if(!(file.exists() && file.isFile()))        {            System.out.println("该文件有问题,要么补存在,要么不是文件");            return ;        }        if(!file.getName().endsWith(".jpg"))        {            System.out.println("图片格式错误,请重新选择");            return ;        }        if(file.length()>1024*1024*5)        {            System.out.println("文件过大,没安好心");            return ;        }        Socket s = new Socket("192.168.1.254",10007);        FileInputStream fis = new FileInputStream(file);        OutputStream out = s.getOutputStream();        byte[] buf = new byte[1024];        int len = 0;        while((len=fis.read(buf))!=-1)        {            out.write(buf,0,len);        }        //告诉服务端数据已写完        s.shutdownOutput();        InputStream in = s.getInputStream();        byte[] bufIn = new byte[1024];        int num = in.read(bufIn);        System.out.println(new String(bufIn,0,num));        fis.close();        s.close();    }}class PicThread implements Runnable{    private Socket s;    PicThread(Socket s)    {        this.s = s;    }    public void run()    {        int count = 1;        String ip  = s.getInetAddress().getHostAddress();        try        {            System.out.println(ip+"....connected");            InputStream in = s.getInputStream();            File dir =  new File("d:\\pic");            File file = new File(dir,ip+"("+(count)+")"+".jpg");            while(file.exists())                file = new File(dir,ip+"("+(count++)+")"+".jpg");            FileOutputStream fos = new FileOutputStream(file);            byte[] buf = new byte[1024];            int len = 0;            while((len=in.read(buf))!=-1)            {                fos.write(buf,0,len);            }            OutputStream out = s.getOutputStream();            out.write("上传成功".getBytes());            fos.close();            s.close();        }        catch (Exception e)        {            throw new RuntimeException(ip+"上传失败");        }    }}//服务端class  PicServer{    public static void main(String[] args) throws Exception    {        ServerSocket ss = new ServerSocket(10007);        while(true)        {            Socket s = ss.accept();            //开启线程(达到同时上传的效果)            new Thread(new PicThread(s)).start();        }        //ss.close();    }}
0 0