| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 591 人关注过本帖
标题:问题:聊天工具的转发功能!
只看楼主 加入收藏
toshiba
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2007-6-9
收藏
 问题点数:0 回复次数:4 
问题:聊天工具的转发功能!

public class m implements Runnable {
Socket sock;
int id;
ArrayList list;
int i=0;
public void run() {
Iterator it=list.iterator();
DataOutputStream dout;
try {
dout = new DataOutputStream(sock.getOutputStream() );
System.out.println(sock.getInetAddress()+"...."+id);
dout.writeUTF( "sock"+id+"connect!");
while(true){
DataInputStream in=new DataInputStream(sock.getInputStream() );
String str=in.readUTF();
System.out.println(str+'\n');
while(it.hasNext())
{

Socket s=(Socket)(it.next()) ;

DataOutputStream out=new DataOutputStream(s.getOutputStream() );//我想用这一部分做转发功
out.writeUTF( str);                   //能,但出了问题!搞了好就

}                                //久!就来这求救!
}
}
catch (IOException e) {
e.printStackTrace();
}
}
public m(Socket s,int i,ArrayList list)
{
sock=s;
id=i;
this.list=list;
}

}

//////////server///////
public class mm {

static int n=0;

public static void main(String[] args) {
System.out.println("start");
ArrayList list=new ArrayList();
try {
ServerSocket s=new ServerSocket(3333);
while(true){

n=n+1;
Socket client=s.accept() ;
list.add(client);
m m1=new m(client,n,list);

Thread th=new Thread(m1);
th.start();
System.out.println("connect!!");

}

} catch (IOException e) {
e.printStackTrace();
}

}
}
////////client/////
public class cc extends JFrame implements ActionListener {
JTextField f1;
JTextArea a1;
JScrollPane s1;
ServerSocket server;
Socket sock;
public cc()
{
f1=new JTextField(30);
a1=new JTextArea(35,35);
s1=new JScrollPane(a1);
getContentPane().add(f1,"North");
getContentPane().add(s1,"Center");
setSize(300,300);
setVisible(true);
f1.addActionListener(this);
setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
try {

sock=new Socket(InetAddress.getLocalHost(),3333);
//a1.append( "server connect ok");
DataOutputStream dout=new DataOutputStream(sock.getOutputStream() );
dout.writeUTF("client\n");

while(true){
DataInputStream in=new DataInputStream(sock.getInputStream() );
a1.append(in.readUTF()+"\n") ;
}

} catch (IOException e) {
e.printStackTrace();
}
}

public static void main(String[] args) {
new cc();
}
public void actionPerformed(ActionEvent arg0) {
DataOutputStream dout;
try {
dout = new DataOutputStream(sock.getOutputStream() );
String s=f1.getText() ;
dout.writeUTF(s);
a1.append(s+"\n");
} catch (IOException e) {
e.printStackTrace();
}
}
}

搜索更多相关主题的帖子: 聊天工具 
2007-06-09 14:35
野蛮女人
Rank: 5Rank: 5
等 级:贵宾
威 望:19
帖 子:827
专家分:0
注 册:2007-4-7
收藏
得分:0 

package net;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.Socket;
import java.awt.event.KeyEvent;
import javax.swing.KeyStroke;
import javax.swing.JComponent;
import java.net.ServerSocket;
import java.util.LinkedList;
import java.io.*;
import java.awt.event.WindowEvent;
import java.awt.event.WindowAdapter;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class Qq extends JFrame implements ActionListener {

/**
*
*/
private static final long serialVersionUID = -3859430711620083215L;

private static JTextArea textArea_1;

private static JTextArea textArea;

private JButton button;

private static JList list;

private JComboBox comboBox;

private static LinkedList<Socket> list_1;

private static LinkedList<ListenSocket> list_2;

private static ServerSocket ss = null;

private static Socket s = null;

private final JButton button_1;

private static int m;

private String msg = null;

private static DefaultListModel listmodel;

/**
* Launch the applicat
*
* @param args
*/
public static void main(String args[]) {

Qq frame = new Qq();
frame.setVisible(true);

// Thread rec=new Thread(frame);
// rec.start();
while (true) {// 监听客户连接
try {
s = ss.accept();
System.out.println("====="+s);
ListenSocket socket = new ListenSocket(textArea, s);

AddConnect(s, socket);
socket.start();

} catch (Exception e) {

}

}
}

/**
* Create the frame
*/
public Qq() {
super("学习交流聊天室");
getContentPane().setBackground(Color.GREEN);
getContentPane().setLayout(null);
setBounds(100, 100, 439, 490);
// setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// this.setResizable(false);
list_1 = new LinkedList<Socket>();

final JLabel label = new JLabel();
label.setBounds(0, 0, 431, 59);
getContentPane().add(label);

comboBox = new JComboBox();
comboBox.setBounds(0, 406, 97, 29);
comboBox.addItem("all");
getContentPane().add(comboBox);

button = new JButton();

button.setText("发送");
button.setBounds(103, 406, 77, 29);
button.addActionListener(this);
button.registerKeyboardAction(this, KeyStroke.getKeyStroke(
KeyEvent.VK_ENTER, java.awt.event.InputEvent.CTRL_DOWN_MASK),
JComponent.WHEN_IN_FOCUSED_WINDOW);
getContentPane().add(button);

button_1 = new JButton();
button_1.setText("关闭");
button_1.addActionListener(this);
button_1.setBounds(199, 406, 77, 29);
getContentPane().add(button_1);
listmodel = new DefaultListModel();

list = new JList(listmodel);

JScrollPane scrol = new JScrollPane(list);
scrol.setBounds(282, 66, 152, 369);
getContentPane().add(scrol);

final JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(0, 65, 276, 262);
getContentPane().add(scrollPane);

textArea = new JTextArea();// 聊天窗口
textArea.setLineWrap(true);
textArea.setEditable(false);
scrollPane.setViewportView(textArea);

final JScrollPane scrollPane_1 = new JScrollPane();
scrollPane_1.setBounds(0, 333, 276, 67);
getContentPane().add(scrollPane_1);

textArea_1 = new JTextArea();
textArea_1.setLineWrap(true);
scrollPane_1.setViewportView(textArea_1);

try {
ss = new ServerSocket(5000);
textArea.append("服务器启动\n");
} catch (Exception e) {

}
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
try {
ss.close();
System.exit(0);

}

catch (Exception ee) {

}
}
});

}

public static int geti(String s) {
return listmodel.indexOf(s);

}

public static void sendtosomebody(int i, String msg) {
Socket s = (Socket) list_1.get(i);
try {
PrintWriter sos = new PrintWriter(s.getOutputStream());
sos.println(msg);
sos.flush();
} catch (Exception e) {

}
}

public void actionPerformed(ActionEvent e) {
Object soso = e.getSource();
if (soso == button) {
if (!textArea_1.getText().trim().equals("")) {
msg = textArea_1.getText();
sendtoall("系统消息:" + msg);
textArea.append("系统消息:" + msg + "\n");
textArea_1.setText("");
}

}

if (soso == button_1) {
try {
ss.close();
dispose();

}

catch (Exception ee) {

}

}
}

public static void sendtoall(String msg) {
if (list_1 != null) {
for (int i = 0; i < list_1.size(); i++) {
Socket s = (Socket) list_1.get(i);
try {
PrintWriter sos = new PrintWriter(s.getOutputStream());
sos.println(msg);
sos.flush();
} catch (Exception e) {

}

}
}
}

public static void updatename() {
if (listmodel.getSize() > 1) {
for (int i = 0; i < list_1.size() - 1; i++) {
Socket s = (Socket) list_1.get(i);
try {
PrintWriter sos = new PrintWriter(s.getOutputStream());
sos.println("listmodel.getElementAt(i).toString()"
+ listmodel.getElementAt(listmodel.getSize() - 1));
;
sos.flush();

} catch (Exception e) {

}
}
}
}

public static void sendallname() {
if (listmodel.getSize() >= 1) {
for (int i = 0; i < listmodel.getSize(); i++) {// 用户打开聊天框发送确保无重名
Socket s = (Socket) list_1.get(list_1.size() - 1);
try {
PrintWriter sos = new PrintWriter(s.getOutputStream());
sos.println("listmodel.getElementAt(i).toString()"
+ listmodel.getElementAt(i).toString());
sos.flush();// 必须有此句

} catch (Exception e) {

}
}
}

}

public static void deletename(String msg) {
int i = geti(msg);
Socket s = (Socket) list_1.remove(i);
listmodel.remove(i);

try {
s.close();
} catch (Exception e) {

}
// ListenSocket socket=(ListenSocket)list_2.remove(i);

}

public static void addname(String msg) {// 添加用户列表
listmodel.addElement(msg);

}

public static void AddConnect(Socket s, ListenSocket socket) {
if (list_1 == null) {
list_1 = new LinkedList<Socket>();
} else {
list_1.add(s);
}
if (list_2 == null) {
list_2 = new LinkedList<ListenSocket>();
} else {
list_2.add(socket);
}
}
}

class ListenSocket extends Thread {
private Socket s = null;

private ServerSocket ss = null;

private BufferedReader in = null;

private PrintWriter out = null;

private boolean go;

private JTextArea a1;

private String msg = "";

private int count;

public ListenSocket(JTextArea a1, Socket s) {

this.a1 = a1;

this.s = s;
this.go = true;
this.count = 0;
}

public void run() {// 监听客户发送信息
String msg = null;

try {

in = new BufferedReader(new InputStreamReader(s.getInputStream()));
out = new PrintWriter(s.getOutputStream());
} catch (Exception e) {

}

if (count == 0) {
Qq.sendallname();
}

while (go) {

try {

msg = in.readLine().toString();
System.out.println("====Run===="+msg);
count++;
if (count == 1) {

Qq.addname("※" + msg + "※");
Qq.sendtoall("【" + msg + "】" + "上线了!");
Qq.updatename();
a1.append("【" + msg + "】" + "上线了!\n");
} else {
if (msg
.indexOf("comboBox.getSelectedItem().equals(all)",
0) != -1) {
Qq.sendtoall(msg.substring(38));
a1.append(msg.substring(38) + '\n');
} else {
if (msg.indexOf("this.addWindowListener(new WindowAdapter())",0) != -1) {
int nam = msg.indexOf("※");
String name = msg.substring(nam);
Qq.deletename(name);
Qq.sendtoall(msg);

} else {
int i = msg
.indexOf("this.listmodel.addElement(msg.substring(36))");
int ii = msg.indexOf("对", i);
int j = msg
.indexOf("mn=listmodel.indexOf(kehu)", i);
int jj = msg.indexOf("说", j);

String s1 = msg.substring(0, i);
String s2 = msg.substring(ii + 1, j);
int mm = Qq.geti(s1);
int nn = Qq.geti(s2);
Qq.sendtosomebody(mm, msg.substring(0, i)
+ msg.substring(ii, j) + msg.substring(jj));
Qq.sendtosomebody(nn, msg.substring(0, i)
+ msg.substring(ii, j) + msg.substring(jj));
a1.append(msg.substring(0, i)
+ msg.substring(ii, j) + msg.substring(jj));
}
}

}

} catch (Exception e) {

}

}
}

}
===================================
package net;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.Socket;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.io.*;
import java.awt.event.WindowEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.KeyEvent;
import javax.swing.KeyStroke;
import javax.swing.JComponent;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JOptionPane;

public class Client extends JFrame implements ActionListener, Runnable,
ListSelectionListener {

private JTextField textField;

private DefaultListModel listmodel;

private JTextArea textArea_1;

private JTextArea textArea;

private final JLabel label_1;

private JList list;

private final JButton button;

private JComboBox comboBox;

private final JScrollPane scrollPane_2;

private final JButton button_1;

private Socket socket = null;

protected String username = new String("游客");

private BufferedReader in = null;

private PrintWriter out = null;

private final JButton button_2;

private String msg = null;

private boolean kesong = false;

/**
* Launch the applicat
*
* @param args
*/
/*
* public static void main(String args[]) { try { Qq frame = new Qq();
* frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } }
*/

/**
* Create the frame
*/
public Client() {
super("学习交流聊天室");

getContentPane().setBackground(Color.GREEN);
getContentPane().setLayout(null);
setBounds(100, 100, 439, 490);
// setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
final JLabel label = new JLabel();
label.setBounds(0, 0, 431, 59);
getContentPane().add(label);

comboBox = new JComboBox();
comboBox.setBounds(0, 406, 97, 29);
comboBox.addItem("all");
getContentPane().add(comboBox);

button = new JButton();

button.setText("发送");
button.addActionListener(this);
button.registerKeyboardAction(this, KeyStroke.getKeyStroke(
KeyEvent.VK_ENTER, java.awt.event.InputEvent.CTRL_DOWN_MASK),
JComponent.WHEN_IN_FOCUSED_WINDOW);
button.setBounds(103, 406, 77, 29);
getContentPane().add(button);

button_1 = new JButton();
button_1.setText("关闭");
button_1.addActionListener(this);
button_1.setBounds(199, 406, 77, 29);
getContentPane().add(button_1);

listmodel = new DefaultListModel();

list = new JList(listmodel);
list.addListSelectionListener(this);

scrollPane_2 = new JScrollPane(list);
scrollPane_2.setBounds(282, 66, 152, 262);
getContentPane().add(scrollPane_2);

final JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(0, 65, 276, 262);
getContentPane().add(scrollPane);

textArea = new JTextArea();
textArea.setLineWrap(true);
textArea.setEditable(false);
scrollPane.setViewportView(textArea);

final JScrollPane scrollPane_1 = new JScrollPane();
scrollPane_1.setBounds(0, 333, 276, 67);
getContentPane().add(scrollPane_1);

textArea_1 = new JTextArea();
textArea_1.setLineWrap(true);
scrollPane_1.setViewportView(textArea_1);

label_1 = new JLabel();
label_1.setText("昵称");
label_1.setBounds(282, 336, 31, 15);
getContentPane().add(label_1);

textField = new JTextField();
textField.setBounds(306, 334, 85, 20);
getContentPane().add(textField);

button_2 = new JButton();
button_2.setText("确定");
button_2.addActionListener(this);
button_2.setBounds(282, 364, 77, 23);
getContentPane().add(button_2);


try {
socket = new Socket("127.0.0.1", 5000);
textArea.append("已连接服务器\n");
in = new BufferedReader(new InputStreamReader(socket
.getInputStream()));
out = new PrintWriter(socket.getOutputStream());
} catch (Exception e) {
System.out.println(e.toString());
}
Thread rec = new Thread(this);
rec.start();
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
if (kesong) {
out.println("this.addWindowListener(new WindowAdapter()"
+ "※" + username + "※");
out.flush();
try {
socket.close();
} catch (Exception ee) {

}
System.exit(0);

} else {

try {
socket.close();
} catch (Exception ee) {

}
dispose();
}
}
});

}

public void valueChanged(ListSelectionEvent e) {
if (e.getValueIsAdjusting() == false) {
if (list.getSelectedIndex() != -1) {
if (comboBox.getItemCount() > 1) {
comboBox.removeItemAt(1);
comboBox.addItem(list.getSelectedValue().toString());
comboBox.setSelectedIndex(1);
} else {
comboBox.addItem(list.getSelectedValue().toString());
comboBox.setSelectedIndex(1);
}
}
}

}

public boolean denglu(String s) {
String m = "※" + s + "※";
boolean go = true;
for (int i = 0; i < listmodel.getSize(); i++) {
if (listmodel.getElementAt(i).equals(m)) {
go = false;
}
}
return go;
}

public void actionPerformed(ActionEvent e) {
if (e.getSource() == button_2) {

if (!textField.getText().trim().equals("")
&& denglu(textField.getText().trim())) {
username = textField.getText();
kesong = true;
label_1.setVisible(false);
textField.setVisible(false);
button_2.setVisible(false);
scrollPane_2.setBounds(282, 66, 152, 369);
try {

out.println(username);
out.flush();

} catch (Exception ee) {
System.out.println(e.toString());
}

} else {
JOptionPane.showMessageDialog(null, "昵称不能为空或已有人使用此名字");
}
}
if (e.getSource() == button) {
if (kesong) {

if (!textArea_1.getText().trim().equals("")) {// 去掉空格不为空
try {

if (comboBox.getSelectedItem().equals("all")) {
msg = "comboBox.getSelectedItem().equals(all)"
+ "※" + username + "※" + "说:"
+ textArea_1.getText();
} else {
msg = "※"
+ username
+ "※"
+ "this.listmodel.addElement(msg.substring(36))对"
+ comboBox.getSelectedItem().toString()
+ "mn=listmodel.indexOf(kehu)说:"
+ textArea_1.getText();
}
out.println(msg);
textArea_1.setText("");
out.flush();
} catch (Exception ee) {

}
}
} else {
JOptionPane.showMessageDialog(null, "请确定昵称");
}
}

if (e.getSource() == button_1) {
if (kesong) {
out.println("this.addWindowListener(new WindowAdapter()" + "※"
+ username + "※");
out.flush();
try {
socket.close();
} catch (Exception ee) {

}
dispose();

} else {

try {
socket.close();
} catch (Exception ee) {

}
dispose();
}

}
}

public void run() {
String msg = null;

while (true) {
try {

msg = in.readLine();
if (msg.indexOf("listmodel.getElementAt(i).toString()", 0) != -1) {// 接收用户列表
this.listmodel.addElement(msg.substring(36));
} else {
if (msg.indexOf(
"this.addWindowListener(new WindowAdapter()", 0) != -1) {
int i = msg.indexOf("※");
String kehu = msg.substring(i);
int mn = listmodel.indexOf(kehu);// 查找下线客户并删除
listmodel.remove(mn);
if (comboBox.getItemCount() > 1) {
if (comboBox.getItemAt(1).equals(kehu)) {
comboBox.removeItemAt(1);
comboBox.setSelectedIndex(0);
}
}
textArea.append(kehu + "离开了聊天室\n");
} else {
textArea.append(msg + "\n");// 接受消息
}
}

} catch (Exception e) {

}
}
}

public static void main(String args[]) {
Client client = new Client();
client.setVisible(true);
}

}

可以看看


[shadow=255,purple,5]好人不长命,祸害一万年![/shadow]
2007-06-09 21:20
toshiba
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2007-6-9
收藏
得分:0 

你好厉害哦!你在工作还是在校?
你可以说明一下我的程序错在哪吗?
你的代码有点长,我看了有点

2007-06-09 22:00
a276202460
Rank: 2
等 级:新手上路
威 望:4
帖 子:392
专家分:1
注 册:2007-4-10
收藏
得分:0 

ServerSocket s=new ServerSocket(3333);
while(true){

n=n+1;
Socket client=s.accept() ;
list.add(client);
m m1=new m(client,n,list);

Thread th=new Thread(m1);
th.start();
System.out.println("connect!!");

}你想为每个客户创建一个进程 为什么还在每个进程里用list增加用户呢?后一个线程里面都有前面的用户信息
 100个用户的话就有100个线程同时监听客户一,不知道能不能行 自己在改下:)
楼上的楼上的程序 好眼熟啊


2007-06-10 10:31
toshiba
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2007-6-9
收藏
得分:0 

我用了类似你那种 for (int i = 0; i < list_1.size(); i++) {
Socket s = (Socket) list_1.get(i);

的方法,已经搞定了!!!!!!!!!

非常感谢!!!!!!!!!!!!

2007-06-10 10:43
快速回复:问题:聊天工具的转发功能!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.051027 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved