import java.io.*; import java.net.*; public class Receiver { private int port=8000; private ServerSocket serverSocket; private static int stopWay=1; //结束通信的方式 private final int NATURAL_STOP=1; //自然结束 private final int SUDDEN_STOP=2; //突然终止程序 private final int SOCKET_STOP=3; //关闭Socket,再结束程序 private final int INPUT_STOP=4; //关闭输入流,再结束程序 private final int SERVERSOCKET_STOP=5; //关闭ServerSocket,再结束程序
public Receiver() throws IOException { serverSocket = new ServerSocket(port); System.out.println("服务器已经启动"); }
public static void main(String args[])throws Exception { if(args.length>0)stopWay=Integer.parseInt(args[0]); new Receiver().receive(); } }
//Sender.java
import java.net.*; import java.io.*; import java.util.*; public class Sender { private String host="localhost"; private int port=8000; private Socket socket; private static int stopWay=1; //结束通信的方式 private final int NATURAL_STOP=1; //自然结束 private final int SUDDEN_STOP=2; //突然终止程序 private final int SOCKET_STOP=3; //关闭Socket,再结束程序 private final int OUTPUT_STOP=4; //关闭输出流,再结束程序
public Sender()throws IOException{ socket=new Socket(host,port); } public static void main(String args[])throws Exception{ if(args.length>0)stopWay=Integer.parseInt(args[0]); new Sender().send(); } private PrintWriter getWriter(Socket socket)throws IOException{ OutputStream socketOut = socket.getOutputStream(); return new PrintWriter(socketOut,true); }