public abstract class TCP implements Runnable { protected Socket s = null; protected InputStream in = null; protected OutputStream out = null; protected int port = 9999;
public void initialize() { Socket s; try { s = new Socket(serverAddress, port); in = s.getInputStream(); out = s.getOutputStream(); } catch (IOException e) { e.printStackTrace(); } }
public static void main(String[] args) throws IOException { String ip = null; int port = 9999; if (args.length == 2) { ip = args[0]; port = Integer.parseInt(args[1]); } else if (args.length == 1) { ip = args[0]; } TCPClient client = null; if (ip != null) client = new TCPClient(ip, port); else client = new TCPClient(port); client.startCommunicate(); client.close(); } } package messager;
import java.net.*; import java.io.*;
public class TCPServer extends TCP{ private ServerSocket ss = null;
public TCPServer(int port) { this.port = port; }
public void initialize() { try { ss = new ServerSocket(port); s = ss.accept(); in = s.getInputStream(); out = s.getOutputStream(); } catch (IOException e) { e.printStackTrace(); } }
public static void main(String[] args) throws IOException { int port = 9999; if (args.length > 0) port = Integer.parseInt(args[0]); TCPServer server = new TCPServer(port); server.startCommunicate(); server.close(); } }