package socket; import java.awt.im.InputSubset; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.net.Socket; import java.util.Scanner; public class Client { private Socket socket; public Client(){ try{ System.out.println("正在连接服务端...."); socket = new Socket("localhost",8088); System.out.println("已连接服务端...."); }catch(Exception e){ e.printStackTrace(); } } public void start(){ try{ Scanner scanner = new Scanner(System.in); OutputStream out = socket.getOutputStream(); OutputStreamWriter osw =new OutputStreamWriter(out,"UTF-8"); BufferedWriter bw = new BufferedWriter(osw); PrintWriter pw = new PrintWriter(bw,true); InputStream in = socket.getInputStream(); InputStreamReader isr = new InputStreamReader(in,"UTF-8"); BufferedReader br = new BufferedReader(isr); String line = null; while(true){ line = scanner.nextLine(); pw.println(line); //读取服务器端发送过来的一行字符串 line = br.readLine(); System.out.println(line); } }catch(Exception e){ e.printStackTrace(); } } public static void main(String[] args){ Client client = new Client(); client.start(); } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)