java旅行第二站--javaSE第9天--服务器端与客户端之间互相通信

package 服务器端与客户端双向通信;

import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;

import com.HomeWork.day07.FileOperation;

public class ServerInputThread extends Thread {

	
	private Socket socket;
	private FileOperation fo = new FileOperation();

	public ServerInputThread(Socket socket) {
		this.socket = socket;
	}

	{
		System.out.println(fo.read("e:/Test/chat.txt"));
	}

	@Override
	public void run() {
		InputStream is = null;
		try {
			is = this.socket.getInputStream();

			byte[] buffer = new byte[1024];
			int len = 0;
			String message = null;

				while (-1 != (len = is.read(buffer))) {
					
					message = new String(buffer, 0, len);
					String s = "客户端:" + message;
					System.out.println(s);
					
					fo.write("e:/Test/chat.txt", s);
					
//					if ("bye".equals(message)) {
//						ServerMain.flag = false;
//						break;
//					}
				}
			

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				is.close();
				this.socket.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}


package 服务器端与客户端双向通信;

import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Scanner;

public class ServerOutputThread extends Thread {
	private Socket socket;

	public ServerOutputThread(Socket socket) {
		this.socket = socket;
	}

	@Override
	public void run() {

		OutputStream os = null;
		Scanner sc = null;
		try {
			os = this.socket.getOutputStream();
			sc = new Scanner(System.in);
			
				while (true) {

					String message = sc.nextLine();

					os.write(message.getBytes());
					os.flush();

//					if ("bye".equals(message)) {
//						ServerMain.flag = false;
//						break;
//					}
				}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				sc.close();
				os.close();
				this.socket.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}


package 服务器端与客户端双向通信;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class ServerMain {
	
	public static void main(String[] args) throws IOException {
		ServerSocket ss = new ServerSocket(8080);
		
		Socket s = ss.accept();
		
		ServerInputThread inputThread = new ServerInputThread(s);
		ServerOutputThread outputThread = new ServerOutputThread(s);
		
//		inputThread.setPriority(1);
//		outputThread.setPriority(10);
		
		inputThread.start();
		outputThread.start();
		
		ss.close();
	}

}


package 服务器端与客户端双向通信;

import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;

import com.HomeWork.day07.FileOperation;

public class ClientInputThread extends Thread {
	private Socket socket;
	private FileOperation fo = new FileOperation();

	public ClientInputThread(Socket socket) {
		this.socket = socket;
	}

	{
		System.out.println(fo.read("e:/Test/chat.txt"));
	}

	@Override
	public void run() {

		InputStream is = null;
		try {
			is = this.socket.getInputStream();

			byte[] buffer = new byte[1024];
			int len = 0;
			String message = null;

				while (-1 != (len = is.read(buffer))) {
					
					message = new String(buffer, 0, len);
					String s = "服务器端:" + message;
					System.out.println(s);

					fo.write("e:/Test/chat.txt", s);
					
//					if ("bye".equals(message)) {
//						ClientMain.flag = false;
//						break;
//					}
				}
			

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				is.close();
				this.socket.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}


package 服务器端与客户端双向通信;

import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Scanner;

public class ClientOutputThread extends Thread {
	private Socket socket;

	public ClientOutputThread(Socket socket) {
		this.socket = socket;
	}

	@Override
	public void run() {
		OutputStream os = null;
		Scanner sc = null;
		try {
			os = this.socket.getOutputStream();
			sc = new Scanner(System.in);

			while (true) {
				
				String message = sc.nextLine();
				
				os.write(message.getBytes());
				os.flush();
				
//				if ("bye".equals(message)) {
//					ClientMain.flag = false;
//					break;
//				}
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				sc.close();
				os.close();
				this.socket.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

}


package 服务器端与客户端双向通信;

import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;

public class ClientMain {
	
	public static void main(String[] args) throws UnknownHostException, IOException {
		// TODO Auto-generated method stub
		Socket s = new Socket("127.0.0.1", 8080);
		
		ClientInputThread inputThread = new ClientInputThread(s);
		ClientOutputThread outputThread = new ClientOutputThread(s);
		
//		inputThread.setPriority(1);
//		outputThread.setPriority(10);
		
		inputThread.start();
		outputThread.start();
	}

}


然后是将聊天记录保存到文件以及读取的代码

package com.HomeWork.day07;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class FileOperation {
	
	public FileOperation() {
		
	}
	
	public String read(String filePathName) {
		
		StringBuffer sb = new StringBuffer();
		
		InputStream is = null;
		try {
			is = new FileInputStream(filePathName);
			byte[] buffer = new byte[1024];
			int len = 0;
			
			while((len = is.read(buffer)) != -1) {
				String s = new String(buffer, 0, len);
				sb.append(s);
			}

		} catch (FileNotFoundException e) {
			System.out.println("文件找不到!");
		} catch (IOException e) {
			System.out.println("文件读取失败!");
		}

		try {
			is.close();
		} catch (IOException e) {
			System.out.println("文件读取流关闭失败!");
		}
		return sb.toString();
	}
	
	public void write(String filePathName, String context) {
		
		File f = new File(filePathName);
		if(!f.exists()) {
			try {
				f.createNewFile();
			} catch (IOException e) {
				System.out.println("文件创建失败!");
			}
		}
		OutputStream os = null;
		
		try {
			
			os = new FileOutputStream(filePathName, true);
			os.write(context.getBytes());
			os.write(("\r\n").getBytes()); //当把一个文件读取完毕之后,多打一个换行,以便下次复制文件可以显示在下一行
			
		} catch (FileNotFoundException e) {
			System.out.println("文件找不到!");
		} catch (IOException e) {
			System.out.println("文件写入失败");
		}
		
		try {
			os.close();
		} catch (IOException e) {
			System.out.println("文件写入流关闭失败");
		}
	}
}


暂时只能一个服务端与一个客户端通信,而且如果要结束通信,只能强制关闭,最后上效果图


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值