Java Socket通信发送端与接收端程序代码

下面分别为发送端与接收端的具体实现:

发送端:

import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;


public class Client {

	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		int lenRead=0;
		double sumL=0;
		boolean  IsSuccess=false;
		DataOutputStream dos=null;
		 FileInputStream fis=null;
		 Socket socket=null;
		try{
        File  file=new File("D:/cocosAndpython/workspace/TestSocket/src/TransData.xml");
        long lenth=file.length();
        socket=new Socket();
        socket.connect(new InetSocketAddress("127.0.0.1",48123));
        dos=new  DataOutputStream(socket.getOutputStream());
        fis=new FileInputStream(file);
        byte[] sendBytes=new byte[1024];
        while((lenRead=fis.read(sendBytes, 0, sendBytes.length))>0){
        	sumL+=lenRead;
        	System.out.println("已传输: "+((sumL/lenth)*100)+"%");
        	dos.write(sendBytes,0,(int)lenth);
        }
        dos.flush();
        if(sumL==lenth){  //会自动地进行转换
			IsSuccess=true;
		}
		}catch(Exception e){
			System.out.println("客户端传送文件失败!");
			IsSuccess=false;
			e.printStackTrace();
		}
		finally{
			if(dos!=null){
				dos.close();
			}
			if(fis!=null){
				fis.close();
			}
			if(socket!=null){
				socket.close();
			}
		}
		System.out.println(IsSuccess?"成功":"失败");
	}

}

接收端:

import java.io.DataInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Random;


public class Server {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
     try {
		final ServerSocket server=new ServerSocket(48123);
		Thread thread=new Thread(new Runnable(){

			public void run() {
				// TODO Auto-generated method stub
			 while(true){
				 try{
				 System.out.println("开始监听...");
				 Socket socket=server.accept();
				 System.out.println("有连接!");
				 receiveFile(socket);
			 }catch(Exception e){
				 System.out.println("服务器异常!");
			 }
				
			}
			
		}
		});
		thread.run();
		
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
     
		
		
	}
	public static void receiveFile(Socket socket) throws IOException{
		byte[] receiveByte=null;
		int length=0;
		DataInputStream   dis=null;
		FileOutputStream  fos=null;
		String filePath="D:/temp/"+"ZYQ"+new Random().nextInt(10000)+".xml";
		try{
			dis=new DataInputStream(socket.getInputStream());
			File  file=new File("D:/temp");
			if(!file.exists()){
				file.mkdir();
			}
			fos=new FileOutputStream(new File(filePath));
			receiveByte=new byte [1024];
			System.out.println("开始接受数据...");
			while((length=dis.read(receiveByte,0,receiveByte.length))>0){
				fos.write(receiveByte,0,length);
				fos.flush();
			}
			System.out.println("完成接受: "+filePath);
			
		}
		catch(Exception e){
			e.printStackTrace();
		}
		
		finally{
			if(fos!=null){
				fos.close();
			}
			if(dis!=null){
				dis.close();
			}
			if(socket!=null){
				socket.close();
			}
		}
		
	}

}

其中传输文件TransData.xml内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<Accounts>
 <Account type="type1">
  <code>100001</code>
  <pass>123</pass>
  <name>李四</name>
  <money>1000000.00</money>
 </Account>
 <Account type="type2">
  <code>100002</code>
  <pass>123</pass>
  <name>张三</name>
  <money>1000.00</money>
 </Account>
</Accounts>

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值