JAVA聊天室七

JAVA聊天室七,第1张

JAVA聊天室七
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();
	}

}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5597376.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-15
下一篇 2022-12-15

发表评论

登录后才能评论

评论列表(0条)

保存