Wednesday, August 22, 2007

Clent

//파일부분 전송해서 쓰는거 밖에 못해요..
2대 1 전송은 아직 하나도 못했음..ㅠㅠㅠ
서버쪽에서 또다른 클라이언트가 접속했을때
파일읽어오라고 어떻게 해야할까요?..
쓰레드를 써야하나요? 힘들어서 자요..ㅋㅋ

import java.net.*;
import java.nio.channels.*;
import java.io.*;
import java.nio.*;

public class Client {
static RandomAccessFile InputFile;
static FileChannel InputChannel;
static int divSize = 128;

public static void main(String[] args) {

try{

InputFile = new RandomAccessFile("C:\\Documents and Settings\\mina\\test\\Random\\src\\d.txt","rw");
InputChannel = InputFile.getChannel();

SocketAddress addr = new InetSocketAddress("127.0.0.1", 10001);
SocketChannel socket= SocketChannel.open(addr);
System.out.println(socket);

//서버가 보내온 position읽기
ByteBuffer buf1 = ByteBuffer.allocate(2);
socket.read(buf1);
buf1.flip();
System.out.print("# socket read :");
while(buf1.hasRemaining()){
System.out.print("Piece :"+buf1.get());
}
buf1.clear();
int position = buf1.get();
//파일읽어 서버에게 전송하기
ByteBuffer buf2 = ByteBuffer.allocate(divSize);
InputChannel.position(position);
InputChannel.read(buf2);
buf2.flip();
socket.write(buf2);
buf2.flip();
while(buf2.hasRemaining()){
System.out.print((char)buf2.get());
}
buf2.clear();
}catch(Exception e){
System.out.println(e);
}
}
}

No comments: