黑马程序员——网络编程之CTP传输

                                     android培训、java培训、期待与您交流!  
TCP传输
Socket和ServerSocket,实现了两台机器间的套接字端点,绑定本机IP地址。建立客户端和服务端,客户端对应的对象是Socket,服务端对应的对象是ServerSocket。
        //客户端部分
public class ClientA {
private String filePath ;
public String getFilePath() {return filePath;}
public void setFilePath(String filePath ) { thisfilePath = filePath;}
public String getHost() {return host;}
public void setHost(String host ) { thishost = host;}
public int getPort() {return port}
public void setPort(int port) {this.port = port ;}
private String host ;
private int port;
           public static void main (String[] args) {
                    // TODO Auto-generated method stub
ClientA ca=new ClientA();
ca.setHost ("127.0.0.1") ;
ca.setPort (9527) ;
ca.setFilePath ("d:\\") ;
ca.upLoad ("N1.mp3") ;
           }
           //上传方法
           private void upLoad(String fileName ) {
                    // TODO Auto-generated method stub
                   Socket s =null;
                    try {
                             s =new Socket(host ,port );
                             File fi =new File(filePath +fileName );
                             System .out.println( "文件大小:" +fi .length ());
                             FileInputStream fis =new FileInputStream(new File(filePath+fileName)) ;
                             OutputStream os =s .getOutputStream ();
                              int len;
                              byte[] buff =new byte[ 1024];
                              while((len =fis .read (buff)) >0 ){os.write( buff0len);}
                             s .shutdownOutput ();
                              //获取服务器发来的消息。三次握手规则
                             InputStream ip =s .getInputStream ();
                              byte[] buffser =new byte[ 1024];
                             System .out.println( new String(buffser,0,ip.read( buffser)));
                              //关闭资源
                             fis .close ();
                             os .close ();
                             ip .close ();
                             s .close ();
                    } catch (Exception e ) {
                              // TODO: handle exception
                             e .printStackTrace ();
                    }
           }
 
}
//服务端部分,用了服务端继承Thread的办法
public class ServerA extends Thread {
 
           public static void main (String[] args) {
                    // TODO Auto-generated method stub
                   ServerA sa =new ServerA();
                   sa .start ();
           }
           @Override
           public void run(){
 
                    try {
                             ServerSocket ss=new ServerSocket (9527) ;
                             Socket s =ss .accept ();
                              //养成良好习惯,获取对方 ip并记录下来
                             String ip =s .getInetAddress ()getHostAddress();
                             System .out.println( ip">>>conected");
                             File file =new File(ip +".mp3" );
                              //将客户端上传的资料写到存储区
                             FileOutputStream fis =new FileOutputStream(new File("d:\\"file));
                             InputStream ips =s .getInputStream ();
                              int len;
                              byte[] buff =new byte[ 1024];
                              while((len =ips .read (buff)) !=-1 )
                                      fis .write (buff,0,len) ;
                              //通知客户端,上传成功
                             OutputStream ops =s .getOutputStream ();
                             ops .write ("成功上传"getBytes());
                              //养成随手关闭资源的习惯
                             ops .close ();
                             fis .close ();
                             ips .close ();
                             s .close ();
                    } catch (Exception e ) {
                              // TODO: handle exception
                             e .printStackTrace ();
                    }
           }
 
}
                                                         android培训、java培训、期待与您交流!  

转载于:https://www.cnblogs.com/shuawang/p/3731487.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值