import java.io.DataOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ConnectException;
import java.net.Socket;
import java.net.UnknownHostException;
public class FileTransferClient {
final static int SIZE = 1024;
public static void main(String[] args) {
String serverIp = "127.0.0.1";
int serverPort = 1209;
System.out
.println("connect to server..." + serverIp + ":" + serverPort);
try {
Socket socket = new Socket(serverIp, serverPort);
OutputStream out = socket.getOutputStream();
DataOutputStream dos = new DataOutputStream(out);
dos.writeInt(1209);
Thread.sleep( 50 );
FileReceiveThread frt;
frt = new FileReceiveThread("pic2.jpg", "127.0.0.1", 1155);
frt.run();
System.out.println("close connection");
socket.close();
} catch (ConnectException ce) {
ce.printStackTrace();
} catch (IOException ie) {
ie.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
class FileReceiveThread extends Thread {
Socket socket;
FileOutputStream fos;
InputStream is;
int serverPort;
String serverIp;
String filePath;
final static int BUFFER_SIZE = 1024 * 4;
public FileReceiveThread(String filePath, String serverIp, int serverPort) {
this.serverPort = serverPort;
this.serverIp = serverIp;
this.filePath = filePath;
try {
fos = new FileOutputStream(filePath);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public void run() {
byte[] buffer;
int i;
buffer = new byte[BUFFER_SIZE];
try {
socket = new Socket(serverIp, serverPort);
while (true) {
is = socket.getInputStream();
i = is.read(buffer);
if (i == -1)
break;
fos.write(buffer, 0, i);
} // while( true )
socket.close();
fos.close();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
이거 돌렸는데
이상하게 java.net.connectexecption : connection refused: connet라고 뜨는데 이거 이유가뭐죠? ㅠㅠ
http://gall.dcinside.com/programming/291568
읽으라고
이것이 자네의 도끼인가?