标题:求助 谁来帮我看一下这个关于套接字的程序
取消只看楼主
st_win
Rank: 2
等 级:论坛游民
帖 子:48
专家分:35
注 册:2010-9-19
结帖率:100%
已结贴  问题点数:50 回复次数:2 
求助 谁来帮我看一下这个关于套接字的程序
我想写一个类似于qq聊天窗口的程序,目前这个只是写了一个单方向的从服务器的窗口输入文字然后再客户端的控制台上可以看到,可是不知道为什么不能实现通信还有希望可以告送我怎么样可以方便的实现从服务器的Textfield中输入文字能在客户端的TextArea上看到
程序代码:
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import java.awt.TextField;
import java.awt.TextArea;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ChatPanel
{
    JFrame jframe=new JFrame("主机");
     TextField textfield=new TextField(30);
     JButton button=new JButton("发送");
    ServerChat se=new ServerChat();
    
    //private JList chat=new JList();
    //private LinkedList<String> content=new LinkedList<String>();
     TextArea textcontent=new TextArea(10,10);
    private final int NUM=15;
    public void init()
    {
            createframe();
             se.link();    
        button.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent evt)
            {
                se.setL(textfield.getText());
                se.send(se.list.get(0));                
                textcontent.setText("你对"+"说:"+textfield.getText()+"\n");
                 textfield.setText("");
            }
        });
            textfield.setText("");                            
            
        
             System.out.println("se.link()");
    }
    public void createframe()
    {
        JPanel panel=new JPanel();
        JPanel textpanel=new JPanel();
        textpanel.add(textfield);
        textpanel.add(button);
        panel.setLayout(new BorderLayout());
        panel.add(textcontent);
        panel.add(textpanel,BorderLayout.SOUTH);
        jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jframe.setPreferredSize(new Dimension(400,300));
        jframe.add(panel);
        jframe.setVisible(true);
        jframe.pack();

    }
    public static void main(String[] args) 
    {
        new ChatPanel().init();
    }
}
import import import import import import import import java.util.ArrayList;
import javax.swing.JFrame;
class  ServerChat
{
    ServerSocket serversocket;
    Socket socket;
    String liaotian;
    ArrayList<Socket> list=new ArrayList<Socket>();
    public void setL(String liao)
    {
        liaotian=liao;
        System.out.println(liaotian);
    }
    public void link()
    {
       try
       {
        serversocket=new ServerSocket(30010);
        socket=serversocket.accept();
        list.add(socket);

       }
       catch (IOException io)
       {
         io.printStackTrace();
       }
    }
    public void send(Socket s)
    {
        ServerThread st=new ServerThread(s);
        st.liaotian=liaotian;
            st.start();
    }

}
class ServerThread extends Thread
{
    Socket socket;
    BufferedReader br;
    PrintStream ps;
    String line;
    String liaotian;
    public ServerThread(Socket socket)
    {
         this.socket=socket;         
    }
    public void run()
    {
        
        try
        {
             System.out.println("run()");
             br=new BufferedReader(new InputStreamReader(socket.getInputStream()));
            ps=new PrintStream(socket.getOutputStream());
             while((line=br.readLine())!=null)
              {
                   ps.println("zhenfule");
              }             
        }
        catch (IOException io)
        {
            io.printStackTrace();
        }        
    }
    public void Sclose()
    {
          try
            {
                 socket.close();
    
            }
            catch (IOException io)
            {
               io.printStackTrace();
            }
    }
}
import java.awt.TextArea;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ChatPanelc
{
    private JFrame jframe=new JFrame("客户端");
    private TextField textfield=new TextField("",30);
    private JButton button=new JButton("发送");
    Clientsocket se=new Clientsocket();
    private TextArea textcontent=new TextArea(10,10);
    private final int NUM=15;
    public void init()
    {        
        se.init();
        button.addActionListener(new  ActionListener()
        {
            public void actionPerformed(ActionEvent ae)
            {
                
            }
        });    
        createframe();    
        
    }
    public void createframe()
    {
        JPanel panel=new JPanel();
        JPanel textpanel=new JPanel();
        textpanel.add(textfield);
        textpanel.add(button);
        panel.setLayout(new BorderLayout());
        panel.add(textcontent);
        panel.add(textpanel,BorderLayout.SOUTH);
        jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jframe.setPreferredSize(new Dimension(400,300));
        jframe.add(panel);
        jframe.setVisible(true);
        jframe.pack();

    }
    public static void main(String[] args) 
    {
        new ChatPanelc().init();
    }
}

import import import import import import import class Clientsocket 
{
    Socket socket;

    public void init()
    {
        try
        {
            socket=new Socket("127.0.0.1",30010);
            System.out.println("连接成功");
            new ClientThread(socket).start();
            System.out.println("连接成功");
            
        }
        catch (IOException io)
        {
            io.printStackTrace();
        }
    }
}
class ClientThread extends Thread
{
    Socket socket;
    BufferedReader br;
    PrintStream ps;
    String line;
    public ClientThread(Socket socket)
    {
        this.socket=socket;
       
    }
    public void run()
    {
        
        try
        {
             
            br=new BufferedReader(new InputStreamReader(socket.getInputStream()));
                System.out.println("bufferedreader");        
            while((line=br.readLine())!=null)
            {
                 System.out.println(line);
            }
           
        }
        catch (IOException io)
        {
            io.printStackTrace();
        }
        finally 
        {
            try
            {
                
                  br.close();
            
            }
            catch (IOException io)
            {
               io.printStackTrace();
            }
        }
        
    }
}
搜索更多相关主题的帖子: 输入文字 服务器 控制台 客户端 
2011-07-15 17:27
st_win
Rank: 2
等 级:论坛游民
帖 子:48
专家分:35
注 册:2010-9-19
得分:0 
呃 那不要让他沉了阿 在顶格
2011-07-15 23:33
st_win
Rank: 2
等 级:论坛游民
帖 子:48
专家分:35
注 册:2010-9-19
得分:0 
呃 那不要让他沉了阿 在顶格
2011-07-15 23:33



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-345588-1-1.html




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

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