晚上好,我試圖做一個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;
}
}
您從未指定過任何問題。此外,這是很多代碼。創建一個再現問題的小程序 –