올해는 머신러닝이다.
[펌]Socket을 이용한 서버로 파일 송수신 예제 입니다. 본문
출처 : http://cafe.naver.com/javacircle.cafe?iframe_url=/ArticleRead.nhn%3Farticleid=36277&
Socket을 이용한 서버로 파일 송수신 예제 입니다.
// FileServer.java
import java.net.ServerSocket;
import java.net.Socket;
import java.io.*;
public class FileServer implements Runnable{
private final int PORT = 25573;
public static void main(String[] args) {
new Thread(new FileServer() ).start();
}
public void run() {
ServerSocket s = null;
try {
s = new ServerSocket(PORT);
}
catch (IOException e) {
e.printStackTrace();
}
while (s != null) {
try {
Socket client = s.accept();
System.out.println("client = " + client.getInetAddress());
new Thread( new FileServerClient(client) ).start();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
static class FileServerClient implements Runnable {
private Socket socket;
FileServerClient( Socket s) {
socket = s;
}
public void run() {
try {
BufferedOutputStream out = new BufferedOutputStream( socket.getOutputStream() );
FileInputStream fileIn = new FileInputStream( "/home/warner/yoursourcefile.ext");
byte[] buffer = new byte[8192];
int bytesRead =0;
while ((bytesRead = fileIn.read(buffer)) > 0) {
out.write(buffer, 0, bytesRead);
}
out.flush();
out.close();
fileIn.close();
}
catch (IOException e) {
e.printStackTrace();
}
finally {
try {
socket.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
------------------------------------------------------------------------
// FileClient.java
import java.net.Socket; |
'자바 > 자바팁' 카테고리의 다른 글
java socket 프로그래밍(헤더/본문포함) (0) | 2011.10.27 |
---|---|
[JAVA]Socket Image 전송 (6) | 2011.10.26 |
[펌]Transfer a file via Socket (0) | 2011.10.20 |
[자바]소켓(CLient측) 연결 확인 (0) | 2011.10.20 |
properties to hashmap (0) | 2011.04.11 |