Thanks. I have tried it but there it doesnt work.
Can anybody tell me whats wrong:
Server.
/*
* Client.java
*
* Created on 27 augustus 2006, 15:21
*/
package chatapplicatie;
import java.awt.event.ActionEvent;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
/**
*
* @author Rthijssen
*/
public class Server extends javax.swing.JFrame {
/** Creates new form Client */
public Server() {
initComponents();
setupServer();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc=" Generated Code ">
private void initComponents() {
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(0, 400, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(0, 300, Short.MAX_VALUE)
);
pack();
}// </editor-fold>
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Server().setVisible(true);
}
});
}
// Variables declaration - do not modify
// End of variables declaration
public void setupServer(){
ServerSocket server;
try {
server = new ServerSocket(4321);
Socket client = new Socket("Client", 4321);
client = server.accept();
BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
PrintWriter out = new PrintWriter(client.getOutputStream(), true);
while(true){
String line = in.readLine();
out.println(line);
System.out.println(line);
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
Client:
/*
* Client.java
*
* Created on 27 augustus 2006, 15:37
*/
package chatapplicatie;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
/**
*
* @author Rthijssen
*/
public class Client extends javax.swing.JFrame {
PrintWriter out;
BufferedReader in;
/** Creates new form Client */
public Client() {
initComponents();
listenSocket();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc=" Generated Code ">
private void initComponents() {
btnSend = new javax.swing.JButton();
txtInput = new javax.swing.JTextField();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
btnSend.setText("Send");
btnSend.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnSendActionPerformed(evt);
}
});
org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.addContainerGap()
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(txtInput, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 160, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(btnSend))
.addContainerGap(230, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.addContainerGap()
.add(txtInput, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(btnSend)
.addContainerGap(241, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void btnSendActionPerformed(java.awt.event.ActionEvent evt) {
String text = txtInput.getText();
out.println(text);
txtInput.setText(new String(""));
out.println(text);
try {
String line = in.readLine();
} catch (IOException ex) {
ex.printStackTrace();
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Client().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton btnSend;
private javax.swing.JTextField txtInput;
// End of variables declaration
public void listenSocket(){
try {
Socket socket = new Socket("Client", 4321);
out = new PrintWriter(socket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
} catch (UnknownHostException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
> Thanks. I have tried it but there it doesnt work.
> Can anybody tell me whats wrong:
The first thing that's wrong is that you haven't told us how it doesn't work.
The second thing that's wrong is that you are conducting blocking operations on Sockets in your Server and Client constructors, and in the AWT thread via actionPerformedListeners.
The third thing that's wrong is that your server can only handle one client at a time.
You need more threads.
I suggest you read Example 2 and the link at the bottom of Lesson 1 above.
Im sorry you are completley right. Here the answers to your questions.
When i run the server and/or the client i get a UnknownHostException. And it comes from the line where i define the socket.
And it is my intention that only 1 client can comunicate with the server.
Finaly im sorry but i don't know what you mean with: "The second thing that's wrong is that you are conducting blocking operations on Sockets in your Server and Client constructors, and in the AWT thread via actionPerformedListeners."
Greetz LeDieu