怎么写服务器端
1. socket通信服务器端怎么写
Android客户端与PC服务器实现Socket通信(wifi)本文介绍Android终端持续扫描AP信息并发送给服务器端的实现。
首先基于TCP协议在Android终端和PC两端之间形成网络虚拟链路。使用ServerSocket创建TCP服务器端,然后在Android客户端使用Socket的构造器来连接服务器。
其中Android终端通过WIFI连接和PC处于同一局域网。1. PC服务器启用ServerSocket两个通信实体在建立虚拟链路之前,需要有一方先准备好,主动接受来自其他通信实体的连接请求。
使用ServerSocket对象监听来自客户端的Socket连接//创建ServerSocket对象//by wayne from /dwayne/ServerSocket ss = new ServerSocket(30000);//监听来自客户端的请求while(true){Socket s = ss.accept();…}如果没有连接,则将一直处于等待状态。当接收到连接请求后,获取消息到输入流,并保存到文件。
//接收客户端消息//by wayne from /dwayne/BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));String str;BufferedWriter bw = new BufferedWriter(new FileWriter("D:/ApInfo"+ (i++) +".txt"));while ((str = in.readLine()) != null) {System.out.println(str);bw.write(str);bw.newLine();}2. Android终端使用Socket通信客户端使用Socket的构造器连接服务器,指定服务器IP和端口号就可以了。Socket s = new Socket(“192.168.1.100”, 30000);这样服务器端的accept()方法就得到响应,从而向下执行,服务器端和客户端就形成了一对互相连接的Socket。
再进行通信时就没有服务器和客户端之分了,都是通过输入输出流进行通信。详细步骤采用Handler和TimerTask来定时扫描AP信息并发送给服务器端。
TimerTask规定了到达指定的时间所要进行的任务。TimerTask task = new TimerTask(){public void run() {Message message = new Message();message.what = 1;handler.sendMessage(message);}};handler传递message内容:Handler handler = new Handler(){public void handleMessage(Message msg) {switch (msg.what) {case 1:// 执行定时器时间到了之后由handler传递的任务break;}super.handleMessage(msg);}};因为需要持续执行扫描任务,所以启用新线程执行定时任务//启动单独线程定时向服务器发送AP信息//by wayne from /dwaynenew Thread(){@Overridepublic void run() {// TODO Auto-generated method stubtimer.schedule(task, 2000,10000); //在2秒后每10秒执行一次定时器中的方法}}.start();接下来扫描AP信息并发送给服务器端,然后将结果保存。
WifiManager wifiManager=(WifiManager) getSystemService(WIFI_SERVICE);wifiManager.startScan();mWifiList = wifiManager.getScanResults();由WifiManager说明可知,它可以用于处理已配置的网络,当前连接的网络及AP信息的扫描等情况。This class provides the primary API for managing all aspects of Wi-Fi connectivity. Get an instance of this class by calling Context.getSystemService(Context.WIFI_SERVICE). It deals with several categories of items:The list of configured networks. The list can be viewed and updated, and attributes of individual entries can be modified.The currently active Wi-Fi network, if any. Connectivity can be established or torn down, and dynamic information about the state of the network can be queried.Results of access point scans, containing enough information to make decisions about what access point to connect to.It defines the names of various Intent actions that are broadcast upon any sort of change in Wi-Fi state.向服务器发送消息:socket = new Socket("192.168.1.211",30000);//向服务器端发送消息PrintWriter out = new PrintWriter( new BufferedWriter( new OutputStreamWriter(socket.getOutputStream())),true);out.println(message);其中message为获取的AP信息测试收到的信息格式为:SSID: ICIS_LAB, BSSID: 1c:af:f7:9a:65:e4, capabilities: [WPA-PSK-TKIP+CCMP], level: -80, frequency: 2。
怎么写服务器
1. 我的世界怎么写服务器
1.下载一个水桶服务器:见底部附件craftbukkit-1.4.7-R1.0.jar(官网下载: (单击添加,输入index.html,确定。
进行添加.)Default.htmDefault.aspiisstart.asp调整顺序:单击添加后的列表里的任一个项目,然后按左边的向上键,向上移动,向下键,向下移。以上的顺序说明:比如你的网站上有"index.htm、index.html、index.asp"这三种网页文件,那么你在网页中进行访问,会先访问到index.htm,若你的网站不存在index.htm,那么网页会访问index.asp,index.htm和index.asp都没时,那么我们输入网址访问到的就是index.html文件.(这些都是根据我们上边的设置来进行的)(不打勾)启用文档页脚这一页完成接下来我们只看一个目录安全性匿名访问和验证控制单击“编辑"在弹出的"验证方法"窗口中进行如下设置(打勾)匿名访问单击编辑这时弹出一个“匿名用户帐号"匿名用户用户名:(选择一个只可以进行访问的帐号)(这里一般已经默认设置了这样一个帐号) 密码:默认 (打勾)允许IIS控制密码完后,我们就直按确定,全部设置完成有不懂的请联系我:蓶独メ灵晃QQ:303071497邮箱:k1z2@tom.com空有一个网站空间怎么行,还要个域名才可以让别人访问到我们的网站,要不刚才设的都白设了。
好,我们用花生壳来完成这个任务先到/ 注册一个帐号注册完成后,在里面申请一个免费域名(这些在花生壳网站里可以看看帮助)然后在站里对域名进行设置详细设置如下:维护花生壳服务请如实填写以下资料,这些资料将用于您的站点推广,其中带有“*”。
转载请注明出处育才学习网 » 怎么写服务器端程序
育才学习网