简单的socket实现:
package socket;import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;/*** Created by hupo.wh on 2016/7/14.*/
public class SocketClient {public static void main(String args[]) {try {Socket socket = new Socket("localhost",8888);OutputStream out = socket.getOutputStream();out.write("hello".getBytes());out.flush();out.close();} catch (IOException e) {e.printStackTrace();}}
}
package socket;import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;/*** Created by hupo.wh on 2016/7/14.*/
public class SocketServer {public static void main(String args[]) {try {ServerSocket serverSocket = new ServerSocket(8888);boolean isStopped = false;while (!isStopped) {System.out.println("accept waiting...");Socket clientSocket = serverSocket.accept();BufferedReader is=new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));System.out.println(is.readLine());clientSocket.close();}serverSocket.close();} catch (IOException e) {e.printStackTrace();}}
}