62,621
社区成员
发帖
与我相关
我的任务
分享import java.net.*;
import java.io.*;
public class chatServer implements Runnable{
public static final int PORT=1234;
protected ServerSocket listen;
Thread connect;
clientThread cThread[]=new clientThread[5];
int num=0;
public chatServer() {
try{
listen=new ServerSocket(PORT);
}
catch(UnknownHostException e){}
catch(IOException e2){}
connect=new Thread(this);
connect.start();
}
public void run() {
try {
while(true) {
Socket client=listen.accept();
cThread[num]=new clientThread(this,client);
cThread[num].start();
num++;
}
}
catch(IOException e){}
}
public void broadcast(String msg) {
for(int i=0;i <num;i++) {
try {
cThread[i].out.writeUTF(msg);
}
catch(IOException e) {}
}
}
public static void main(String args[]) {
new chatServer();
System.out.println("Chat Server is starting!.....");
}
}
class clientThread extends Thread {
protected Socket client;
protected DataOutputStream out;
protected DataInputStream in;
protected chatServer server;
public clientThread(chatServer server,Socket client) {
this.server=server;
this.client=client;
try{
in=new DataInputStream(client.getInputStream());
out=new DataOutputStream(client.getOutputStream());
}
catch(IOException e) {}
}
public void run() {
try{
while(true) {
server.broadcast(in.readUTF());
}
}
catch(IOException e) {}
}
}
客户端文件:
import java.net.*;
import java.io.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.applet.*;
//import java.awt.Panel;
class Spanel extends Panel{
TextField msg_txt;
JButton button;
Spanel(){
setLayout(new FlowLayout());
msg_txt=new TextField(20);
button=new JButton("送出");
add(new JLabel("你说的话"));
add(msg_txt);
add(button);
}
}
public class chatClient extends Applet implements Runnable,ActionListener{
public static final int PORT=1234;
String name;
Socket socket;
DataOutputStream out;
DataInputStream in;
Thread thread;
TextArea chat_txt;
Spanel sp;
public void init(){
setBackground(new Color(230,230,200));
setLayout(new BorderLayout());
name=getParameter("Chatname");
chat_txt=new TextArea(10,45);
chat_txt.setEditable(false);
Spanel sp=new Spanel();
add("Center",chat_txt);add("South",sp);
sp.button.addActionListener(this);
chat_txt.setBackground(new Color(200,185,220));
}
public void start(){
try{
socket=new Socket(this.getCodeBase().getHost(),PORT);
in=new DataInputStream(socket.getInputStream());
out=new DataOutputStream(socket.getOutputStream());
}
catch(IOException e){}
chat_txt.append("★★★"+" "+name+",欢迎你来到聊天室 "+"★★★\n");
if(thread==null){
thread=new Thread(this);
thread.start();
}
}
public void run(){
try{
while(true){
chat_txt.append(in.readUTF()+'\n');
}
}
catch(IOException e){}
}
public void actionPerformed(ActionEvent e){
if((name!=null)){
try{
out.writeUTF(name+"说→"+":"+sp.msg_txt.getText());
sp.msg_txt.setText(null);
}
catch(IOException e1){}
}
}
public static void main(String args[]){
new Spanel();
new chatClient();
}
}
public class Hello {
// 程序入口
public static void main(String[] args) {
System.out.println("Hello!");
}
}