客户端
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;
public class ChatClient {
public static void main(String[] args) throws Exception, IOException {
//创建客户端的Socket服务,指定目的主机和端口号
Socket socket=new Socket("202.194.126.159",10000);
BufferedReader dosReader=new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bufferedWriter=new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(socket.getInputStream()));
new ChatClientToServerWriter(dosReader, bufferedWriter).start();
new ChatServerToClinetReader(bufferedReader, bufferedWriter).start();
}
}
class ChatClientToServerWriter extends Thread{
BufferedReader dosReader;
BufferedWriter bufferedWriter;
public ChatClientToServerWriter(BufferedReader dosReader,
BufferedWriter bufferedWriter) {
super();
this.dosReader = dosReader;
this.bufferedWriter = bufferedWriter;
}
@Override
public void run() {
super.run();
String line=null;
try {
while(true)
{
while((line=dosReader.readLine())!=null){
bufferedWriter.write(line);
bufferedWriter.newLine();
bufferedWriter.flush();
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
class ChatServerToClinetReader extends Thread{
BufferedReader bufferedReader;
BufferedWriter bufferedWriter;
public ChatServerToClinetReader(BufferedReader bufferedReader,
BufferedWriter bufferedWriter) {
super();
this.bufferedReader = bufferedReader;
this.bufferedWriter = bufferedWriter;
}
@Override
public void run() {
super.run();
String line=null;
try {
while(true)
{
while((line=bufferedReader.readLine())!=null){
System.out.println("服务端发来:"+line);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
服务器
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class ChatServer {
public static void main(String[] args) throws Exception {
//创建服务端的Socket服务,指定端口号
ServerSocket serverSocket=new ServerSocket(10000);
while(true){
//通过accept方法获取连接过来的Socket对象
Socket socket=serverSocket.accept();
BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(socket.getInputStream()));
BufferedWriter bufferedWriter=new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
BufferedReader dosReader=new BufferedReader(new InputStreamReader(System.in));
new ChatClientToServerReader(bufferedReader, bufferedWriter).start();
new ChatServerToClientWriter(dosReader, bufferedWriter).start();
}
}
}
class ChatClientToServerReader extends Thread{
BufferedReader bufferedReader;
BufferedWriter bufferedWriter;
public ChatClientToServerReader(BufferedReader bufferedReader,
BufferedWriter bufferedWriter) {
super();
this.bufferedReader = bufferedReader;
this.bufferedWriter = bufferedWriter;
}
@Override
public void run() {
super.run();
String line=null;
try {
while(true)
{
while((line=bufferedReader.readLine())!=null){
System.out.println("客户端发来:"+line);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
class ChatServerToClientWriter extends Thread{
BufferedReader dosReader;
BufferedWriter bufferedWriter;
public ChatServerToClientWriter(BufferedReader dosReader,
BufferedWriter bufferedWriter) {
super();
this.dosReader = dosReader;
this.bufferedWriter = bufferedWriter;
}
@Override
public void run() {
super.run();
String line=null;
try {
while(true)
{
while((line=dosReader.readLine())!=null){
bufferedWriter.write(line);
bufferedWriter.newLine();
bufferedWriter.flush();
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}