聊天系统java源代码 java聊天软件代码( 七 )


private static MapString,Socket clientMap=new HashMapString,Socket();
public static void main(String[] args) {
int i = 1;
try {
ServerSocket s = new ServerSocket(8189);
for (;;) {
Socket incoming = s.accept();
System.out.println("连接成功" + i);
ThreadedEchoHandler teh=new ThreadedEchoHandler(incoming, i);
teh.start();
String name=teh.getClientname();
clientMap.put(name,incoming);
i++;
}
} catch (Exception e) {
System.out.println(e);
}
}
}
class ThreadedEchoHandler extends Thread {
Frame f;
TextArea ta;
TextField tf;
Button send, bye;
InputStream ins;
Socket s;
PrintStream out1;
BufferedReader in;
PrintWriter out;
public ThreadedEchoHandler(Socket i, int c) {
incoming = i;
counter = c;
f=new Frame("server");
f.setLayout(new FlowLayout());
ta=new TextArea("",10,30,TextArea.SCROLLBARS_BOTH);
tf=new TextField("",20);
send=new Button("send");
bye=new Button("bye");
send.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
send(tf.getText());
}
});
bye.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
send("bye");
}
});
f.add(ta);
f.add(tf);
f.add(send);
f.add(bye);
f.setSize(300,300);
f.setVisible(true);
}
public String getClientname() {
try {
in = new BufferedReader(new InputStreamReader(
incoming.getInputStream()));
out = new PrintWriter(incoming.getOutputStream(), true);
return in.readLine();
} catch (IOException e) {
System.out.println(e.getMessage()+"get");
}
return null;
}
public void send(String context){
out.println(context);
out.flush();
}
public void run() {
try {
boolean done = false;
while (!done) {
String str = in.readLine();
if (str == null)
done = true;
else {
out.println("Echo(" + counter + "):" + str);
ta.append("Echo(" + counter + "):" + str+"\n");
if (str.trim().equals("BYE"))
done = true;
}
}
incoming.close();
} catch (Exception e) {
System.out.println(e.getMessage()+"run");
}
}
private Socket incoming;
private int counter;
}
这个鸟东西是个新手写的 。唉,太烂了,我无力吐槽 。
请问能不能帮我写一个Java的聊天窗口文件源代码,不要很复杂,只要能运行,聊天就行了!我用淘宝金币换,谢话说网上真的好多啊...
package client;
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.Date;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class ClientFrame extends JFrame{
private JTextArea allmsg;
private JTextField welcome,copyright,chatmsg;
private JButton send;
private JScrollPane js;
private boolean isConnected = true;
public DataOutputStream out;
public DataInputStream in;
public Socket s = null;
String nic;/*-- 保存用户昵称 --*/
/**
* 初始化客户端资源
* 1.获取从LoginFrame传递过来的参数
* 2.初始化界面元素

推荐阅读