国产探花免费观看_亚洲丰满少妇自慰呻吟_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();		}				}	}


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 桂林市| 达拉特旗| 阜南县| 镇赉县| 泽普县| 兴安盟| 牟定县| 瑞昌市| 老河口市| 兰坪| 邛崃市| 禹州市| 抚州市| 漳浦县| 卢湾区| 贵德县| 广宁县| 涿州市| 孝感市| 高清| 宁都县| 江北区| 和龙市| 莒南县| 宁强县| 五家渠市| 南宁市| 息烽县| 晋江市| 安达市| 宁武县| 永善县| 博罗县| 玉龙| 新建县| 从江县| 林口县| 福清市| 宣汉县| 清涧县| 寻乌县|