服务器端连续向客户端发送数据,客户端为何不能连续将数据写入文件啊?
//服务器端
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();
}
}