1. 현재 작업 전송 방법 : 순차적인 방법
makeSocket("127.0.0.1",10003,10,110);
makeSocket("127.0.0.1",10002,0,10);
-> 위와 같은 전송 방법은 첫번째 전송에서 문제가 발생하면 두번째 전송을 수행하지 않음.
따라서 이를 쓰레드로 만들어서 처리함.(멀티쓰레드, 쓰레드풀)
2. 돌프야 어여 모바일쪽 서버 접속 프로그램 처리해야지?????
Monday, August 27, 2007
Sunday, August 26, 2007
Peer_1
Peer_2 도 똑같음..ㅋㅋ
토큰안하고 그냥 랭스랑 포지션 같이 보내는걸로 했어요..ㅋㅋ
import java.net.*;
import java.nio.channels.*;
import java.io.*;
import java.nio.*;
public class Peer_1 {
static int Port = 10001;
static FileChannel InputChannel;
static RandomAccessFile InputFile;
static ServerSocketChannel server;
static ServerSocket serverSocket;
static boolean flag = false;
static int length = 0;
static int position = 0;
private static void makeSocket() throws IOException{
//소켓생성
server = ServerSocketChannel.open();
serverSocket = server.socket();
SocketAddress addr = new InetSocketAddress(Port);
serverSocket.bind(addr);
System.out.println("[Peer1]:" + server + "접속을 기다립니다");
SocketChannel socket = server.accept();
readInfo(socket);
sendFile(socket);
serverSocket.close();
}
private static void makeFileChannel(String filename) throws FileNotFoundException{
//File,Filechannel 생성
InputFile = new RandomAccessFile(filename,"rw");
InputChannel = InputFile.getChannel();
}
private static void readInfo(SocketChannel socket) throws IOException{
//Client로부터 요청된 position 읽기
ByteBuffer readBuf = ByteBuffer.allocate(3);
socket.read(readBuf);
readBuf.flip();
System.out.print("#read[From Clent]:" );
while(readBuf.hasRemaining()){
System.out.print("Position :"+readBuf.get());
}
readBuf.flip();
System.out.print("\n");
byte[] b = new byte[2];
readBuf.get(b);
System.out.println("P:"+ b[0]+" s:"+ b[1]);
readBuf.clear();
position = b[0];
length = b[1];
}
private static void sendFile(SocketChannel socket) throws IOException{
//파일읽어 Clietn에게 전송하기
ByteBuffer writeBuf = ByteBuffer.allocate(length);
InputChannel.position(position);
InputChannel.read(writeBuf);
writeBuf.flip();
socket.write(writeBuf);
writeBuf.flip();
while(writeBuf.hasRemaining()){
System.out.print((char)writeBuf.get());
}
System.out.print("\n");
writeBuf.clear();
}
public static void main(String[] args){
try{
while(!flag){
String filename = "C:\\Documents and Settings\\mina\\test\\Buffer\\src\\a.txt";
makeFileChannel(filename);
makeSocket();
}
}catch(Exception e){
e.printStackTrace();
}finally{
try{
serverSocket.close();
server.close();
InputFile.close();
InputChannel.close();
}catch (Exception e) {
e.printStackTrace();
}
}
}
}
토큰안하고 그냥 랭스랑 포지션 같이 보내는걸로 했어요..ㅋㅋ
import java.net.*;
import java.nio.channels.*;
import java.io.*;
import java.nio.*;
public class Peer_1 {
static int Port = 10001;
static FileChannel InputChannel;
static RandomAccessFile InputFile;
static ServerSocketChannel server;
static ServerSocket serverSocket;
static boolean flag = false;
static int length = 0;
static int position = 0;
private static void makeSocket() throws IOException{
//소켓생성
server = ServerSocketChannel.open();
serverSocket = server.socket();
SocketAddress addr = new InetSocketAddress(Port);
serverSocket.bind(addr);
System.out.println("[Peer1]:" + server + "접속을 기다립니다");
SocketChannel socket = server.accept();
readInfo(socket);
sendFile(socket);
serverSocket.close();
}
private static void makeFileChannel(String filename) throws FileNotFoundException{
//File,Filechannel 생성
InputFile = new RandomAccessFile(filename,"rw");
InputChannel = InputFile.getChannel();
}
private static void readInfo(SocketChannel socket) throws IOException{
//Client로부터 요청된 position 읽기
ByteBuffer readBuf = ByteBuffer.allocate(3);
socket.read(readBuf);
readBuf.flip();
System.out.print("#read[From Clent]:" );
while(readBuf.hasRemaining()){
System.out.print("Position :"+readBuf.get());
}
readBuf.flip();
System.out.print("\n");
byte[] b = new byte[2];
readBuf.get(b);
System.out.println("P:"+ b[0]+" s:"+ b[1]);
readBuf.clear();
position = b[0];
length = b[1];
}
private static void sendFile(SocketChannel socket) throws IOException{
//파일읽어 Clietn에게 전송하기
ByteBuffer writeBuf = ByteBuffer.allocate(length);
InputChannel.position(position);
InputChannel.read(writeBuf);
writeBuf.flip();
socket.write(writeBuf);
writeBuf.flip();
while(writeBuf.hasRemaining()){
System.out.print((char)writeBuf.get());
}
System.out.print("\n");
writeBuf.clear();
}
public static void main(String[] args){
try{
while(!flag){
String filename = "C:\\Documents and Settings\\mina\\test\\Buffer\\src\\a.txt";
makeFileChannel(filename);
makeSocket();
}
}catch(Exception e){
e.printStackTrace();
}finally{
try{
serverSocket.close();
server.close();
InputFile.close();
InputChannel.close();
}catch (Exception e) {
e.printStackTrace();
}
}
}
}
Client
import java.net.*;
import java.nio.channels.*;
import java.io.*;
import java.nio.*;
public class Client {
static RandomAccessFile OutputFile;
static FileChannel OutputChannel;
private static void makeSocket(String IP,int Port,int position,int length) throws IOException{
SocketAddress addr = new InetSocketAddress(IP,Port);
SocketChannel socket = SocketChannel.open(addr);
System.out.println("[peer socket]: "+ socket);
sendInfo(socket,position,length);
readSocket(socket,length,position);
closeSocketChannel(socket);
}
private static void sendInfo(SocketChannel socket,int position,int length) throws IOException{
byte[] b = new byte[2];
b[0]= (byte) position;
b[1]= (byte) length;
ByteBuffer Info = ByteBuffer.allocate(b.length);
Info.put(b);
Info.flip();
socket.write(Info);
System.out.println("#write Info[To Peer)]:"+ b[0]+ " " + b[1]);
Info.clear();}
private static void readSocket(SocketChannel socket,int length,int position) throws IOException{
//Peer로부터 전송된 파일 읽기
ByteBuffer readBuf = ByteBuffer.allocate(length);
socket.read(readBuf);
readBuf.flip();
System.out.print("#read [From Peer]:" );
while(readBuf.hasRemaining()){
System.out.print((char)readBuf.get());
}
System.out.print("\n\n");
readBuf.flip();
writeFile(readBuf,position);
}
private static void makeFileChannel(String filename) throws FileNotFoundException{
OutputFile = new RandomAccessFile(filename,"rw");
OutputChannel = OutputFile.getChannel();
}
private static void writeFile(ByteBuffer readBuf,int position) throws IOException{
//읽은 내용 파일에 쓰기
OutputChannel.position(position);
OutputChannel.write(readBuf);
readBuf.clear();
}
private static void closeSocketChannel(SocketChannel socket) throws IOException{
socket.close();
}
public static void main(String[] args) {
try{
String filename = "C:\\Documents and Settings\\mina\\test\\Buffer\\src\\a_copy.txt";
makeFileChannel(filename);
makeSocket("127.0.0.1",10001,10,110);
makeSocket("127.0.0.1",10002,0,10);
}catch(Exception e){
System.out.println(e);
}finally{
try{
OutputFile.close();
OutputChannel.close();
}catch (Exception e) {
e.printStackTrace();
}
}
}
}
import java.nio.channels.*;
import java.io.*;
import java.nio.*;
public class Client {
static RandomAccessFile OutputFile;
static FileChannel OutputChannel;
private static void makeSocket(String IP,int Port,int position,int length) throws IOException{
SocketAddress addr = new InetSocketAddress(IP,Port);
SocketChannel socket = SocketChannel.open(addr);
System.out.println("[peer socket]: "+ socket);
sendInfo(socket,position,length);
readSocket(socket,length,position);
closeSocketChannel(socket);
}
private static void sendInfo(SocketChannel socket,int position,int length) throws IOException{
byte[] b = new byte[2];
b[0]= (byte) position;
b[1]= (byte) length;
ByteBuffer Info = ByteBuffer.allocate(b.length);
Info.put(b);
Info.flip();
socket.write(Info);
System.out.println("#write Info[To Peer)]:"+ b[0]+ " " + b[1]);
Info.clear();}
private static void readSocket(SocketChannel socket,int length,int position) throws IOException{
//Peer로부터 전송된 파일 읽기
ByteBuffer readBuf = ByteBuffer.allocate(length);
socket.read(readBuf);
readBuf.flip();
System.out.print("#read [From Peer]:" );
while(readBuf.hasRemaining()){
System.out.print((char)readBuf.get());
}
System.out.print("\n\n");
readBuf.flip();
writeFile(readBuf,position);
}
private static void makeFileChannel(String filename) throws FileNotFoundException{
OutputFile = new RandomAccessFile(filename,"rw");
OutputChannel = OutputFile.getChannel();
}
private static void writeFile(ByteBuffer readBuf,int position) throws IOException{
//읽은 내용 파일에 쓰기
OutputChannel.position(position);
OutputChannel.write(readBuf);
readBuf.clear();
}
private static void closeSocketChannel(SocketChannel socket) throws IOException{
socket.close();
}
public static void main(String[] args) {
try{
String filename = "C:\\Documents and Settings\\mina\\test\\Buffer\\src\\a_copy.txt";
makeFileChannel(filename);
makeSocket("127.0.0.1",10001,10,110);
makeSocket("127.0.0.1",10002,0,10);
}catch(Exception e){
System.out.println(e);
}finally{
try{
OutputFile.close();
OutputChannel.close();
}catch (Exception e) {
e.printStackTrace();
}
}
}
}