编程语言
563
//客户端
import java.io.*; import java.net.*; public class Myclient { public static void main(String[] args) { Socket client= null; BufferedWriter out = null; try { client = new Socket(InetAddress.getLocalHost(),5555); out = new BufferedWriter(new OutputStreamWriter(client.getOutputStream())); out.write("11111111111"); out.flush(); } catch (IOException e) { e.printStackTrace(); }finally { try { out.close(); } catch (IOException e1) { e1.printStackTrace(); } try { client.close(); } catch (IOException e) { e.printStackTrace(); } } } }
//客户端
import java.io.*; import java.net.*; public class Myservice { public static void main(String[] args) { ServerSocket server= null; Socket client = null; BufferedReader in = null; try { server = new ServerSocket(5555); client = server.accept(); in = new BufferedReader(new InputStreamReader(client.getInputStream())); System.out.println(in.readLine()); } catch (Exception e) { e.printStackTrace(); }finally { try { if(in!=null) in.close(); } catch (IOException e) { e.printStackTrace(); } try { if(client!=null) client.close(); } catch (IOException e) { e.printStackTrace(); } try { if(server!=null) server.close(); } catch (IOException e) { e.printStackTrace(); } } } }
广告