2015-11-23 90 views
0

晚上好,我试图做一个java聊天客户端/服务器,2人之间,一切都运行良好,除了我欺骗了一行代码,我想知道我该怎么做才能做出正确的工作。 (ⅰ将标志着问题行代码)java聊天套接字收到消息时,它不显示在客户端

package sockets; 

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.io.PrintWriter; 
import java.net.InetAddress; 
import java.net.Socket; 
import java.util.Scanner; 

public class Client 
{ 
    public static void main(String[] args) throws IOException, Throwable 
    { 
    Socket s; 
    BufferedReader in; 
    PrintWriter out; 
    Scanner sc = new Scanner(System.in); 
    s = new Socket(InetAddress.getLocalHost(), 9000); 
    System.out.println("Connection pending"); 
    in = new BufferedReader(new InputStreamReader(s.getInputStream())); 
    out = new PrintWriter(s.getOutputStream()); 
    ReceiveMessageThread thread = new ReceiveMessageThread(in); 
    String msg = in.readLine(); 
    System.out.println(msg); 
    thread.start(); 
    while (msg != "") 
    { 
     msg = sc.nextLine(); 
     out.println(msg + "\n"); 
     out.flush(); 
    } 
    s.close(); 
    sc.close(); 
} 
} 

================================= ==========

package sockets; 

import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.io.PrintWriter; 
import java.net.Socket; 

import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JTextArea; 
import javax.swing.JTextField; 

public class TClient extends Thread 
{ 
private int num; 
private Socket s; 
private BufferedReader in; 
private PrintWriter out; 
private JFrame frame; 
private JButton button; 
private JTextField txt; 
private JTextField named; 
private JTextArea chat; 

public JTextArea getChat() 
{ 
    return chat; 
} 

public int getNum() 
{ 
    return num; 
} 

public void setNum(int num) 
{ 
    this.num = num; 
} 

public Socket getS() 
{ 
    return s; 
} 

public BufferedReader getIn() 
{ 
    return in; 
} 

public String getTxt() 
{ 
    return txt.getText(); 
} 

public String getNamed() 
{ 
    return named.getText(); 
} 

public PrintWriter getOut() 
{ 
    return out; 
} 

public TClient(Socket s, int num) throws IOException 
{ 
    this.s = s; 
    this.setNum(num); 
    System.out.println("Client " + num); 
    out = new PrintWriter(s.getOutputStream()); 
    in = new BufferedReader(new InputStreamReader(s.getInputStream())); 
    out.println("Connected " + num + "\n"); 
    out.flush(); 
    frame = new JFrame("Chat in sockets"); 
    button = new JButton("Send"); 
    txt = new JTextField(); 
    chat = new JTextArea(); 
    named = new JTextField(); 
    frame.setVisible(true); 
    frame.setSize(500, 500); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setLayout(null); 
    frame.setResizable(false); 
    frame.add(button); 
    frame.add(chat); 
    frame.add(txt); 
    frame.add(named); 
    button.setVisible(true); 
    button.setSize(85, 74); 
    button.setLocation(375, 391); 
    named.setVisible(true); 
    named.setSize(100, 25); 
    named.setLocation(5, 2); 
    txt.setVisible(true); 
    txt.setSize(365, 75); 
    txt.setLocation(5,391); 
    chat.setVisible(true); 
    chat.setEditable(false); 
    chat.setSize(455, 355); 
    chat.setLocation(5,30); 
    button.addActionListener(new ActionListener() 
    { 
     public void actionPerformed(ActionEvent e) 
     { 
      sendMessage(named.getText()+" : "+txt.getText()+"\n"); 
      chat.append(named.getText()+" : "+txt.getText()+"\n"); 
      txt.setText(""); 
     } 
    }); 
} 

public void run() 
{ 
    while (true) 
    { 

     try 
     { 
      String msg = ""; 
      msg = in.readLine(); 
      System.out.println("client(" + getNum() + ")" + msg); 
      chat.append("client(" + getNum() + ")" + msg); 
      sendMessage(msg); 
      if (msg.equals(".")) 
       break; 
     } catch (IOException e) 
     { 
      e.printStackTrace(); 
     } 
    } 
    try 
    { 
     s.close(); 
    } 
    catch (IOException e) 
    { 
     e.printStackTrace(); 
    } 
} 

public void sendMessage(String message) 
{ 
    int client = (getNum()-1)==1 ? 0:1; 
    Server.send(client, message); 
      System.out.printf("Sending message(%s) to client:%d from client:%d%n",message,client,getNum()); 
} 
} 

================================= ========================

package sockets; 

import java.io.IOException; 
import java.net.ServerSocket; 
import java.net.Socket; 

public class Server 
{ 
static TClient[] connexions = new TClient[2]; 

public static void main(String[] args) 
{ 
    ServerSocket ss; 
    Socket s = null; 
    int nb_clients = 0; 
    try 
    { 
     ss = new ServerSocket(9000); 
     System.out.println("Server is listening in:" + ss.getLocalPort()); 
     boolean continu = false; 
     while (!continu) 
     { 
      s = ss.accept(); 
      connexions[nb_clients] = new TClient(s, nb_clients + 1); 
      connexions[nb_clients].start(); 
      nb_clients++; 
      if (nb_clients > 2) 
       continu = true; 
     } 
     System.out.println("Clients connected"); 
     s.close(); 
     ss.close(); 
    } 
    catch (IOException e) 
    { 
     e.printStackTrace(); 
    } 
} 

public static void send(int clientNum, String message) { 
    TClient t = connexions[clientNum]; 
    if(t != null) 
    { 
     t.getOut().println(message); 
     t.getChat().append(message);//this line is not correct even it shows a correct result 
     t.getOut().flush(); 
    } 
} 
} 

=================== =========================

package sockets; 

import java.io.BufferedReader; 
import java.io.IOException; 

public class ReceiveMessageThread extends Thread 
{ 
private BufferedReader in; 

public ReceiveMessageThread(BufferedReader in) 
{ 
    this.in = in; 
} 

public void run() 
{ 
    while(true) 
    { 
     try 
     { 
      String message = readMessage(in); 
      if(message!=null && !message.equals("")) 
      { 
       System.out.println(message); 
      } 

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

public String readMessage(BufferedReader in) throws IOException 
{ 
    String msgreceived = ""; 
    String readValue = ""; 
    while(in.ready()) 
    { 
     readValue = in.readLine(); 
     if(readValue !=null) 
      msgreceived += readValue; 
    } 
    return msgreceived; 
} 
} 
+1

您从未指定过任何问题。此外,这是很多代码。创建一个再现问题的小程序 –

回答

0
while(in.ready()) 

通常会滥用此方法。循环只有在有东西需要读取时才会执行,这可能不是真的:您可能需要阻止。将其更改为

while (true) 

,如果你从readLine()得到null,返回null让来电者知道对方已经关闭了连接。

+0

问题不在这里,它在服务器 –

+0

中t.getChat()。append(message);这是我的问题,似乎不正确的使用它 –

相关问题