import java.io.*;
import java.net.*;
import java.util.*;
public class Server {
public static ArrayList<ServerThread> list=new ArrayList<ServerThread>();
public static int clientnum=0;
public static void main(String[] args){
try {
//创建一个服务器的Socket
ServerSocket serverSocket=new ServerSocket(8888);
Socket socket=null;
System.out.println("*****服务器即将启动,等待客户端链接*****");
DataInputStream dis;
DataOutputStream dos;
while(true){
//调用accept()方法开始监听,等待客户端的链接
socket=serverSocket.accept();
dis=new DataInputStream(socket.getInputStream());
dos=new DataOutputStream(socket.getOutputStream());
//创建线程
ServerThread thread=new ServerThread(dis,dos,clientnum);
thread.start();
list.add(thread);
clientnum++;
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.net.*;
/*
* 服务器线程的作用主要是:
* 1.接收来自客户端的信息
* 2.将接收到的信息解析,并转发给目标客户端
* */
public class ServerThread extends Thread {
public static String[] name=new String[10];
int id;
DataInputStream dis;
DataOutputStream dos;
public ServerThread(DataInputStream dis,DataOutputStream dos,int num){
this.dis=dis;
this.dos=dos;
this.id = num;
}
public void run() {
while(true){
try{
String masg=dis.readUTF();
for(int i=0;i<Server.clientnum;i++){
Server.list.get(i).dos.writeUTF(masg);
dos.flush();
}
}catch(IOException e){
e.printStackTrace();
}
}
}
}
import java.io.*;
import java.net.*;
import java.util.ArrayList;
import javax.swing.*;
public class Client {
public static void main(String[] args) {
try {
Socket socket = new Socket("localhost", 8888);
DataInputStream dis=new DataInputStream(socket.getInputStream());
DataOutputStream dos=new DataOutputStream(socket.getOutputStream());
Load load=new Load(dos);
load.setVisible(true);
ClientThread thread=new ClientThread(dis,load.getChat().getta());
thread.start();
//开启一个线程接收信息,并解析
//thread.start();
//主线程用来发送信息
}catch(Exception e){
System.out.println("服务器异常");
}
}
} import java.io.*; import java.net.*; import java.util.ArrayList;import javax.swing.*;import java.awt.*; /** * 作用:一直接收服务端转发过来的信息 * */ public class ClientThread extends Thread { DataInputStream dis; JTextArea ta; public ClientThread(DataInputStream dis,JTextArea ta){ this.dis = dis; this.ta=ta; //this.start(); } public void run() { String masg=null; while(true){ try { masg=dis.readUTF(); ta.append(masg); }catch(IOException e1) { e1.printStackTrace(); } } } } class MyListener implements ActionListener,KeyListener { public void actionPerformed(ActionEvent e) { String s = ""; Date d=new Date(); SimpleDateFormat sj=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); if(e.getActionCommand()=="牛头梗"){ me.setText(s+"To:"+e.getActionCommand()); dxname="牛头梗"; masg=sj.format(d)+"\n"+s+"对"+dxname+"说:"+tf1.getText(); }else if(e.getActionCommand()=="中华田园犬"){ me.setText(s+"To:"+e.getActionCommand()); dxname="中华田园犬"; masg=sj.format(d)+"\n"+s+"对"+dxname+"说:"+tf1.getText(); }else if(e.getActionCommand()=="东北二哈"){ me.setText(s+"To:"+e.getActionCommand()); dxname="东北二哈"; masg=sj.format(d)+"\n"+s+"对"+dxname+"说:"+tf1.getText(); }else if(e.getActionCommand()=="发送") { s1=sj.format(d)+"\n"+s+jc1.getSelectedItem()+"的对"+dxname+"说:"+tf1.getText()+"\n"; ta1.append(s1);//tf1.getText()获取输入框的文字将其输入到ta1留言框中 tf1.setText (null); try{ dos.writeUTF(masg); dos.flush(); }catch(IOException e1) { e1.printStackTrace(); } } } @Override public void keyPressed(KeyEvent a) { // TODO Auto-generated method stub String s= ""; int key=a.getKeyCode(); if(key==10) { Date d=new Date(); SimpleDateFormat sj=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); s=sj.format(d)+"\n"+jc1.getSelectedItem()+"地说:"+tf1.getText()+"\n"; ta1.append(s);//tf1.getText()获取输入框的文字将其输入到ta1留言框中 tf1.setText (null); } } public void keyReleased(KeyEvent arg0) { } public void keyTyped(KeyEvent arg0) { } }
添加回答
举报
0/150
提交
取消