栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > .Net

java socket 工具包

.Net 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java socket 工具包

废话1000000字省略.........呵呵......

首先是服务器端:FileServer.java

package com.stars.windsystem.tool.utils;

import java.io.DataInputStream;

import java.io.DataOutputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.IOException;

import java.net.ServerSocket;

import java.net.Socket;

public class FileServer {

public static void main(String[] a) {

FileServer fileServer = new FileServer();

fileServer.sendFile();

}

public void sendFile() {

byte[] buffer = new byte[1024];// 定义一byte类型的缓冲区。

try {

ServerSocket ssocket = new ServerSocket(2000);

System.out.println("服务器启动!");

Socket socket = ssocket.accept();

int length = 0;

File f = new File("d:\aaa.txt");

FileInputStream fis = new FileInputStream(f);

DataInputStream dataIn = new DataInputStream(fis);

DataOutputStream dataOut = new DataOutputStream(socket.getOutputStream());

while ((length = dataIn.read(buffer)) != -1)

{

dataOut.write(buffer, 0, length);

}

dataOut.flush();

dataOut.close();

fis.close();

System.out.println("文件传送完毕!!!");

socket.close();

}

catch (IOException ex)

{

ex.printStackTrace();

}

}

}

其次是 客户端:FileClient.java

package com.stars.windsystem.tool.utils;

import java.io.DataInputStream;

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import java.net.Socket;

public class FileClient {

public static void main(String[] args) {

FileClient fileClient = new FileClient();

fileClient.receiveFile();

}

public void receiveFile() {

byte[] buffer = new byte[1024];

int length = 0;

try {

Socket socket = new Socket("10.40.2.7", 2000);

DataInputStream dataIn = new DataInputStream(socket

.getInputStream());

File f = new File("d:\fff.txt");

if (!f.exists())

f.createNewFile();

FileOutputStream fos = new FileOutputStream(f);

while ((length = dataIn.read(buffer)) != -1) {

fos.write(buffer, 0, length);

}

fos.close();

System.out.println("客户端文件接收完毕!!!");

socket.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

Over!!!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/955296.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号