import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Lolek
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
String addressIP;
if (args.length == 0) {
addressIP = "127.0.0.1";
}
else {
addressIP = args[0];
}
ReciveT recive = new ReciveT(addressIP,5000);
SendT send = new SendT(recive.getSocket());
}
}
class ReciveT implements Runnable
{
private Socket socket;
private String IPAddress;
private int port;
ReciveT(String IPAddress,int port)
{
try {
this.IPAddress = IPAddress;
this.port = port;
this.socket = new Socket(IPAddress, port);
new Thread(this, "Recive thread").start();
} catch (UnknownHostException ex) {
Logger.getLogger(ReciveT.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(ReciveT.class.getName()).log(Level.SEVERE, null, ex);
}
}
public Socket getSocket()
{
return this.socket;
}
public void run()
{
BufferedReader read = null;
try {
read = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String line = null;
while ((line = read.readLine()) != null) {
System.out.println(line);
System.out.flush();
}
} catch (IOException ex) {
Logger.getLogger(ReciveT.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
read.close();
} catch (IOException ex) {
Logger.getLogger(ReciveT.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
class SendT implements Runnable
{
private Socket socket;
SendT(Socket socket)
{
this.socket = socket;
new Thread(this, "Send Thread").start();
}
public void run()
{
BufferedWriter write = null;
try {
BufferedReader readKeyboard = new BufferedReader(new InputStreamReader(System.in));
write = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
String line;
while ((line = readKeyboard.readLine()) != null) {
write.write(line);
write.newLine();
write.flush();
}
} catch (IOException ex) {
Logger.getLogger(SendT.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
write.close();
} catch (IOException ex) {
Logger.getLogger(SendT.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}