将信息发送到特定套接字连接

时间:2014-06-04 15:43:31

标签: java sockets chat serversocket

所以我的聊天服务器只能连接到端口5000上的客户端。我只能同时向所有人广播信息。

有没有办法向一个人发送消息?就像当天的服务器消息(或者我编码完全错误的方式,我必须进行大修,如果可以,请列出我需要修复的内容?)

这是我的服务器代码:

public class ServerProgram 
{
JTextArea incoming;
ArrayList clientOutputStreams;
JTextField outgoing;

public class ClientHandler implements Runnable {
    BufferedReader reader;
    Socket sock;


    public ClientHandler(Socket clientSocket){
    try{
         sock = clientSocket;
        InputStreamReader isReader = new InputStreamReader(sock.getInputStream());
        reader = new BufferedReader(isReader);
    } catch(Exception e) {e.printStackTrace();}
    }

    public void run(){
        String message;
        try{
            while ((message=reader.readLine())!=null){
                incoming.append("USER: "+message+"\n");
                tellEveryone(message);
            }
        }catch(Exception e){e.printStackTrace();}
    }
}

public static void main (String[] args){
    new ServerProgram().go();
}

public void go(){
    clientOutputStreams = new ArrayList();
    JFrame frame = new JFrame("Simple Chat Server");
    JPanel mainPanel = new JPanel();
    incoming = new JTextArea(15,50);
    incoming.setLineWrap(true);
    incoming.setWrapStyleWord(true);
    incoming.setEditable(false);
    JScrollPane qScroller = new JScrollPane(incoming);
    qScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
    qScroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
    outgoing = new JTextField(20);
    JButton sendButton = new JButton ("Send");
    sendButton.addActionListener(new SendButtonListener());
    mainPanel.add(qScroller);
    mainPanel.add(outgoing);
    mainPanel.add(sendButton);
    frame.getContentPane().add(BorderLayout.CENTER, mainPanel);
    frame.setSize(600,500);
    frame.setVisible(true);

    try{
        ServerSocket serverSock = new ServerSocket(5000);

        while(true){
            Socket clientSocket = serverSock.accept();
            PrintWriter writer = new PrintWriter(clientSocket.getOutputStream());
            clientOutputStreams.add(writer);

            Thread t = new Thread(new ClientHandler(clientSocket));
            t.start();
            incoming.append("got a connection at "+" \n");}
        }catch (Exception e){e.printStackTrace();}
    }


public void tellEveryone(String message){
    Iterator it = clientOutputStreams.iterator();

    while(it.hasNext()){
        try{
            PrintWriter writer = (PrintWriter) it.next();
            writer.println(message);
            writer.flush();
        }catch(Exception e){e.printStackTrace();}
    }
}
private class SendButtonListener implements ActionListener{
    public void actionPerformed(ActionEvent ev){
        Iterator it = clientOutputStreams.iterator();   
        if(it.hasNext()==false){
            incoming.append(outgoing.getText());
        }
        else{tellEveryone("!SERVER;!"+outgoing.getText());
        incoming.append(outgoing.getText()+"\n");}

        outgoing.setText("");
        outgoing.requestFocus();
    }
}
}

这是我的客户代码:

public class ChatClient
{
JTextArea incoming;
JTextField outgoing;
BufferedReader reader;
PrintWriter writer;
Socket sock;
String UserName;
String ipAdd="127.0.0.1";
int FinishedNet=0;

JMenuBar menuBar;
JMenu menu, submenu;
JMenuItem menuItem;
JRadioButtonMenuItem rbMenuItem;
JCheckBoxMenuItem cbMenuItem;


public static void main(String[] args) throws Exception
{ChatClient chat = new ChatClient();
chat.go();}

public void go(){
menuBar = new JMenuBar();

JFrame frame = new JFrame("Simple Chat client");
JPanel mainPanel = new JPanel();

menu = new JMenu("Menu");
menu.getAccessibleContext().setAccessibleDescription(
        "The only menu in this program that has menu items");
menuBar.add(menu);
cbMenuItem = new JCheckBoxMenuItem("Message AutoScroll");
cbMenuItem.addActionListener(new autoScrollListener());
menu.add(cbMenuItem);
frame.setJMenuBar(menuBar);

incoming = new JTextArea(15,50);
incoming.setLineWrap(true);
incoming.setWrapStyleWord(true);
incoming.setEditable(false);

JScrollPane qScroller = new JScrollPane(incoming);
qScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
qScroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
outgoing = new JTextField(20);

outgoing.addKeyListener(new EnterListener());
JButton sendButton = new JButton ("Send");
sendButton.addActionListener(new SendButtonListener());

mainPanel.add(qScroller);
mainPanel.add(outgoing);
mainPanel.add(sendButton);
setUpNetworking();

Thread readerThread = new Thread(new IncomingReader());
readerThread.start();

frame.getContentPane().add(BorderLayout.CENTER, mainPanel);
frame.setSize(600,500);
frame.setVisible(true);
incoming.setText("Type /Help to get the help Text \n");
}

private void setUpNetworking(){
try{

ipAdd = JOptionPane.showInputDialog("What is the IP you wish to connect to");
UserName = JOptionPane.showInputDialog("What is your UserName?");

sock = new Socket(ipAdd, 5000);
InputStreamReader streamReader = new InputStreamReader(sock.getInputStream());
reader = new BufferedReader(streamReader);
writer = new PrintWriter(sock.getOutputStream());
incoming.append("Networking Finished");
} catch(IOException e){
e.printStackTrace();}
}

public void SendMessage(String message){
writer.println(UserName+": "+message);
writer.flush();
outgoing.setText("");
outgoing.requestFocus();
}

public void SendMessage(){
try{
    incoming.setForeground(Color.RED);
    String outText=outgoing.getText();
    if(outText.length()>1 && outText.substring(0, 1).equals("/")){
    String realOutText=outgoing.getText().substring(1);

        if(realOutText.equals("help")){
            incoming.append("\n help: Displays the help text \n reconnect: reconnects to any IP \n newUserName: Select a new username \n about: Displays the about text \n ipAddress: Displays your public IP Address to only you \n ipAdressAll: Sends your public IP Adress to everyone on the server \n");
        }
        else if(realOutText.equals("about")){
            incoming.append("\n Coded by Dan Janes with the help of Ian Sacco \n Started on 5/27/2014 \n");
        }
        else if(realOutText.equals("newUserName")){
            String old=UserName;
            UserName = JOptionPane.showInputDialog("What is your NEW UserName?");
            writer.println(old+" is now known as "+UserName);
            writer.flush();
        }
        else if (realOutText.equals("reconnect")){
            setUpNetworking();
            }
        else if (realOutText.equals("ipAddress")){
            try{ URL whatismyip = new URL("http://bot.whatismyipaddress.com");
            BufferedReader in = new BufferedReader(new InputStreamReader(
                    whatismyip.openStream()));

                    String ip = in.readLine();
                    incoming.append("Your IP address is "+ip+"\n");}
            catch(Exception e){e.printStackTrace();                 
            }
        }
        else if (realOutText.equals("ipAddressAll")){
            try{URL whatismyip = new URL("http://bot.whatismyipaddress.com");
            BufferedReader in = new BufferedReader(new InputStreamReader(
                    whatismyip.openStream()));

                    String ip = in.readLine();
                    SendMessage( UserName+"'s IP address is "+ip);}
            catch(Exception e){e.printStackTrace();                 
            }
        }

        else{
            incoming.append("Sorry that is not a command, try /help \n");
        }
    }
    else{
    writer.println(UserName+": "+outText);
    writer.flush();
}}catch(Exception e) {e.printStackTrace();}

outgoing.setText("");
outgoing.requestFocus();
}

private class SendButtonListener implements ActionListener{
public void actionPerformed(ActionEvent ev){



        SendMessage();

}
}

private class autoScrollListener implements ActionListener{
public void actionPerformed(ActionEvent ev){

    if(cbMenuItem.isSelected()==true){

    incoming.setCaretPosition(incoming.getDocument().getLength());}

}
}


private class EnterListener implements KeyListener{

@Override
public void keyPressed(KeyEvent arg0) {
    if(arg0.getKeyCode()==10){
        SendMessage();
    }

    }
    @Override
public void keyReleased(KeyEvent arg0) {
    // TODO Auto-generated method stub

}

@Override
public void keyTyped(KeyEvent arg0) {
    // TODO Auto-generated method stub

}

}

public class IncomingReader implements Runnable{
public void run(){
    String message;
    try{
        while ((message=reader.readLine())!=null){System.out.println("read "+message);
        incoming.append(message+"\n");
        if(cbMenuItem.isSelected()==true){

            incoming.setCaretPosition(incoming.getDocument().getLength());}
        }
    }catch(Exception e){e.printStackTrace();}
    }
}
}

0 个答案:

没有答案