자바 socket 프로 그래 밍 모음 집 2
import java.io.*;
import java.net.*;
/**
* ServerSocket( Exception)。
* ServerSocket accept() 。
* Socket 。
* 。
* Socket 。
* ( , Writer Reader)。
* @author Administrator
*
*/
public class RemoteFileServer {
int listenPort;
public RemoteFileServer(int listenPort) {
this.listenPort=listenPort;
}
/**
* ServerSocket
*/
public void acceptConnections() {
try {
ServerSocket server = new ServerSocket(listenPort);
Socket incomingConnection = null;
while(true) {
//accept()
// setSoTimeout() accept() , 。
// setSoTimeout() accept() IOException
//accept() Socket, Socket
incomingConnection = server.accept();
// Socket handleConnection()
handleConnection(incomingConnection);
}
}
catch(BindException e) {
System.out.println("Unable to bind to port "+listenPort);
}
catch(IOException e) {
System.out.println("Unable to instantiate a ServerSocket on port: "+listenPort);
}
}
/**
* Socket
*/
public void handleConnection(Socket incomingConnection) {
try {
OutputStream outputToSocket = incomingConnection.getOutputStream();
InputStream inputFromSocket = incomingConnection.getInputStream();
// socket,
BufferedReader streamReader = new BufferedReader(new InputStreamReader(inputFromSocket));
FileReader fileReader = new FileReader(new File(streamReader.readLine()));
BufferedReader bufferedFileReader = new BufferedReader(fileReader);
// socket
PrintWriter streamWriter = new PrintWriter(outputToSocket);
String line = null;
while((line=bufferedFileReader.readLine())!=null){
streamWriter.println(line);
}
fileReader.close();
// streamWriter streamReader,
// Socket , ( )
streamWriter.close();
streamReader.close();
}
catch(Exception e) {
System.out.println("Error handling a client: "+e);
e.printStackTrace();
}
}
public static void main(String args[]) {
RemoteFileServer server = new RemoteFileServer(1001);
server.acceptConnections();
}
}
(2) 클 라 이언 트 측
import java.io.*;
import java.net.*;
/**
* IP Socket。
* Socket 。
* BufferedReader/PrintWriter , 。
* Socket 。
* 。
* @author Administrator
*/
public class RemoteFileClient {
protected BufferedReader socketReader;
protected PrintWriter socketWriter;
protected String hostIp;
protected int hostPort;
/**
*
*/
public RemoteFileClient(String hostIp, int hostPort) {
this.hostIp = hostIp;
this.hostPort=hostPort;
}
/**
* ,
*/
public String getFile(String fileNameToGet) {
StringBuffer fileLines = new StringBuffer();
try {
socketWriter.println(fileNameToGet);
socketWriter.flush();
String line = null;
while((line=socketReader.readLine())!=null)
fileLines.append(line+"
");
}
catch(IOException e) {
System.out.println("Error reading from file: "+fileNameToGet);
}
return fileLines.toString();
}
/**
* , Socket
*/
public void setUpConnection() {
try {
Socket client = new Socket(hostIp,hostPort);
socketReader = new BufferedReader(new InputStreamReader(client.getInputStream()));
socketWriter = new PrintWriter(client.getOutputStream());
}
catch(UnknownHostException e) {
System.out.println("Error1 setting up socket connection: unknown host at "+hostIp+":"+hostPort);
}
catch(IOException e) {
System.out.println("Error2 setting up socket connection: "+e);
}
}
/**
* “ ”。
*/
public void tearDownConnection() {
try {
socketWriter.close();
socketReader.close();
}catch(IOException e) {
System.out.println("Error tearing down socket connection: "+e);
}
}
public static void main(String args[]) {
RemoteFileClient remoteFileClient = new RemoteFileClient("127.0.0.1",1001);
remoteFileClient.setUpConnection();
StringBuffer fileContents = new StringBuffer();
fileContents.append(remoteFileClient.getFile("D:/test.txt"));
//remoteFileClient.tearDownConnection();
System.out.println(fileContents);
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Is Eclipse IDE dying?In 2014 the Eclipse IDE is the leading development environment for Java with a market share of approximately 65%. but ac...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.