• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question as follows

 
Sccot Smith
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a simple chatting program.
The source code:
ChatClient.java
import java.awt.*;
import java.io.*;
import java.net.*;
import java.awt.event.*;
public class ChatClient extends Frame {

Socket s = null;
TextField tfInput = new TextField();
TextArea taDisplay = new TextArea();
DataOutputStream dos = null;
public static void main(String[] args) {
new ChatClient().launchFrame();
}

private void launchFrame()
{
setLocation(400,300);
setSize(300,300);
tfInput.addActionListener(new InputListener());
add(tfInput,BorderLayout.SOUTH);
add(taDisplay,BorderLayout.NORTH);
addWindowListener(new WindowAdapter(){


public void windowClosing(WindowEvent e) {
Disconnect();
System.exit(0);
}

});
pack();
setVisible(true);
Connect();
}

public void Disconnect()
{
try {
dos.close();
s.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

private class InputListener implements ActionListener
{

public void actionPerformed(ActionEvent e) {
String str = null;
str = tfInput.getText();
taDisplay.append(str+"\n");
tfInput.setText("");
try {
dos.writeUTF(str);
dos.flush();
//dos.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}

}

private void Connect()
{
try {
s = new Socket ("127.0.0.1",8888);
dos = new DataOutputStream(s.getOutputStream());
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

}
}


ChatServer.java
import java.io.DataInputStream;
import java.io.IOException;
import java.net.*;
public class ChatServer {

public static void main(String[] args) {

boolean started = false;
try {
ServerSocket ss = new ServerSocket(8888);
started = true;
while(true)
{
Socket s = new Socket();
s = ss.accept();
System.out.println("a client connected!!!");
DataInputStream dis = new DataInputStream (s.getInputStream());
String str = dis.readUTF();
System.out.println(str);
//dis.close();
}
} catch (IOException e) {

e.printStackTrace();
}

}

}


The question is : why does ServerChat cant receive the message from ChatClient in the second time
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12100
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please read our post on how to use code tags (here). I've put them around your code here. See how much easier it is to read?


 
Sccot Smith
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:Please read our post on how to use code tags (here). I've put them around your code here. See how much easier it is to read?



Thank you very much!!!
 
Tony Docherty
Bartender
Posts: 2953
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure exactly what your question means but looking at the server code once the socket reads a String it prints it out and then calls accept() again so it will wait for a client to connect before doing anything else.

BTW why are you creating a Socket (line 97) and then calling accept() (line 98) which will return a Socket and overwrite the reference you have just stored in 's'.
 
Sccot Smith
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Docherty wrote:I'm not sure exactly what your question means but looking at the server code once the socket reads a String it prints it out and then calls accept() again so it will wait for a client to connect before doing anything else.

BTW why are you creating a Socket (line 97) and then calling accept() (line 98) which will return a Socket and overwrite the reference you have just stored in 's'.



That's for listening for a connection to be made to this socket and accepts it!
 
Sccot Smith
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Docherty wrote:I'm not sure exactly what your question means but looking at the server code once the socket reads a String it prints it out and then calls accept() again so it will wait for a client to connect before doing anything else.

BTW why are you creating a Socket (line 97) and then calling accept() (line 98) which will return a Socket and overwrite the reference you have just stored in 's'.


My question is :l can receive the message when l send a message to the server by the client in the first time,but in the second time,l send it again,the server can't receive that
 
Tony Docherty
Bartender
Posts: 2953
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's for listening for a connection to be made to this socket and accepts it!

Which one of the 2 points I made does this answer refer to?

So what you are saying is you have designed the Server so that every time the Client wants to send a message to the Server it has to connect, send the message and then disconnect. But you have designed the Client so it connects, can send multiple messages and then disconnects. It looks like a mismatch to me.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic