在网上找了孙鑫的视频,然后他的客户端编码:
public static void client() throws UnknownHostException, IOException{
Socket soc=new Socket(InetAddress.getByName(null),6000); OutputStream opStrm=soc.getOutputStream(); InputStream ipStrm=soc.getInputStream(); byte[] buf=new byte[100]; int len=ipStrm.read(buf); System.out.print(new String(buf,0,len)); opStrm.write("Hello,this is zhou!".getBytes()); opStrm.close(); ipStrm.close(); soc.close();

}
服务器端为
public class MapServer extends Thread{
private Socket soc;
public MapServer(Socket soc){
this.soc=soc;
}
public void run(){
try{
OutputStream opStrm=soc.getOutputStream(); BufferedOutputStream bos=new BufferedOutputStream(opStrm); InputStream ipStrm=soc.getInputStream(); opStrm.write("Hello,Welcome you!".getBytes()); bos.write("Hello,Welcome you!".getBytes());

                  bos.flush(); byte[] buf=new byte[100]; int len=ipStrm.read(buf); System.out.println(new String(buf,0,len)); opStrm.close(); ipStrm.close(); soc.close();} catch(Exception ex){
ex.printStackTrace();
}
} public static void server() throws IOException {
ServerSocket sevSoc=new ServerSocket(6000);
//System.out.println("Hello");
while(true){
Socket s=sevSoc.accept();
new MapServer(s).start();
}
//sevSoc.close();
}

public static void main(String[] args) throws IOException{
{
 server();
 

}
}
现在我想传一个文件怎么改上诉代码呢?

解决方案 »

  1.   

    发送文件我已经解决了,大家请看:
    服务器端:
    public class MapServer extends Thread{
    private Socket soc; 
    public MapServer(Socket soc){ 
    this.soc=soc; 

    public void run(){ 
    try{ 

    DataOutputStream out = null;   
    DataInputStream reader = null;

    reader = new DataInputStream(new BufferedInputStream(new FileInputStream("D://MapServer1//POI.MIF")));   

    out = new DataOutputStream(soc.getOutputStream());   

        int bufferSize=2048;
        
    byte[] buf=new byte[bufferSize]; 
     int read = 0;  

         while((read=reader.read(buf)) != -1){   
             out.write(buf, 0, read);   
         }   
            
         out.flush();       System.out.println("数据发送完毕");   
         reader.close();   
         out.close();   
     soc.close();}  catch(Exception ex){ 
    ex.printStackTrace(); 


    }  public static void server() throws IOException { 
    ServerSocket sevSoc=new ServerSocket(6000); 
    System.out.println("服务器启动...");
    while(true){ 
    Socket s=sevSoc.accept(); 
    new MapServer(s).start(); 


    }  public static void main(String[] args) throws IOException{ 

    server(); 
    }  }
    }
    接收端类似,但是这个程序只能发送一个文件,我试着利用这个套路发送第二个文件,结果是第二个文件是空白public class MapServer extends Thread{
    private Socket soc; 
    public MapServer(Socket soc){ 
    this.soc=soc; 

    public void run(){ 
    try{ 

    DataOutputStream out = null;   
    DataInputStream reader = null;

    reader = new DataInputStream(new BufferedInputStream(new FileInputStream("D://MapServer1//POI.MIF")));   

    out = new DataOutputStream(soc.getOutputStream());   

        int bufferSize=2048;
        
    byte[] buf=new byte[bufferSize]; 
     int read = 0;  

         while((read=reader.read(buf)) != -1){   
             out.write(buf, 0, read);   
         }   
            
         out.flush();  
    reader = new DataInputStream(new BufferedInputStream(new FileInputStream("D://MapServer1//POI.MID")));   

    out = new DataOutputStream(soc.getOutputStream());   

           bufferSize=2048;
        
           buf=new byte[bufferSize]; 
    read = 0;  

         while((read=reader.read(buf)) != -1){   
             out.write(buf, 0, read);   
         }   
            
         out.flush();  

         System.out.println("数据发送完毕");   
         reader.close();   
         out.close();   
     soc.close();}  catch(Exception ex){ 
    ex.printStackTrace(); 


    }  public static void server() throws IOException { 
    ServerSocket sevSoc=new ServerSocket(6000); 
    System.out.println("服务器启动...");
    while(true){ 
    Socket s=sevSoc.accept(); 
    new MapServer(s).start(); 


    }  public static void main(String[] args) throws IOException{ 

    server(); 
    }  }
    }
    这个问题出在哪?
      

  2.   

    可以参考下列的例子 
    http://blog.csdn.net/hzhxxx/archive/2009/12/09/4970196.aspx下列描述了JNSP(java  network service platform)服务的部署和功能,如有不明白之处,自己查看源代码,我无力和没有太多时间回答太多的问题.
    源代码一起提供,可以随意修改,发布,并商业化,但请保留作者的信息.为了技术的进步,请注意共享成就.目前已经发布于:http://download.csdn.net/source/1885332;http://hzhxxx.download.csdn.net/
    如果查看后有技术问题探讨和建议的,欢迎致电致信联系.系统架构设计和实现有如下特点:
    A. 采用 Accept-Connect 通讯模型,能统一管理系统中使用的所有 Socket 资源;
    B. 系统支持通信和协议分离的实现原则,支持自定义协议解析模块;通信负责数据收发,协议负责数据鉴别,两者配合实现通信和协议的和谐工作;
    C. 灵活支持业务处理功能重定义,分级支持慢业务和快业务的不同业务处理线程;
    D. 丰富灵活的配置参数,能满足高扩展性;
    E. 配合业务处理和通信模型,能异步的管理所有的交互步骤;
    F. 分布式服务设计和部署,实现动态(热拔插)的增加和较少业务服务器,减少乃至拒绝单点服务;
      

  3.   

    http://hi.csdn.net/link.php?url=http://blog.csdn.net%2Fxianzq888里面有