当前位置: 代码迷 >> J2SE >> 如何读不出来,socket
  详细解决方案

如何读不出来,socket

热度:21   发布时间:2016-04-24 01:08:31.0
怎么读不出来,socket
Java code
public class ClientDemo implements Runnable{    public void run(){        try{            //连接服务器            Socket s=new Socket("127.0.0.1",10033);            //读取本机文件            BufferedReader br=new BufferedReader(new FileReader("j:\\new.txt"));            //获得输出流            System.out.println("获取成功");                    PrintWriter out=new PrintWriter(s.getOutputStream(),true);            //增加标记        //    long time=System.currentTimeMillis();            //DataOutputStream dos=new DataOutputStream(s.getOutputStream());            //dos.writeLong(time);            //像服务器写文件                        String line=null;            while((line=br.readLine())!=null){                out.print(line);            }                        System.out.println(line);            //System.out.println("上传成功");            s.shutdownOutput();//关闭客户端输出流,相当于给个-1;            //获得输入流            BufferedReader Sbr=new BufferedReader(new InputStreamReader(s.getInputStream()));            //获得服务器返回信息            String str=Sbr.readLine();            System.out.println(str);            br.close();            s.close();        }                catch(IOException e){            e.printStackTrace();                    }             }}

Java code
public class ServerDemo implements Runnable{    public void run(){        try {            ServerSocket ss=new ServerSocket(10033);            Socket s=ss.accept();            String ip=s.getInetAddress().getHostAddress();            System.out.println(ip+"....connected.....");            BufferedReader br=new BufferedReader(new InputStreamReader(s.getInputStream()));            //DataInputStream dis=new DataInputStream(s.getInputStream());            //long l=dis.readLong();            PrintWriter out=new PrintWriter(new FileWriter("text.txt"),true);            String line=null;            while((line=br.readLine())!=null){                //if("over".equals(line)){                    //break;                //}                out.print(line);            }            PrintWriter Cout=new PrintWriter(s.getOutputStream());            Cout.write("上传成功");            out.close();            s.close();            ss.close();        } catch (IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }    }}

Java code
public class Demo1 {    /**     * @param args     */    public static void main(String[] args) {        new Thread(new ServerDemo()).start();        new Thread(new ClientDemo()).start();    }}

结果
Java code
获取成功127.0.0.1....connected.....nullnull

问题 1,貌似读取就不成功,更没法复制了
问题2,想清空缓冲区,发现无法调用flush()方法
问题3,如果我把 两线程放于两个类中分别启动就报socket reset异常

------解决方案--------------------
不要使用readlien去读数据
byte[] temp = new byte[1024]; 
sck.getInputStream().read(temp)
(ret = sck.getInputStream().read(temp))!=-1
------解决方案--------------------
这个我刚写了个服务器向客户端传输文件 
发给你看看啊
------解决方案--------------------
[code=import] java.net.*;
import java.io.*;
import java.util.*;

public class TcpServer {

/**
*
*/
public static void main(String[ args) throws Exception {
  相关解决方案