Created
January 12, 2012 06:43
-
-
Save youtalk/1599093 to your computer and use it in GitHub Desktop.
Server-client programs for socket communication in Java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package jp.youtalk; | |
import java.io.IOException; | |
import java.net.Socket; | |
import java.util.concurrent.Callable; | |
import java.util.concurrent.TimeUnit; | |
public class SocketAcceptor implements Callable { | |
public static final int INTERVAL_MS = 1000; | |
public static final int MAX_TRIAL = 100; | |
private final String host; | |
private final int port; | |
public SocketAcceptor(final String host, int port) { | |
this.host = host; | |
this.port = port; | |
} | |
@Override | |
public Socket call() throws InterruptedException { | |
return connect(0); | |
} | |
private Socket connect(int count) throws InterruptedException { | |
try { | |
return new Socket(host, port); | |
} catch (IOException e) { | |
if (count < MAX_TRIAL) { | |
System.out.println(getClass().getSimpleName() + ": fail to connect"); | |
TimeUnit.MILLISECONDS.sleep(INTERVAL_MS); | |
return connect(count + 1); | |
} else { | |
System.out.println(getClass().getSimpleName() + ": error"); | |
return null; | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package jp.youtalk; | |
import java.io.BufferedReader; | |
import java.io.BufferedWriter; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.io.OutputStreamWriter; | |
import java.net.ServerSocket; | |
import java.net.Socket; | |
public class SocketReaderWriter { | |
private final Socket socket; | |
private final BufferedReader reader; | |
private final BufferedWriter writer; | |
// for server | |
public SocketReaderWriter(int port) throws IOException { | |
ServerSocket server = new ServerSocket(port); | |
socket = server.accept(); | |
reader = new BufferedReader(new InputStreamReader(socket.getInputStream())); | |
writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())); | |
} | |
// for client | |
public SocketReaderWriter(String host, int port) throws InterruptedException, IOException { | |
socket = new SocketAcceptor(host, port).call(); | |
reader = new BufferedReader(new InputStreamReader(socket.getInputStream())); | |
writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())); | |
} | |
public final BufferedReader getReader() { | |
return reader; | |
} | |
public final BufferedWriter getWriter() { | |
return writer; | |
} | |
public void close() throws IOException { | |
reader.close(); | |
writer.close(); | |
socket.close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment