www.pudn.com > TestToolsProject.rar > StartServer.java, change:2014-05-18,size:2030b


package com.mervyn.server; 
 
import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.io.PrintWriter; 
import java.net.ServerSocket; 
import java.net.Socket; 
 
import javax.swing.JOptionPane; 
 
import com.mervyn.testtools.TestToolsPing; 
import com.mervyn.testtools.UpdateDriay; 
 
public class StartServer { 
	private ServerSocket ss; 
 
	private InputStreamReader inputStreamReader; 
 
	private BufferedReader bufferedReader; 
	public void server() { 
		System.out.println("服务器已启动。。。。"); 
		try { 
			// 让服务器端程序开始监听来自4242端口的客户端请求 
			if (ss == null) { 
				ss = new ServerSocket(7850); 
				//System.out.println("服务器启动..."); 
			} 
 
			// 服务器无穷的循环等待客户端的请求 
			while (true) { 
				/* 
				 * accept()方法会在等待用户的socket连接时闲置着,当用户链接 
				 * 上来时,此方法会返回一个socket(在不同的端口上)以便与客户端 
				 * 通信。Socket与ServerSocket的端口不同,因此ServerSocket可以 空闲出来等待其他客户端 
				 */ 
				// 这个方法会停下来等待要求到达之后再继续 
				Socket s = ss.accept(); 
 
				inputStreamReader = new InputStreamReader(s.getInputStream()); 
				bufferedReader = new BufferedReader(inputStreamReader); 
				String advice = null; 
				String request = bufferedReader.readLine(); 
 
				//System.out.println("接收到了客户端的请求:" + request); 
				PrintWriter printWriter = new PrintWriter(s.getOutputStream()); 
				if (request.equals("更新记录")) { 
					UpdateDriay driay = new UpdateDriay(); 
					driay.setVisible(true); 
				}  
				if(request.equals("程序有更新")) 
				{ 
					JOptionPane.showMessageDialog(null, "程序有更新版本,请登录//192.168.71.11服务器获取最新版本!", "版本更新提示", 
							JOptionPane.INFORMATION_MESSAGE); 
				} 
				else { 
					advice = "无法识别发送的数据格式有误,请核对!!!"; 
				} 
 
				printWriter.println(advice); 
				printWriter.close(); 
 
			} 
		} catch (IOException e) { 
			// TODO Auto-generated catch block 
			e.printStackTrace(); 
		} 
 
	} 
}