黑马程序员技术交流社区
标题:
自己用tcp写的聊天工具,有点功能不能实现,帮忙看下?
[打印本页]
作者:
吴小铁你好
时间:
2012-6-5 22:44
标题:
自己用tcp写的聊天工具,有点功能不能实现,帮忙看下?
为什么发送信息不能显示啊,我测试过应该发出去了?
//服务端
package server;
import java.rmi.ConnectException;
import java.awt.Image;
import javax.swing.*;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class ServerDemo {
public static void main (String []args)throws ConnectException{
new Jframe();
}
}
class Jframe {
public Jframe()throws ConnectException{
JFrame jf=new JFrame("服务端");
JFrame.setDefaultLookAndFeelDecorated(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JComponent com=new Jpanel();
com.setOpaque(true);
jf.setContentPane(com);
ImageIcon icon=new ImageIcon("02.gif");
Image icon2=icon.getImage();
jf.setIconImage(icon2);
jf.setBounds(500,500,350,350);
jf.setResizable(false);
jf.setVisible(true);
}
}
@SuppressWarnings("serial")
class Jpanel extends JPanel implements ActionListener{
private JTextArea area1,area2;
private JButton button;
String str5=null;
public Jpanel() throws ConnectException{
JPanel panel1=new JPanel();
JPanel panel2=new JPanel();
area1=new JTextArea(10,25);
area2=new JTextArea(5,18);
JScrollPane scro1=new JScrollPane(area1);
JScrollPane scro2=new JScrollPane(area2);
setBorder(BorderFactory.createEtchedBorder(Color.BLUE,Color.blue));
area1.setBorder(BorderFactory.createEtchedBorder(Color.BLUE,Color.blue));
area2.setBorder(BorderFactory.createLineBorder(Color.red));
panel1.setLayout(new GridLayout(1,1));
panel1.add(scro1);
button=new JButton("发送(Q)");
button.setForeground(Color.CYAN);
button.setMnemonic(KeyEvent.VK_Q);
button.addActionListener(this);
panel2.setLayout(new FlowLayout());
panel2.add(scro2);
panel2.add(button);
setLayout(new FlowLayout());
add(panel1);
add(panel2);
//event();
}
/*private void event() throws ConnectException {
// TODO Auto-generated method stub
ServerText text=new ServerText(str5);
area1.setText(text.toString());
}*/
public void actionPerformed(ActionEvent e){
str5=area2.getText();
ServerText text = null;
try {
text = new ServerText(str5);
} catch (ConnectException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
area1.setText(text.toString());
/* try {
@SuppressWarnings("unused")
ServerText text=new ServerText(str5);
} catch (ConnectException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
*/
area2.setText("");
}
}
class ServerText {
//
//StringBuffer str3=null;
String str2=null;
//ServerSocket server=new ServerSocket(3005);
public ServerText(String Obj)throws ConnectException{
try {
ServerSocket server=new ServerSocket(10222);
while(true){
Socket sc=server.accept();
@SuppressWarnings("unused")
String str=sc.getInetAddress().getHostName();
//读取接受的信息
BufferedReader bu=new BufferedReader(new InputStreamReader(sc.getInputStream()));
//发送信息
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(sc.getOutputStream()));
while((str2=bu.readLine())!=null){
System.out.println(str2);
//str3.append(str2);
bw.write(Obj);
bw.newLine();
bw.flush();
}
bw.close();
server.close();
sc.close();
toString();
}
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch(NullPointerException e){
System.out.print(e.toString());
}
}
public String toString(){
//String str4=toString(str2);
System.out.println(str2);
return str2;
}
}
//客户端
package mybao;
import java.rmi.ConnectException;
import java.awt.Image;
import javax.swing.*;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.UnknownHostException;
public class ScokeDemo {
public static void main(String []args) throws ConnectException{
new Jframe1();
}
}
class Jframe1 {
public Jframe1() throws ConnectException{
JFrame jf=new JFrame("客户端");
JFrame.setDefaultLookAndFeelDecorated(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JComponent com=new Jpanel_1();
com.setOpaque(true);
jf.setContentPane(com);
ImageIcon icon=new ImageIcon("02.gif");
Image icon2=icon.getImage();
jf.setIconImage(icon2);
jf.setBounds(500,500,350,350);
jf.setResizable(false);
jf.setVisible(true);
}
}
@SuppressWarnings("serial")
class Jpanel_1 extends JPanel implements ActionListener{
private JTextArea area1,area2;
private JButton button;
String str5=null;
public Jpanel_1() throws ConnectException{
JPanel panel1=new JPanel();
JPanel panel2=new JPanel();
area1=new JTextArea(10,25);
area2=new JTextArea(5,18);
JScrollPane scro1=new JScrollPane(area1);
JScrollPane scro2=new JScrollPane(area2);
setBorder(BorderFactory.createEtchedBorder(Color.BLUE,Color.blue));
area1.setBorder(BorderFactory.createEtchedBorder(Color.BLUE,Color.blue));
area2.setBorder(BorderFactory.createLineBorder(Color.red));
panel1.setLayout(new GridLayout(1,1));
panel1.add(scro1);
button=new JButton("发送(D)");
button.setForeground(Color.CYAN);
button.setMnemonic(KeyEvent.VK_D);
button.addActionListener(this);
panel2.setLayout(new FlowLayout());
panel2.add(scro2);
panel2.add(button);
setLayout(new FlowLayout());
add(panel1);
add(panel2);
//event();
}
/*private void event() throws ConnectException {
ScokeText text= new ScokeText(str5);
area1.setText(text.toString());
}*/
public void actionPerformed(ActionEvent e){
str5=area2.getText();
ScokeText text = null;
try {
text = new ScokeText(str5);
} catch (ConnectException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
area1.setText(text.toString());
/* try {
@SuppressWarnings("unused")
ScokeText text=new ScokeText(str5);
} catch (ConnectException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}*/
area2.setText("");
}
}
class ScokeText {
//StringBuffer str3=null;
String str2=null;
//HashSet<String> h=new HashSet<String>();
public ScokeText(String string) throws ConnectException {
try {
Socket s=new Socket("127.0.0.1",10222);
BufferedReader br=new BufferedReader(new InputStreamReader(s.getInputStream()));
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
while((str2=br.readLine())!=null){
//str3.append(str2);
bw.write(string);
bw.newLine();
bw.flush();
toString();
}
s.close();
bw.close();
br.close();
//toString(str2);
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch(NullPointerException e){
System.out.print(e.toString());
}
}
public String toString(){
/*Iterator<String> it=h.iterator();
String str3=null;
while(it.hadNext){
str3=it.next();
}*/
//String str4=str3.toString();
System.out.println(str2);
return str2;
}
}
作者:
何拴绪
时间:
2012-6-5 23:00
你把问题说的具体点,这么长的代码,没有很多时间看,你要实现什么功能,哪里出错了?你贴出来呀
作者:
黑马-唐磊
时间:
2012-6-5 23:13
你什么东西不能实现啊,这么长的代码谁有这个闲心去看啊???
作者:
吴小铁你好
时间:
2012-6-6 09:08
在聊天时,发送消息不能再另一边显示。
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2