国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Socket

2019-11-08 18:34:43
字體:
來源:轉載
供稿:網友

客戶端:

public class MyClient {	public static void main(String[] args) {				try {			Socket s = new Socket("192.168.0.106",9090);			OutputStream ops = s.getOutputStream();			ops.write("gg".getBytes());			ops.close();		} catch (IOException e) {						e.PRintStackTrace();		}			}}服務端:

public class MyServer {	public static void main(String[] args) {		try {				ServerSocket serverSocket = new ServerSocket(9090);				System.out.println("服務器開啟");				while(true){									Socket socket = serverSocket.accept();					new ServiceThread(socket);				}		} catch (IOException e) {						e.printStackTrace();		}	}}

服務端線程

public class ServiceThread implements Runnable{		Socket socket;	private InputStream iS;	public ServiceThread(Socket socket){		this.socket = socket;		InetAddress address = socket.getInetAddress();		System.out.println("---客戶端連接:"+address+"---");		try {			iS = socket.getInputStream();		} catch (IOException e) {						e.printStackTrace();		}		new Thread(this).start();	}		@Override	public void run() {		while(true){			try{			String msg = MessageUtil.read(iS);			System.out.println("客戶端發送的消息:"+msg);			Thread.sleep(200);			}catch(Exception e){				System.out.println("---客戶端關閉連接---");				break;			}		}			}}信息的讀與寫的方法

public class MessageUtil {	public static String read(InputStream iS){								try {										byte[] b = new byte[1024];//緩存數組(1k)					int length = -1;					String content = "";					while((length = iS.read(b))!= -1){						String result = new String(b,0,length);						if("#end#".equals(result)){							break;						}else{							content += result;						}					}					return content;				} catch (Exception e) {					throw new RuntimeException();				}					}	public static void write(OutputStream ops,String t){		try {			ops.write(t.getBytes());			ops.write("#end#".getBytes());		} catch (IOException e) {						e.printStackTrace();		}				}	}


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 新邵县| 昌宁县| 云林县| 临沭县| 昌吉市| 曲阜市| 东乌| 安庆市| 岢岚县| 高安市| 绩溪县| 屏边| 霍邱县| 紫阳县| 布拖县| 云和县| 延津县| 凌云县| 尼玛县| 濮阳县| 汉沽区| 合江县| 秦皇岛市| 抚宁县| 邻水| 淳化县| 盘山县| 枣强县| 福海县| 沛县| 仙居县| 乐陵市| 明光市| 台中市| 伊宁县| 武义县| 广汉市| 大渡口区| 阳原县| 瑞安市| 原阳县|