标签:ide style rtt 环境 调用 ring return 版本支持 socket
1 便民查询网站的分析
1.1 为了方便广大网民的日常查询需求,通过便民查询网站可以查询手机号、IP地址、天气等信息,如下图所示:
1.2 客户端/服务器通信
1.3 Socket实现
package cn.server; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.Socket; /** * 描述: */ public class WeatherThread implements Runnable { private Socket socket; public WeatherThread(Socket socket){ this.socket = socket; } @Override public void run() { InputStream is = null; OutputStream os = null; try { //读取数据 is = socket.getInputStream(); byte[] buffer = new byte[1024]; int len = 0; while((len = is.read(buffer)) != -1){ System.out.print("客户端发送来的数据:"+new String(buffer,0,len)); } //线程睡眠 Thread.sleep(1000); //返回数据 String result = "晴天"; os.write(result.getBytes("utf-8")); } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } finally { } } }
package cn.server; import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; /** * 描述: */ public class WeatherServer { public static void main(String[] args) throws IOException { ServerSocket socket = new ServerSocket(12345); System.out.print("服务器端已经启动"); while(true){ //监听客户端连接 Socket accept = socket.accept(); new Thread(new WeatherThread(accept)).start(); } } }
package cn.client; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.Socket; /** * 描述: */ public class WeatherClient { public static void main(String[] args) throws IOException { Socket socket = null; InputStream is = null; OutputStream os = null; try{ socket = new Socket("127.0.0.1",12345); os = socket.getOutputStream(); os.write("江苏".getBytes("utf-8")); is = socket.getInputStream(); byte[] buffer = new byte[1024]; int len = 0; while((len = is.read(buffer))!= -1){ System.out.print("服务器传送的数据"+new String(buffer,0,len)); } }finally { socket.close(); is.close(); os.close(); } } }
1.4 WebService实现
1.4.1 JAX-WS示例
package cn.server; /** * 描述:天气查询服务接口 */ public interface WeatherInterface { public String queryWeather(String cityName); }
package cn.server; import javax.jws.WebService; /** * 描述:天气查询SEI的实现类 */ //使用@WebService标识此类是webservice的服务类 @WebService public class WeatherInterfaceImpl implements WeatherInterface { @Override public String queryWeather(String cityName) { System.out.println("从客户端:"+cityName); String result = "晴朗"; System.out.println("到客户端:"+result); return null; } }
package cn.server; import javax.xml.ws.Endpoint; /** * 描述:发布天气查询服务 */ public class WeatherServerWebService { public static void main(String[] args){ Endpoint.publish("http://127.0.0.1:12345/weather",new WeatherInterfaceImpl()); } }
http://127.0.0.1:12345/weather?wsdl
标签:ide style rtt 环境 调用 ring return 版本支持 socket
原文地址:http://www.cnblogs.com/xuweiweiailixing/p/6914754.html