TcpServer 类
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.security.Provider;
import java.util.*;
public class TcpServer {
//创建 一个客服端的集合 之后会遍历
public static List<Client_channel> list = new ArrayList<Client_channel>();
//
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(8888);
while(true){
Socket socket = serverSocket.accept();
System.out.println("现在有一个线程进来了!!");
Client_channel client_channel = new Client_channel(socket);
list.add(client_channel);
new Thread(client_channel).start();
}
}
}
Client_sta类型
public class Client_sta implements Runnable {
public static int number=0;
public static void main(String[] args) {
for( int i =0;i<3;i++){
Client_sta client_sta= new Client_sta();
new Thread(client_sta).start();
}
}
@Override
public void run() {
new client(number);
Client_sta.number++;
}
}
client 类型
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.net.Socket;
public class client extends JFrame implements ActionListener,Runnable
{
private int number;
private JTextArea jTextArea;
private JPanel jPanel;
private JButton jb;
private JScrollPane jScrollPane;
private JTextField jTextField;
private DataOutputStream dos;
private Send send;
private Socket socket;
private Thread thread;
public client ( int number){
super("my_chat");
//通信相关
this.number=number;
try {
this.socket= new Socket("127.0.0.1",8888);
this.dos= new DataOutputStream(this.socket.getOutputStream());
this.send= new Send(this.socket);
//this.receive= new Receive(this.socket);
} catch (IOException e) {
JOptionPane.showMessageDialog(this,"连接不到服务器");
}
this.jTextArea= new JTextArea();
this.jTextField= new JTextField(10);
this.jScrollPane= new JScrollPane(this.jTextArea);
this.jb= new JButton("send");
this.jPanel= new JPanel();
//添加组件
this.getContentPane().setLayout( new BorderLayout());
this.getContentPane().add(this.jScrollPane,BorderLayout.CENTER);
this.getContentPane().add(this.jPanel,BorderLayout.SOUTH);
this.jPanel.add(this.jTextField);
this.jPanel.add(this.jb);
this.jb.addActionListener(this); //由this对象处理
//位置
this.setLocationRelativeTo(null);
this.setBounds(200,400, 300,200);
this.setResizable(false);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setVisible(true);
new Thread(this).start();
}
// 对于这个问题而言 只要点击按钮就是发送数据
@Override
public void actionPerformed(ActionEvent actionEvent) {
if(actionEvent.getSource().equals(this.jb)) {
String str = this.jTextField.getText();
System.out.println("现在发送 的是"+str);
if (str == "") {
JOptionPane.showMessageDialog(this, "不能为空!!");
} else {
Send.str=str;
new Thread(this.send).start();
this.jTextArea.append("\n" + "me say :" + str);
System.out.println("正在发送数据!!");
//点击发送之后发送线程启动
}
}
}
@Override
public void run() {
System.out.println("客服端正在接受数据!!!");
DataInputStream inputStream;
BufferedReader bf;
try {
inputStream = new DataInputStream(socket.getInputStream());
BufferedReader buffer = new BufferedReader(new InputStreamReader(inputStream));
//
String s=null;
while(true){ //应该是会阻塞自己的
s=buffer.readLine();
this.jTextArea.append("\n"+s);
}
} catch (IOException e) {
}
}
}
send
import java.io.*;
import java.net.Socket;
/*
发送线程 因为不会只接一次
*/
public class Send implements Runnable {
public static String str;
public BufferedReader bf;
public DataOutputStream dos ;
private PrintWriter pw;
private boolean flag = true;
public Send(Socket socket){
try {
this.dos= new DataOutputStream(socket.getOutputStream());
this.pw= new PrintWriter(this.dos);
} catch (IOException e) {
e.printStackTrace();
}
}
public String my_send(String str){
String a= null;
try {
//this.dos.writeUTF(str);
pw.println(str);
pw.flush();
//this.dos.flush();
} catch (Exception e) {
this.flag=false;
try {
this.dos.close();
//清空缓冲区
} catch (IOException ex) {
ex.printStackTrace();
}
}
return a ;
}
//收发进程
@Override
public void run() {
//一直在发送 直到this.flag
System.out.println("客服端正发送"+str);
this.my_send(Send.str);
//
}
public void close(){
try{
this.dos.close();
}catch (Exception e){
e.printStackTrace();
}
}
}
client_channel 类型
/*
1.输入流
2.输出流
3.发送数据
4.接收数据
*/
import javax.swing.*;
import java.io.*;
import java.net.Socket;
import java.util.List;
public class Client_channel implements Runnable {
private DataInputStream dataInputStream;
private DataOutputStream dataOutputStream;
private boolean flag=true;
private BufferedReader bf;
private PrintWriter pw;
private int number;
private Socket socket;
// 构造方法
public Client_channel(Socket socket) {
try{
number=0;
this.socket=socket;
dataInputStream= new DataInputStream(socket.getInputStream());
dataOutputStream = new DataOutputStream(socket.getOutputStream());
this.bf= new BufferedReader(new InputStreamReader(dataInputStream));
this.pw= new PrintWriter(this.dataOutputStream);
//输入流
}
catch (IOException e){
this.flag= false;
try {
this.dataOutputStream.close();
this.dataInputStream.close();
} catch (IOException ex) {
System.out.println("create 中出错!!");
}
}
}
public int getPort(){
return this.socket.getPort();
}
/*
//receive 数据
private String receive() {
String str="";
try {
str= dataInputStream.readUTF();
System.out.println("我收到了"+str);
} catch (IOException e) {
this.flag= false;
// 关闭dataInputStream
try {
this.dataInputStream.close();
}
catch (IOException ex) {
ex.printStackTrace();
}
TcpServer.list.remove(this);
// 关闭dataInputStream
}
return str;
}
*/
// send
private void send( String str){
if(str!=""&&str.length()!=0){
try {
this.pw.println(str);
this.pw.flush();
System.out.println("now the function send 发送l"+str);
//将缓冲区中 的东西强制送出去 防止数据收不到的情况。
} catch (Exception e) {
this.flag=false;
//e.printStackTrace();
}
}
}
// 给别的进程发送数据
private void send_other(String str) {
System.out.println(" send_other :现在是转发给别的线程" + str);
List<Client_channel> list = TcpServer.list;
//
synchronized ( list) {
for (int i = 0; i < list.size(); i++) {
System.out.println("Port is :"+list.get(i).getPort());
if (list.get(i).equals(this)) {
continue;
}
list.get(i).send("客户端"+str);
//现在最致命的问题是线程只跑了一次之后不跑了
}
}
}
@Override
public void run() {
while (true) {
try {
String s= bf.readLine();
if(s!=null)
//线程会阻塞
{
this.send_other(s);
}
// 一直监听线程发送的内容
} catch (Exception e) {
System.out.println("客服端断开连接!!");
break;
}
}
}
}