Socket Programming 6

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class Test {

public static final int BUFFER_SIZE = 1024 * 50;
private byte[] buffer;

public Test() {
buffer = new byte[BUFFER_SIZE];
}

public void startServer() throws Exception {
System.out.println("In Loop");

ServerSocket socket = new ServerSocket(9000);
Socket client = socket.accept();
System.out.println("In ACCept ");
BufferedInputStream in =
new BufferedInputStream(
new FileInputStream("C:\\vip1.txt"));
System.out.println("Reading File");
BufferedOutputStream out =
new BufferedOutputStream(client.getOutputStream());


int len = 0;
while ((len = in.read(buffer)) > 0) {
out.write(buffer, 0, len);
System.out.print("#");
}
in.close();
out.flush();
out.close();
client.close();
socket.close();
System.out.println("\nDone!");
}

public void startClient() throws Exception {
Socket socket = new Socket("127.0.0.1", 9000);
BufferedInputStream in =
new BufferedInputStream(socket.getInputStream());

BufferedOutputStream out =
new BufferedOutputStream(new FileOutputStream("C:\\temp\\a.txt"));

int len = 0;
while ((len = in.read(buffer)) > 0) {
out.write(buffer, 0, len);
System.out.print("#");
}
in.close();
out.flush();
out.close();
socket.close();
System.out.println("\nDone!");
}


public static void main(String[] args) throws Exception {
Test test = new Test();
if (args.length == 0) {
test.startServer();
} else {
System.out.println("In Loop");
test.startClient();
}
}

}

Post a Comment

أحدث أقدم