aspose file tools
The moose likes Swing / AWT / SWT and the fly likes Question as follows Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Reply Bookmark "Question as follows" Watch "Question as follows" New topic
Author

Question as follows

Sccot Smith
Greenhorn

Joined: Apr 20, 2012
Posts: 24
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

Joined: Oct 02, 2003
Posts: 9948
    
    6

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?




Never ascribe to malice that which can be adequately explained by stupidity.
Sccot Smith
Greenhorn

Joined: Apr 20, 2012
Posts: 24
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

Joined: Aug 07, 2007
Posts: 1156
    
    3

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

Joined: Apr 20, 2012
Posts: 24
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

Joined: Apr 20, 2012
Posts: 24
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

Joined: Aug 07, 2007
Posts: 1156
    
    3

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.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Question as follows
 
Similar Threads
4th time i am asking.Please see the code.
Amit Agarwal jee!! please see this.
s.accept ( ) method, gives me an error ?
chat programe, kindly see the code and help me.
Problem in Networking concept