Reputation: 43
I'm trying to get how to make a java server/client program but I don't understand why it doesn't work, my server's interface isn't displayed even though I run the loop in a separate thread and I managed to get port errors at some point, I don't even know what to try anymore, so here's the code: (I executed both programs on one single computer)
what am I doing wrong?
client:
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.net.UnknownHostException;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Client extends JFrame {
String sentence;
String modifiedSentence;
int port = 6791;
JTextField tf = new JTextField();
JButton valider = new JButton("Send");
JLabel retour = new JLabel("nothing");
Socket clientSocket;
DataOutputStream outToServer;
BufferedReader inFromServer;
public Client() throws UnknownHostException, IOException{
this.setSize(200, 130);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setTitle("Client");
this.setResizable(false);
JPanel zoneclient = (JPanel)getContentPane();
Dimension d = new Dimension(300 , 25);
tf.setPreferredSize(d);
tf.setMaximumSize(d);
retour.setPreferredSize(d);
valider.setPreferredSize(d);
zoneclient.setLayout(new BoxLayout(zoneclient, BoxLayout.Y_AXIS));
zoneclient.add(new JLabel("Client"));
zoneclient.add(tf);
zoneclient.add(retour);
zoneclient.add(valider);
valider.addActionListener(new appActionListener());
}
class appActionListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
sentence = tf.getText();
try {
BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
clientSocket = new Socket("localhost", port);
clientSocket.setReuseAddress(true);
outToServer = new DataOutputStream(clientSocket.getOutputStream());
inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
} catch (Exception f) {
// TODO: handle exception
System.out.println("connexion failed");
}
try {
outToServer.writeBytes(sentence + '\n');
modifiedSentence = inFromServer.readLine();
retour.setText(modifiedSentence);
clientSocket.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
tf.setText("error");
}
}
}
}
and here's the server:
package server;
import java.awt.Dimension;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
class MyServer extends JFrame{
ServerSocket welcomeSocket;
String clientSentence;
String capitalizedSentence;
JLabel retour = new JLabel("nothing");
public MyServer() throws IOException{
System.out.println("server started");
this.setSize(200, 130);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setTitle("Server");
this.setResizable(false);
JPanel zoneclient = (JPanel)getContentPane();
Dimension d = new Dimension(300 , 25);
retour.setPreferredSize(d);
zoneclient.setLayout(new BoxLayout(zoneclient, BoxLayout.Y_AXIS));
zoneclient.add(new JLabel("Server"));
zoneclient.add(retour);
welcomeSocket = new ServerSocket(6791);
welcomeSocket.setReuseAddress(true);
MyThread th = new MyThread();
th.run();
}
class MyThread extends Thread{
public void run(){
while (true) {
Socket connectionSocket;
try {
connectionSocket = welcomeSocket.accept();
BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());
clientSentence = inFromClient.readLine();
retour.setText("Received: " + clientSentence);
capitalizedSentence = clientSentence.toUpperCase();
outToClient.writeBytes(capitalizedSentence);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("???");
}
}
}
}
}
Upvotes: 0
Views: 356
Reputation: 3433
The problem is the Client code wait for the new line character \n
from server. Since you are calling readLine
of the stream.
modifiedSentence = inFromServer.readLine();
Solution:
Change your server code to write \n
too as below.
outToClient.writeBytes(capitalizedSentence+'\n');
Note: I think you don't need the new BufferedReader(new nputStreamReader(System.in))
inside actionPerformed()
Upvotes: 1