求一个JAVA编的聊天小程序。

我刚学JAVA,哪位高手给我个简单(超简单)的聊天程序让我学习下?... 我刚学JAVA,哪位高手给我个简单(超简单)的聊天程序让我学习下? 展开
 我来答
祚胤17
2009-06-04 · TA获得超过786个赞
知道大有可为答主
回答量:1149
采纳率:82%
帮助的人:264万
展开全部
以下基于TCP/IP协议:

16.聊天室服务器端逻辑
//Debug.java
import java.util.*;
public class Debug {
public static String p(String s) {
System.out.println(s);
return s;
}
public static Object p(Object O) {
System.out.println(O.toString());
return O;
}
public static Object[] p(Object[] O) {
System.out.print("[");
String s = "";
for (Object o : O) {
System.out.print(s);
System.out.print(o.toString());
s = ",";
}
System.out.println("]");
return O;
}
public static int p(int i) {
System.out.println(i);
return i;
}
public static long p(long i) {
System.out.println(i);
return i;
}
public static Map.Entry p(Map.Entry l) {
System.out.println(l);
return l;
}
public static HashSet p(HashSet l) {
System.out.println(l);
return l;
}
public static HashMap p(HashMap l) {
System.out.println(l);
return l;
}
public static Map p(Map l) {
System.out.println(l);
return l;
}
public static LinkedHashSet p(LinkedHashSet l) {
System.out.println(l);
return l;
}
public static TreeSet p(TreeSet l) {
System.out.println(l);
return l;
}
public static Set p(Set l) {
System.out.println(l);
return l;
}
public static ArrayList p(ArrayList l) {
System.out.println(l);
return l;
}
public static LinkedList p(LinkedList l) {
System.out.println(l);
return l;
}
public static List p(List l) {
System.out.println(l);
return l;
}
}

//Service.java
import java.io.*;
import java.net.*;
import java.util.*;
public class Service extends Thread{
private Socket client=null;
private Server server=null;
public Service(Server server,Socket client){
this.server=server;
this.client=client;
}
public void run(){
try {
BufferedReader br=new BufferedReader(new InputStreamReader(client.getInputStream()));
while(true){
String str=br.readLine();
List<Socket> li=server.getLi();
for(Socket c:li){
if(!c.isClosed() && c.isConnected()){
OutputStream os=c.getOutputStream();
os.write((str+"\n").getBytes());
}else{
client.close();
server.getLi().remove(client);
break;
}
}
}
}catch (IOException e) {
e.printStackTrace();
}
}
}

//Server.java
import java.util.*;
import java.io.*;
import java.net.*;
public class Server {
private List<Socket> li=new ArrayList<Socket>();
private ServerSocket ss=null;
public List<Socket> getLi(){
return li;
}
public Server(int port){
try {
ss=new ServerSocket(port);
} catch (IOException e) {
e.printStackTrace();
}
}
public void start(){
while(true){
Socket s;
try {
s = ss.accept();
li.add(s);
new Service(this,s).start();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
new Server(8888).start();
}
}

17.聊天室客户端逻辑
//Debug.java
import java.util.*;
public class Debug {
public static String p(String s) {
System.out.println(s);
return s;
}
public static Object p(Object O) {
System.out.println(O.toString());
return O;
}
public static Object[] p(Object[] O) {
System.out.print("[");
String s = "";
for (Object o : O) {
System.out.print(s);
System.out.print(o.toString());
s = ",";
}
System.out.println("]");
return O;
}
public static int p(int i) {
System.out.println(i);
return i;
}
public static long p(long i) {
System.out.println(i);
return i;
}
public static Map.Entry p(Map.Entry l) {
System.out.println(l);
return l;
}
public static HashSet p(HashSet l) {
System.out.println(l);
return l;
}
public static HashMap p(HashMap l) {
System.out.println(l);
return l;
}
public static Map p(Map l) {
System.out.println(l);
return l;
}
public static LinkedHashSet p(LinkedHashSet l) {
System.out.println(l);
return l;
}
public static TreeSet p(TreeSet l) {
System.out.println(l);
return l;
}
public static Set p(Set l) {
System.out.println(l);
return l;
}
public static ArrayList p(ArrayList l) {
System.out.println(l);
return l;
}
public static LinkedList p(LinkedList l) {
System.out.println(l);
return l;
}
public static List p(List l) {
System.out.println(l);
return l;
}
}

//GetMessage.java
import javax.swing.*;
import java.io.*;
import java.net.*;
public class GetMessage extends Thread{
private JTextArea context=null;
private Socket s=null;
public GetMessage(JTextArea context,Socket s){
this.context=context;
this.s=s;
}
public void run(){
try {
BufferedReader br=new BufferedReader(new InputStreamReader(s.getInputStream()));
while(true){
String str=br.readLine();
String c=context.getText();
context.setText(c+"\n"+str);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

//Client.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
public class Client extends JFrame {
private String ip = "";
private int port = 0;
private Socket s = null;
private JTextArea context = new JTextArea(10, 10);
JScrollPane sp = new JScrollPane(context);
private JTextField say = new JTextField(10);
private Container c;
public Client(String ip, int port) {
c = this.getContentPane();
c.setLayout(new BorderLayout());
context.setEditable(false);
c.add(sp);
JPanel p = new JPanel();
p.add(BorderLayout.CENTER, say);
c.add(BorderLayout.SOUTH, p);
try {
s = new Socket(ip, port);
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
say.addActionListener(new ActionListener() {
private void send(String str) {
try {
OutputStream os = s.getOutputStream();
os.write(str.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
}
public void actionPerformed(ActionEvent arg0) {
String str = say.getText();
send(str + "\n");
say.setText("");
}
});
new GetMessage(context, s).start();
this.setSize(300, 300);
this.setVisible(true);
this.setResizable(false);
}
public static void main(String[] args) {
String strIP = null;
try {
strIP = InetAddress.getLocalHost().getHostAddress().toString();
} catch (UnknownHostException e) {
e.printStackTrace();
}
new Client(strIP, 8888); //"127.0.0.1"
}
}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式