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 墨風?* 模擬客戶端?*/public class Client { public static void main(String[] args) throws IOException{ //1.創(chuàng)建客戶端對象 //Socket socket = new Socket("192.168.2.132",8888); //Socket socket = new Socket("localhost",8888);//localhost表示本機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(); //通過流發(fā)送信息 pw.println("客戶端說:"+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 墨風?* 模擬服務器端?*/public class Server { public static void main(String[] args) throws Exception { //1.創(chuàng)建對象 ServerSocket ss = new ServerSocket(8888); //2.開啟服務器監(jiān)聽 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("服務器說:"+info1); if(info1.endsWith("bye")||info.endsWith("bye")){ break; } } }}我有幾點不懂:為什么要使用超類只是為了轉(zhuǎn)換時傳參?客戶端中的nextLine()此掃描器執(zhí)行當前行,并返回跳過的輸入信息。不太懂啥意思客戶端中的這個if(info1.endsWith("bye")||info.endsWith("bye")){break;}啥意思客戶端和服務器就通過socket通信?他們的代碼是靠什么關(guān)聯(lián)的比如父類與子類他們有個extends可以簡要說下這個原理嗎?????跪求大神
這個java網(wǎng)絡聊天室很多不懂
慕先生4463397
2017-10-24 17:10:47