当前位置: 代码迷 >> Java相关 >> 求助 谁来帮我看一下这个关于套接字的程序
  详细解决方案

求助 谁来帮我看一下这个关于套接字的程序

热度:244   发布时间:2011-07-15 17:27:14.0
求助 谁来帮我看一下这个关于套接字的程序
我想写一个类似于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 java.net.Socket;
import java.net.ServerSocket;
import java.io.PrintStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.InputStream;
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 java.net.Socket;
import java.io.PrintStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
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();
            }
        }
        
    }
}
搜索更多相关的解决方案: 输入文字  服务器  控制台  客户端  

----------------解决方案--------------------------------------------------------
我也等答案!
----------------解决方案--------------------------------------------------------
呃 那不要让他沉了阿 在顶格
----------------解决方案--------------------------------------------------------
呃 那不要让他沉了阿 在顶格
----------------解决方案--------------------------------------------------------
好乱。。像是写客户机与客户聊天。。
你在往客户机发信息的时候,没有往你list里的每个客户发送。。而且你那个应该写ps.println(line);把接受的到内容发给所有的客户,server只拿来中转。。
----------------解决方案--------------------------------------------------------
  相关解决方案