Socket编程一
客户端启动后发给服务端一个信息,当服务端收到信息之后发给客户端一个反馈信息。。。服务端:package com.wlh.socket;import java.io.*; import java.net.*;public class TestSockServer {public static void main(String[] args) { InputStream in = null; OutputStream out = null; try { ServerSocket ss = new ServerSocket(5888); Socket socket = ss.accept(); in = socket.getInputStream(); out = socket.getOutputStream(); DataOutputStream dos = new DataOutputStream(out); DataInputStream dis = new DataInputStream(in); String s = null; if((s=dis.readUTF())!=null) { System.out.println(s); System.out.println("from: "+socket.getInetAddress()); System.out.println("Port: "+socket.getPort()); } dos.writeUTF("客户端你好......"); dis.close(); dos.close(); socket.close(); } catch (IOException e) {e.printStackTrace();}}}客户端:package com.wlh.socket;import java.net.*;import java.io.*;public class TestSockClient {public static void main(String[] args) { InputStream is = null; OutputStream os = null; try { Socket socket = new Socket("localhost",5888); is = socket.getInputStream(); os = socket.getOutputStream(); DataInputStream dis = new DataInputStream(is); DataOutputStream dos = new DataOutputStream(os); dos.writeUTF("服务端你好............"); String s = null; if((s=dis.readUTF())!=null); System.out.println(s); dos.close(); dis.close(); socket.close(); } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) {e.printStackTrace();}}}
页:
[1]