D
David Bouchard
Hi,
I'm trying to do a very simple client/server application :
I have 2 text fields and a button in a jframe
The first textfield is editable whereas the second is not.
i want to send what's typed in the first textfield to the server when
the user clicks the button. the server then modifies slightly the
string and sends it back to the client. the client then displays the
string it received from the server into the second textfield
when i run the code below, the server does not receive any string and i
really don't know why...
could someone help ?
thanks !
David.
PS : I know the in.readLine() are useless here but this is a test
application and in my 'real' application I will need them so they must
be used here.
PS 2 : if I uncomment the line //out.close(); in the method
sendAndReceive, the server gets the string from the client but then
fails to respond
(sendAndReceive : error java.net.SocketException: socket closed)
import java.awt.BorderLayout;
import java.awt.GridLayout;
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.ServerSocket;
import java.net.Socket;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import org.apache.log4j.Logger;
public class TestSocket
{
public static void main(String[] args)
{
launchServer();
JFrame frame = new JFrame("Client");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JTextField tfSend = new JTextField();
final JTextField tfReceived = new JTextField();
tfReceived.setEditable(false);
JButton boutonEnvoi = new JButton("Send");
boutonEnvoi.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
sendAndReceive(tfSend, tfReceived);
}
});
JPanel tfPanel = new JPanel(new GridLayout(2,2));
tfPanel.add(new JLabel("Send"));
tfPanel.add(tfSend);
tfPanel.add(new JLabel("Received"));
tfPanel.add(tfReceived);
JPanel mainPanel = new JPanel(new BorderLayout());
mainPanel.add(tfPanel, BorderLayout.CENTER);
mainPanel.add(boutonEnvoi, BorderLayout.SOUTH);
frame.setContentPane(mainPanel);
frame.setSize(250, 100);
frame.setVisible(true);
}
public static void launchServer()
{
Thread t = new Thread()
{
public void run()
{
try
{
ServerSocket ss = new ServerSocket(12012);
System.out.println("[SERVER] started");
for ( ; ; )
{
Socket s = ss.accept();
System.out.println("[SERVER] client connexion");
dealConnexion(s);
}
}
catch (IOException e)
{
System.err.println("run : error : "+e);
e.printStackTrace();
}
}
public void dealConnexion(final Socket s)
{
Thread t = new Thread()
{
public void run()
{
try
{
BufferedReader in = new BufferedReader(
new InputStreamReader(s.getInputStream()));
PrintWriter out =
new PrintWriter(s.getOutputStream());
// read from client
StringBuffer sb = new StringBuffer();
String ligne = null;
sb.setLength(0);
while ((ligne = in.readLine()) != null)
{
sb.append(ligne);
System.out.println("[SERVER] received line '"
+ligne+"'");
}
System.out.println("[SERVER] received '"
+ sb.toString()+"'");
// write to client
out.println("You sent '" + sb.toString() + "'");
}
catch (IOException e)
{
System.err.println("gererConnexion : error : "+e);
e.printStackTrace();
}
}
};
t.start();
}
};
t.start();
}
public static void sendAndReceive(final JTextField toSendTF,
final JTextField receivedTF)
{
Thread t = new Thread()
{
public void run()
{
try
{
Socket s = new Socket("127.0.0.1", 12012);
PrintWriter out = new PrintWriter(s.getOutputStream());
BufferedReader in = new BufferedReader(
new InputStreamReader(s.getInputStream()));
StringBuffer sb = new StringBuffer();
String line = null;
// send what's in text field toSendTF to server
String txt = toSendTF.getText();
if ( null==txt || txt.length()==0 )
{
System.err.println("[CLIENT] nothing to send!");
return;
}
toSendTF.setText(null);
System.out.println("[CLIENT] sends ["+txt+"]");
out.println(txt);
//out.close();
toSendTF.setText(null);
// read response from server
sb.setLength(0);
while ((line = in.readLine()) != null)
{
sb.append(line);
System.out.println("[CLIENT] gets line ["+line+"]");
}
System.out.println("[CLIENT] received '"+sb.toString()+"'");
receivedTF.setText(sb.toString());
s.close();
}
catch (Exception e)
{
System.err.println("sendAndReceive : error "+e);
e.printStackTrace();
}
}
};
t.start();
}
}
I'm trying to do a very simple client/server application :
I have 2 text fields and a button in a jframe
The first textfield is editable whereas the second is not.
i want to send what's typed in the first textfield to the server when
the user clicks the button. the server then modifies slightly the
string and sends it back to the client. the client then displays the
string it received from the server into the second textfield
when i run the code below, the server does not receive any string and i
really don't know why...
could someone help ?
thanks !
David.
PS : I know the in.readLine() are useless here but this is a test
application and in my 'real' application I will need them so they must
be used here.
PS 2 : if I uncomment the line //out.close(); in the method
sendAndReceive, the server gets the string from the client but then
fails to respond
(sendAndReceive : error java.net.SocketException: socket closed)
import java.awt.BorderLayout;
import java.awt.GridLayout;
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.ServerSocket;
import java.net.Socket;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import org.apache.log4j.Logger;
public class TestSocket
{
public static void main(String[] args)
{
launchServer();
JFrame frame = new JFrame("Client");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JTextField tfSend = new JTextField();
final JTextField tfReceived = new JTextField();
tfReceived.setEditable(false);
JButton boutonEnvoi = new JButton("Send");
boutonEnvoi.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
sendAndReceive(tfSend, tfReceived);
}
});
JPanel tfPanel = new JPanel(new GridLayout(2,2));
tfPanel.add(new JLabel("Send"));
tfPanel.add(tfSend);
tfPanel.add(new JLabel("Received"));
tfPanel.add(tfReceived);
JPanel mainPanel = new JPanel(new BorderLayout());
mainPanel.add(tfPanel, BorderLayout.CENTER);
mainPanel.add(boutonEnvoi, BorderLayout.SOUTH);
frame.setContentPane(mainPanel);
frame.setSize(250, 100);
frame.setVisible(true);
}
public static void launchServer()
{
Thread t = new Thread()
{
public void run()
{
try
{
ServerSocket ss = new ServerSocket(12012);
System.out.println("[SERVER] started");
for ( ; ; )
{
Socket s = ss.accept();
System.out.println("[SERVER] client connexion");
dealConnexion(s);
}
}
catch (IOException e)
{
System.err.println("run : error : "+e);
e.printStackTrace();
}
}
public void dealConnexion(final Socket s)
{
Thread t = new Thread()
{
public void run()
{
try
{
BufferedReader in = new BufferedReader(
new InputStreamReader(s.getInputStream()));
PrintWriter out =
new PrintWriter(s.getOutputStream());
// read from client
StringBuffer sb = new StringBuffer();
String ligne = null;
sb.setLength(0);
while ((ligne = in.readLine()) != null)
{
sb.append(ligne);
System.out.println("[SERVER] received line '"
+ligne+"'");
}
System.out.println("[SERVER] received '"
+ sb.toString()+"'");
// write to client
out.println("You sent '" + sb.toString() + "'");
}
catch (IOException e)
{
System.err.println("gererConnexion : error : "+e);
e.printStackTrace();
}
}
};
t.start();
}
};
t.start();
}
public static void sendAndReceive(final JTextField toSendTF,
final JTextField receivedTF)
{
Thread t = new Thread()
{
public void run()
{
try
{
Socket s = new Socket("127.0.0.1", 12012);
PrintWriter out = new PrintWriter(s.getOutputStream());
BufferedReader in = new BufferedReader(
new InputStreamReader(s.getInputStream()));
StringBuffer sb = new StringBuffer();
String line = null;
// send what's in text field toSendTF to server
String txt = toSendTF.getText();
if ( null==txt || txt.length()==0 )
{
System.err.println("[CLIENT] nothing to send!");
return;
}
toSendTF.setText(null);
System.out.println("[CLIENT] sends ["+txt+"]");
out.println(txt);
//out.close();
toSendTF.setText(null);
// read response from server
sb.setLength(0);
while ((line = in.readLine()) != null)
{
sb.append(line);
System.out.println("[CLIENT] gets line ["+line+"]");
}
System.out.println("[CLIENT] received '"+sb.toString()+"'");
receivedTF.setText(sb.toString());
s.close();
}
catch (Exception e)
{
System.err.println("sendAndReceive : error "+e);
e.printStackTrace();
}
}
};
t.start();
}
}