| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 376 人关注过本帖
标题:还是TCP协议
取消只看楼主 加入收藏
whhguo
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:42
专家分:119
注 册:2012-11-21
结帖率:100%
收藏
 问题点数:0 回复次数:1 
还是TCP协议
现在传东西不会丢失了,但我想我文件的名字传过去,接受的时候该怎么写?
客户端:
package jj;

import
import
import
import
import
import
import
import
import
import

public class TcpOneToManyClient {
    public static void main(String[] args) throws UnknownHostException, IOException {
        Socket socket = new Socket("127.0.0.1", 7751);
        DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
        String filename="D://myeclips//新建文件夹(2)//XuLieHua//bin//com//guo//big//Student.class";
        DataInputStream dis = new DataInputStream(new BufferedInputStream(new FileInputStream(filename)));
        int c = 0;
        
     //   dos.writeBytes(filename);
        while((c = dis.read()) != -1) {
            dos.write(c);
        }
        dos.close();
        dis.close();
    }
}
服务器端:
package jj;

import
import
import
import
import
import
import
import
import
import
public class TcpOneToManyServer {

    public static int socketNumber = 1;
    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket(7751);
        System.out.println("Server is running");
        while(true) {
            Socket socket = serverSocket.accept();
            SocketHandle socketHandle = new SocketHandle(socket, socketNumber++);
            socketHandle.start();
        }
    }
}


 class SocketHandle extends Thread {
    private Socket socket;
    private int socketNumber;
    public SocketHandle(Socket socket, int socketNumber) {
        this.socket = socket;
        this.socketNumber = socketNumber;
    }
    public void run(){
        try {
        DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(new FileOutputStream("D://" + socketNumber + ".class")));
        DataInputStream dis = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
        PrintWriter pw = new PrintWriter(socket.getOutputStream());
        //int str=dis.readInt();
    //    System.out.println(str);
   
        int c = 0;
        while((c = dis.read()) != -1) {
            dos.write(c);
        }
        pw.println("aa");
        pw.flush();
        pw.close();
        dos.close();
        dis.close();
        
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
}
搜索更多相关主题的帖子: 客户端 package public import 
2012-11-30 21:17
whhguo
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:42
专家分:119
注 册:2012-11-21
收藏
得分:0 
不用大神们看了,已经搞好了哈
2012-12-01 11:07
快速回复:还是TCP协议
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017519 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved