有关socket的问题,为何客户端无法将数据连续写入文件啊

服务器端连续向客户端发送数据,客户端为何不能连续将数据写入文件啊?
//服务器端
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;


public class ss {

	public static void main(String[] args) {
		ServerSocket ss;
		Socket s;
		/*byte [] bp=new byte[6];
		byte [] stop=new byte[1];*/
		int port=8887;
		
		try {
			/*System.out.println("请输入端口号");
			System.in.read(bp);
			port=Integer.parseInt(new String(bp).trim());*/
			
			ss=new ServerSocket(port);
			s=ss.accept();
			while(true)
			{
				
				s.getOutputStream().write("12ab12cd".getBytes());
				/*System.in.read(stop);
				if((char)stop[0]=='0')
				{
					break;
				}*/
				
			}
		} catch (IOException e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		}

	}

}
//客户端
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;


public class Sockett implements Runnable
{
	String   host   =   "127.0.0.1";   
    int   port   =   8887,i;   
    Socket   socket;   
      
    InputStream   is ;   
    FileOutputStream out ;
    byte [] b=new byte[5000];
    byte [] bb=new byte[1];
    byte [] bh=new byte[16];
	byte [] bp=new byte[6];
    Thread th1;
    
    public Sockett()
    {
    	th1=new Thread(this);
    }
	
	public void run()
	{
		try {
//			System.out.println("请输入IP地址");
//			System.in.read(bh);
//			host=new String(bh).trim();
//			//System.out.println("host is"+host);
//			System.out.println("请输入端口号");
//			System.in.read(bp);
//			port=Integer.parseInt(new String(bp).trim());
			//System.out.println("port is"+port);
			
		  	  socket=new Socket(host,port);
				is=socket.getInputStream();
				out=new FileOutputStream(new File("d:\\\\t2"),true);
				while(true)
				{
					if(is.available()>-1)
					{
					i=is.read(b);
					out.write(b);
					
					}
					System.in.read(bb);
					char c=(char)bb[0];
					if(c=='0')
					{
						break;
					}
				}
				out.flush();
				is.close();
				out.close();
				socket.close();
				System.out.println("stop");
				
			} catch (IOException e) {
				// TODO 自动生成 catch 块
				e.printStackTrace();
			}
		
	}

}
public class Start {

	
	public static void main(String[] args) {
			
		Sockett stt=new Sockett();
		stt.th1.start();
		
	}

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值