package com.tz.net;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStream;import java.io.PrintWriter;import java.net.InetAddress;import java.net.Socket;import java.net.UnknownHostException;import java.util.Scanner;/**?* @author 墨風(fēng)?* 模擬客戶端?*/public class Client { public static void main(String[] args) throws IOException{ //1.創(chuàng)建客戶端對(duì)象 //Socket socket = new Socket("192.168.2.132",8888); //Socket socket = new Socket("localhost",8888);//localhost表示本機(jī)IP Socket socket = new Socket(InetAddress.getLocalHost(),8888); InputStream is = socket.getInputStream(); OutputStream os = socket.getOutputStream(); //將字節(jié)流轉(zhuǎn)化為字符流 PrintWriter pw = new PrintWriter(os,true); BufferedReader br = new BufferedReader(new InputStreamReader(is)); Scanner input = new Scanner(System.in); while(true){ String info = input.nextLine(); //通過(guò)流發(fā)送信息 pw.println("客戶端說(shuō):"+info); String info1 = br.readLine(); System.out.println(info1); if(info1.endsWith("bye")||info.endsWith("bye")){ break; } } }}package com.tz.net;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStream;import java.io.PrintWriter;import java.net.ServerSocket;import java.net.Socket;import java.util.Scanner;/**?* @author 墨風(fēng)?* 模擬服務(wù)器端?*/public class Server { public static void main(String[] args) throws Exception { //1.創(chuàng)建對(duì)象 ServerSocket ss = new ServerSocket(8888); //2.開(kāi)啟服務(wù)器監(jiān)聽(tīng) Socket s = ss.accept(); InputStream is = s.getInputStream(); OutputStream os = s.getOutputStream(); PrintWriter pw = new PrintWriter(os,true); BufferedReader br = new BufferedReader(new InputStreamReader(is)); Scanner input = new Scanner(System.in); while(true){ String info = br.readLine(); System.out.println(info); String info1 = input.nextLine(); pw.println("服務(wù)器說(shuō):"+info1); if(info1.endsWith("bye")||info.endsWith("bye")){ break; } } }}我有幾點(diǎn)不懂:為什么要使用超類只是為了轉(zhuǎn)換時(shí)傳參?客戶端中的nextLine()此掃描器執(zhí)行當(dāng)前行,并返回跳過(guò)的輸入信息。不太懂啥意思客戶端中的這個(gè)if(info1.endsWith("bye")||info.endsWith("bye")){break;}啥意思客戶端和服務(wù)器就通過(guò)socket通信?他們的代碼是靠什么關(guān)聯(lián)的比如父類與子類他們有個(gè)extends可以簡(jiǎn)要說(shuō)下這個(gè)原理嗎?????跪求大神
這個(gè)java網(wǎng)絡(luò)聊天室很多不懂
慕先生4463397
2017-10-24 17:10:47