0
我有一個服務器和客戶端,在我的服務器用戶界面中,我有3個JLabel使用MouseClick我與連接到我的服務器的個人客戶端進行通信。當我點擊JLabel1
消息時,Client1
將在Client1
收到消息時迴應,但它在收到消息時不響應服務器。希望有人指導我的代碼有什麼問題。服務器沒有收到消息 - Java
// SERVER
void connect_clients()
{
try {
ServerSocket listener = new ServerSocket(7700);
jButton1.setText("Server Running!");
jButton1.setEnabled(false);
while (true) {
socket = listener.accept();
socketList.add(socket);
//RECIEVE METHOD FROM CLIENT WILL COME HERE.
}
}
catch(IOException ex)
{
JOptionPane.showMessageDialog(null, "5"+ex);
}
}
*******here when i click my jlabel1,message is goign to 1st client ,it should respond on seeing the message from client. but it is not.****
private void jLabel1MouseClicked(java.awt.event.MouseEvent evt) {
PrintWriter out;
try {
socket = socketList.get(0);
//JOptionPane.showMessageDialog(null, socket);
out = new PrintWriter(socket.getOutputStream(), true);
out.println("PC 1");
} catch (IOException ex) {
JOptionPane.showMessageDialog(null, "1"+ex);}
// CLIENT
void connect_server() throws IOException
{
try {
// TODO code application logic here
String serverAddress = JOptionPane.showInputDialog(
"Enter IP Address of a machine that is\n" +
"running the date service on port 9090:");
s = new Socket(serverAddress, 7700);
while(true){
BufferedReader input =
new BufferedReader(new InputStreamReader(s.getInputStream()));
String answer = input.readLine();
System.out.println(answer);
if(answer != null)
{
//respond method to server will come here.
JOptionPane.showMessageDialog(null,"Answer is not null");
}
}
}