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("文件写入流关闭失败");
}
}
}
暂时只能一个服务端与一个客户端通信,而且如果要结束通信,只能强制关闭,最后上效果图