点对面的通讯(Dos版聊天)

//服务器端
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.io.*;
class FaceServer extends ServerSocket{//点对面的  面Server
	private int port;//端口
	public FaceServer(int port) throws IOException{
		super(port);
		this.port = port;
		System.out.println("服务器已经启动,监听端口号为:" + port);
		System.out.println("正在等待客户的连接...........");
		
		try{
			while(true){//循环挂起等待客户的请求连接
				Socket socketCon = accept();
				new ServerThread(socketCon, port);//建立服务线程,这是调用下面的  线程
			}
		}catch(IOException e ){
			System.out.println("没有监听到客户端的信息......");
		}finally{
			close();
		}
	}
}
class ServerThread extends Thread{
	private int port;
	private Socket socketConn;
	private BufferedReader in;
	private PrintWriter out;
	public ServerThread(Socket s, int port) throws IOException{
		this.port = port;
		this.socketConn = s;
		in = new BufferedReader(new InputStreamReader(socketConn.getInputStream(), "gb2312"));
		out = new PrintWriter(socketConn.getOutputStream(), true);
		out.println("服务器连接成功.........");//向客户端发送的信息
		out.println("输入exit断开与服务连接");
		start();
	}
	public void run(){
		try{
			boolean done = false;
			while(!done){
				String line = in.readLine();
				if(line == null){
					done = true;
				}else{
					System.out.println("客户端传来的内容:" + line);
					String message = line;
					out.println("从服务器发送的内容:" + message);
					if(line.trim().equals("exit")){
						done = true;
					}
				}
			}
			System.out.println("客户端终止发送信息..........");
			socketConn.close();
		}catch(Exception e){
			System.out.println("启动服务器出现错误:" + e.getMessage());
		}
	}
}
public class TextFaceServer{
	public static void main(String[] args){
		try{
			new FaceServer(8080);
		}catch(Exception e){
			System.out.println("服务器进行监听出现异常:" + e.getMessage());
		}
	}
}
//客户端,可启动多个
import java.io.*;
import java.net.*;
class SocketServer{
	private int port;
	public SocketServer(int port){
		this.port = port;//端口
		start();//开始的方法
	}
	public String infoUpperCase(String line){
		return line.toUpperCase();//字符大写
	}
	public void start(){
		try{
			ServerSocket serverSocket = new ServerSocket(port);
			System.out.println("服务器已经启动,监听端口号为:" + port);
			System.out.println("正在等待客户端连接.........");
			
			Socket socketAccept = serverSocket.accept();
			//获取读取客户端的数据流
			BufferedReader in = new BufferedReader(new InputStreamReader(socketAccept.getInputStream()));
			//写入客户端的流,自动刷新
			PrintWriter out = new PrintWriter(socketAccept.getOutputStream(),true);
			
			out.println("服务器连接成功........");//向客户端发送信息
			out.println("输入exit断开与服务连接");
			
			boolean done = false;
			while(!done){
				String line = in.readLine();//读取客户端每行的内容
				if(line == null){//读取的内容为空
					done = true;
				}else{
					System.out.println("客户端传来的内容:" + line);
					String message = infoUpperCase(line);
					out.println("从服务器端口发送的内容:" + message);
					
					if(line.trim().equals("exit")){
						done = true;//退出
					}
				}
			}
			socketAccept.close();//关闭通信资源
		}catch(Exception e){
			System.out.println("启动服务器出现错误:" + e.getMessage());
		}
	}
}

public class TextSocketServer{
	public static void main(String[] args){
		try{
			//SocketServer server = new SocketServer(8080);
			new SocketServer(8080);
		}catch(Exception e){
			System.out.println("测试服务器监听出错:" + e.getMessage());
		}
	}
}



评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值