---------------------- android培训、java培训、期待与您交流! ----------------------
本节继续研究网络编程
我们还是以TCP网络编程为例,下面的例子是实现从客服端传入一个文件,然后在服务器端接受并保存
/**
*程序2
*该程序是服务端程序
*该程序实现传送一个文件,接收并保存
**/
import java.net.*;
import java.io.*;
class Servicer implements Runnable
{
Socket s;
public Servicer(Socket s)
{
this.s = s;
}
public void run()
{
try
{
//InputStream ips=s.getInputStream();
//OutputStream ops=s.getOutputStream();
//getInputStream public InputStream getInputStream() throws IOException返回此套接字的输入流。
//public BufferedInputStream(InputStream in)创建一个 BufferedInputStream 并保存其参数,即输入流 in,以便将来使用。创建一个内部缓
冲区数组并将其存储在 buf 中。
//public DataInputStream(InputStream in)使用指定的底层 InputStream 创建一个 DataInputStream。
//它们之间的关系是
//java.lang.Object
//java.io.InputStream
// java.io.FilterInputStream
// java.io.DataInputStream
DataInputStream dis = new DataInputStream(new BufferedInputStream(s.getInputStream()));
DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(new FileOutputStream("new1.txt")));//在当前目录下建立文件
byte buf[]=new byte[2048];
int i=0;
while((i=dis.read(buf))!=-1)
{dos.write(buf,0,i); }//将内容写入到new1.txt文件中
dos.flush(); //将缓冲区中的内容存入流中,如果没有此步,数据不会写入文件
dos.close();
dis.close();
s.close();
}catch(Exception e){e.printStackTrace();}
}
}
class TcpServer
{
public static void main(String [] args)
{
try
{
ServerSocket ss=new ServerSocket(8001);//定义端口,8000、8001为通用接口
//ServerSocket ss=new ServerSocket(0);//获取当前接口
//int i=ss.getLocalPort();
System.out.println("sever 正在运行"+"端口号为:"+ss.getLocalPort());
while(true)
{
Socket s=ss.accept();
new Thread(new Servicer(s)).start();
}
//ss.close();
}catch(Exception e){e.printStackTrace();}
}
}
/*
*程序2可能存在的问题
*
(1)Connection reset by peer
执行 第三步的While循环可能产生连接关闭异常,
原因:服务器的并发连接数超过了其承载量,服务器会将其中一些连接Down掉,连接并发数由backlog控制,默认值为5。
backlog: 用于在TCP层接收链接的缓冲池的最大个数,这个个数可在应用层中的listen函数里设置,当客户链接请求大于这个个数(缓冲池满),其它的未
进入链接缓冲池的客户端在tcp层上tcp模块会自动重新链接,直到超时(大约57秒后)
解决:1.增加Byte buf[]=new Byte[len];len的值,以减少连接次数,减少While循环的次数。
2 增加并发连接数的值,把serversocket的backlog设置为200。
(2)dos.flush()不调用此方法,刚数据会存在于缓冲区中,但不会写入文件或对应的流中。
*/
/**
*程序2
*该程序是客户端程序
*该程序实现传送一个文件,接收并保存
*
*/
import java.net.*;
import java.io.*;
public class TcpClient
{
public static void main(String [] args)
{
try
{
//Socket s=new Socket(InetAddress.getByName("192.168.0.213"),8001);
if(args.length < 2)
{
System.out.println("Usage:java TcpClient ServerIP ServerPort");
return;
}
Socket s=new Socket(
InetAddress.getByName(args[0]),Integer.parseInt(args[1]));
//InputStream ips=s.getInputStream();
//OutputStream ops=s.getOutputStream();
//DataInputStream dis= new DataInputStream(new BufferedInputStream(new FileInputStream("1.txt")));//在当前文件下实现
DataInputStream dis= new DataInputStream(new BufferedInputStream(new FileInputStream("d:/1.txt")));//在绝对路径中实现,
如d:/src/Dictionary.txt(or d:\\src\\Dictionary.txt
DataOutputStream dos= new DataOutputStream(new BufferedOutputStream(s.getOutputStream()));//网络输出流。
byte buf[]=new byte[2048];
int i=0;
//while(int i=(dis.read(buf)!=-1))是错误的表达式
//write(buf,0,i)将数据以字节的形式输出到网络输出流,然后传递给server:TecServer.java:s.getInputStream()
while((i=dis.read(buf))!=-1)
{dos.write(buf,0,i); }
dos.flush(); //将缓冲区中的内容存入流中,如果没有此步,数据不会写入文件中
dos.close();
dis.close();
s.close();
}catch(Exception e){e.printStackTrace();}
}
}
---------------------- android培训、java培训、期待与您交流! ----------------------详细请查看:http://edu.youkuaiyun.com/heima