Java利用TCP协议发送文件(一)--服务器端

      最近要写一个使用TCP进行文件传输的软件,考虑客户端是使用手机的,因此,准备采用Java作为开发语言,自己本身对于Java本不熟,还好,有个热心的网友已经将TA的代码公开了,有了这个基本原型,就可以进行进一步开发了。



    服务器端采用的是TCP server socket监听端口,如果有客户端连接到服务器,就创建一个新的线程进行处理,在这个线程中,只接受客户端发送过来的数据,并保存发送过来的数据到指定文件中。
    代码如下:

设置TCP Server Socket监听端口的类:
/**
* 
*/
package com.sheng.Server;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

/**
* @author sheng
*
*/
public class Server {

     /**
     * @param args
     * @throws IOException 
     */
     public static void main(String[] args) throws IOException 
     {
          // TODO Auto-generated method stub
          
          // TCP 服务器监听6666端口
          ServerSocket socket = new ServerSocket(6666);
          Socket client = null;
          
          while (true)
          {
               // 接收客户端连接
               client = socket.accept();
               System.out.println("Accept clinte.");
               
               // 处理客户端的连接
               new Thread(new RecivingDataThread(client)).start();     
          }
          

     }

}


    

   处理客户端请求的线程类:
/**
* 
*/
package com.sheng.Server;


import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import java.net.Socket;



/**
* @author sheng
*
*/
public class RecivingDataThread implements Runnable
{
     private Socket Client = null;
     public static int NumberOfThreads = 0;
     
     public RecivingDataThread(Socket Client)
     {
          this.Client = Client;
          NumberOfThreads++;
     }
     
     
     public void run() 
     {
          
          BufferedInputStream SocketInput = null;
          
          
          // TODO Auto-generated method stub
          try 
          {
               SocketInput = new BufferedInputStream(Client.getInputStream());
          }
          catch (IOException e2) 
          {
               // TODO Auto-generated catch block
               e2.printStackTrace();
          }
     
          
          // 接收文件的名字
          FileNamePackage Name = new FileNamePackage(true);
          if (Name.Receive(SocketInput) != -1) {
               System.out.println("The name is " + Name.GetFileName());
          }
          else {
               System.out.println("Cannot read the data.");
          }

          String ReceiveName = Name.GetFileName();
          
          
          // 打开要写入的文件
          FileOutputStream fos = null;
          try {
               fos = new FileOutputStream("D:/" + ReceiveName);
          }
          catch (FileNotFoundException e1) {
               // TODO Auto-generated catch block
               e1.printStackTrace();
          }
          BufferedOutputStream bos = new BufferedOutputStream(fos);

          System.out.println("Reading data.");


          // 接收文件数据
          FileDataPackage Data = new FileDataPackage(true);
          while (Data.Receive(SocketInput) != -1) {
               Data.Write(bos);
          }


          // 关闭输出的文件和Socket
          try
          {
          bos.close();
          fos.close();
          SocketInput.close();
          Client.close();
          }
          catch (IOException e)
          {
               e.printStackTrace();
          }
          
          System.out.println("client socket is end.");

          
     }

}








  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值