A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

import java.io.*;
import java.net.*;
class Consumerp
{
        public static void main(String[] args)throws Exception//建立客户端点
        {
                File file=new File("C:\\1.txt");
                Socket s=new Socket(InetAddress.getLocalHost(),911);//创建套接字并指定ip地址与端口标识
                BufferedOutputStream bo=new BufferedOutputStream(s.getOutputStream());
                BufferedInputStream bi=new BufferedInputStream(new FileInputStream(file));
                byte[] arr=new byte[1024];
                int num=0;
                while((num=bi.read(arr))!=0)
                {
                        bo.write(arr,0,num);
                }
                s.shutdownOutput();//禁用此套接字的输出流
                BufferedReader b=new BufferedReader(new InputStreamReader(s.getInputStream()));
                System.out.println(b.readLine());
                s.close();
                bi.close();
        }
}
class Serverp implements Runnable//实现多线程,并复写run方法
{
        private Socket s;
        Serverp(Socket s)
        {
                this.s=s;
        }
        public void run()
        {
                try
                {
                        System.out.println(s.getLocalAddress().getHostAddress()+".....连接此服务器成功");
                        BufferedInputStream bi=new BufferedInputStream(s.getInputStream());
                        BufferedOutputStream bo=new BufferedOutputStream(new FileOutputStream("D:\\aa\\day24\\add\\d1.txt"));
                        byte[] arr=new byte[1024];
                        int num=0;
                        while((num=bi.read(arr))!=0)//此处出现阻塞性,一直等着读
                        {
                                bo.write(arr,0,num);
                        }
                        PrintWriter pw=new PrintWriter(s.getOutputStream(),true);//用true刷新字符流
                        pw.println(new String("上传成功"));       
                        bo.close();
                        s.close();
                }
                catch (Exception e)
                {
                        throw new RuntimeException();
                }
               
        }
}
class ServiceTotal //建立多线程,建立服务器端点
{
        public static void main(String[] args)throws IOException
        {
                ServerSocket ss=new ServerSocket(911);
                while(true)
                {
                        Socket s=ss.accept();
                        new Thread(new Serverp(s)).start();//一接收到一个客户端点,就建立一个线程
                }
        }
}

评分

参与人数 1技术分 +1 收起 理由
滔哥 + 1

查看全部评分

5 个回复

倒序浏览
把你的Java报异常问题贴出来看下
回复 使用道具 举报
刘敏 发表于 2013-12-6 22:58
把你的Java报异常问题贴出来看下

数组角标越位异常
回复 使用道具 举报
在哪行发生的  是否每次运行都在那里发生异常
回复 使用道具 举报
刘敏 发表于 2013-12-6 23:32
在哪行发生的  是否每次运行都在那里发生异常

已经通过别人找的答案,不过我还有个问题是字节流需要使用flush()刷新吗
回复 使用道具 举报
真是个大坑 ,看了好久,才发现是代码细节的错误。
while((num=bi.read(arr))!=0)
while((num=bi.read(arr))!=0)//此处出现阻塞性,一直等着读
就这两行代码出错,怎么是!= 0呢,明显读到没有之后返回-1,你这样永远跳不出循环了。
另外刷新总是加上好的,当数据较大,可能会溢出。

评分

参与人数 1技术分 +1 收起 理由
简★零度 + 1

查看全部评分

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马